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.
//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!