Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Collision Issue  (Read 1818 times)

0 Members and 1 Guest are viewing this topic.
Collision Issue
« on: April 18, 2011, 10:51:50 pm »
  • ...---^^^---...
  • *
  • Reputation: +6/-0
  • Offline Offline
  • Gender: Male
  • Posts: 616
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!
Logged
Re: Collision Issue
« Reply #1 on: April 18, 2011, 10:55:29 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
May I suggest you use some trig?  If you know the angle of incidence you can use sin and cos to calculate the bounce.
Logged



i love big weenies and i cannot lie
Re: Collision Issue
« Reply #2 on: April 18, 2011, 11:29:27 pm »
  • ...---^^^---...
  • *
  • Reputation: +6/-0
  • Offline Offline
  • Gender: Male
  • Posts: 616
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.
Logged

Xiphirx

wat
Re: Collision Issue
« Reply #3 on: April 19, 2011, 12:34:38 am »
  • Xiphirx
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3007
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.
Logged
  • For The Swarm
Re: Collision Issue
« Reply #4 on: April 19, 2011, 12:46:30 am »
  • Wooper Don't Give a !@#$%
  • *
  • Reputation: +16/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1457
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
Logged
ROLL TIDE WHAT? **** YOU!!! Geaux Tiga

~The Gaurdians of ZFGC~
Kirby, metallica48423, Max, Vash, walnut100
  • Gamers & Developers Unlimited
Re: Collision Issue
« Reply #5 on: April 22, 2011, 08:00:22 pm »
  • ...---^^^---...
  • *
  • Reputation: +6/-0
  • Offline Offline
  • Gender: Male
  • Posts: 616
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.
Logged
Pages: [1]   Go Up

 


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



Page created in 0.049 seconds with 50 queries.