ZFGC

Resources => Coding => Topic started by: Aero88 on April 18, 2011, 10:51:50 pm

Title: Collision Issue
Post by: Aero88 on April 18, 2011, 10:51:50 pm
Okay so here is my problem.  I am creating a game in in GM in which a bullet bounces off walls.  Sounds simple enough as it should.  I feel silly that I am having issues with this, but for some reason everything I have tried just doesn't quite work right.  The wall object is a simple 32x32 object, and the bullet object is 9x9, both of which with centered origins.  I am using the following code to make the bullet bounce off of the wall.  This is called inside of the bullet object upon colliding with a wall object.

Code: [Select]
//move outside the object
    while place_meeting(x,y,other)
    {
        x += lengthdir_x(1,direction+180)
        y += lengthdir_y(1,direction+180)
    }
   
    //If Colliding from the top
    if collision_rectangle(bbox_left,bbox_bottom-1,bbox_right,bbox_bottom+1,Solid_Object_Parent,true,true)
    {
        vspeed = -vspeed
        exit;
    }
    //If Colliding from the bottom
    if collision_rectangle(bbox_left,bbox_top-1,bbox_right,bbox_top+1,Solid_Object_Parent,true,true)
    {
        vspeed = -vspeed
        exit;
    }
    //If Colliding from the left
    if collision_rectangle(bbox_right-1,bbox_top,bbox_right+1,bbox_bottom,Solid_Object_Parent,true,true)
    {
        hspeed = -hspeed
        exit;
    }
    //If Colliding from the right
    if collision_rectangle(bbox_left-1,bbox_top,bbox_left+1,bbox_bottom,Solid_Object_Parent,true,true)
    {
        hspeed = -hspeed
        exit;
    }

What is crazy is that it works almost flawlessly, but if the bullet approaches the wall object from the from the left at a very shallow angle, or from the right side at a shallow angle the bullet does not bounce correctly, and for the life of me I can't figure out why.

I was hopping someone here could help me.  Either by suggesting another way to make the bullet bounce off of the wall object, or by correcting my code.

The code needs to be able to precisely determine if the bullet is approaching from top/bottom or left/right, so that the bullet doesn't act weird when hitting corners.  Thanks!
Title: Re: Collision Issue
Post by: MG-Zero on April 18, 2011, 10:55:29 pm
May I suggest you use some trig?  If you know the angle of incidence you can use sin and cos to calculate the bounce.
Title: Re: Collision Issue
Post by: Aero88 on April 18, 2011, 11:29:27 pm
Sorry I should have been more clear.  It is actually not the angle of the bounce that doesn't work correctly.  Reversing the horizontal speed and vertical speeds takes care of that bit.  My problem is that the bullet gets stuck if shot from the angles I mentioned.  I think the problem lies with the "//move outside the object" bit or the checks I am using to see what side of the object the bullet is colliding from, but i don't know for sure.

I am guessing that for some reason if shot from certain angles the function isn't moving the bullet totally off of the wall object.
Title: Re: Collision Issue
Post by: Xiphirx on April 19, 2011, 12:34:38 am
Simple solution, when it collides, you need to move the bullet out of the wall. Right now, what happens is that the bullet comes at a very slight angle, so when it bounces off, it doesn't bounce enough to get out of the wall (vspeed is too small) so it stays in the wall.

So set the y position to the walls y plus the wall height for the top, walls y - ball height for the bottom and you're good to go.
Title: Re: Collision Issue
Post by: Walnut on April 19, 2011, 12:46:30 am
I just came in to say that there is nothing simple about collisions. Ever. At all.

Collisions are still a big research topic for CS since they have practical application past the scope of videogames, and you can always afford to get something like that more efficient
Title: Re: Collision Issue
Post by: Aero88 on April 22, 2011, 08:00:22 pm
Simple solution, when it collides, you need to move the bullet out of the wall. Right now, what happens is that the bullet comes at a very slight angle, so when it bounces off, it doesn't bounce enough to get out of the wall (vspeed is too small) so it stays in the wall.

So set the y position to the walls y plus the wall height for the top, walls y - ball height for the bottom and you're good to go.

Thanks!  I should have seen that.

Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved