D
deciduous
Guest
Hello there!
I'm looking for some help with an algorithm that allows the player object to bounce off of a rectangular wall.
Some Context: in the game your player object is rotating around a circular "platform". You can tap the screen (or left click) to launch the player at any given time and it will follow its current direction until it interacts will another object, such as a bouncy wall. The bouncy walls are long and thin, and you are meant to bounce off one of the two long sides.
The Problem: Currently the bouncy walls work just fine positioned at any angle as long as you hit one of the long sides, but when you collide with one of the two end caps (shorter sides) of the rectangle it takes on a new direction as though you had hit a long side. (ie: if the wall is positioned at a 45 degree angle and you are travelling upwards at a 90 degree angle you should bounce off the long side and take on a 0 degree angle, and if you were to hit the shorter side you should take on a 180 degree angle. However the shorter side will also give you a new angle of 0. I've drawn an example image and posted it to the bottom of the post.)
Here is the current working code:
if(place_meeting(x, y, obj_wall))
{
if(!global.mute)
audio_play_sound(snd_bounce, 0, false);
the_wall = instance_place(x, y, obj_wall);
launch_dir = (2 * the_wall.image_angle) - launch_dir; // launch_dir is the direction the player is travelling
x += lengthdir_x(move_speed_x, launch_dir);
y += lengthdir_y(move_speed_y, launch_dir);
}
I understand the issue and why it does not work. One possible solution would be to calculate whether or not the player collides with the rectangle and it is in between the coordinates for either long side of the rectangle, but this is challenging because lots of the bouncy walls are positioned at an angle other than 0 or 90 degrees.
My apologies if my explanation is long winded or confusing. Please let me know if I can clear any confusion up, and thank you in advance for any help you may give!
I'm looking for some help with an algorithm that allows the player object to bounce off of a rectangular wall.
Some Context: in the game your player object is rotating around a circular "platform". You can tap the screen (or left click) to launch the player at any given time and it will follow its current direction until it interacts will another object, such as a bouncy wall. The bouncy walls are long and thin, and you are meant to bounce off one of the two long sides.
The Problem: Currently the bouncy walls work just fine positioned at any angle as long as you hit one of the long sides, but when you collide with one of the two end caps (shorter sides) of the rectangle it takes on a new direction as though you had hit a long side. (ie: if the wall is positioned at a 45 degree angle and you are travelling upwards at a 90 degree angle you should bounce off the long side and take on a 0 degree angle, and if you were to hit the shorter side you should take on a 180 degree angle. However the shorter side will also give you a new angle of 0. I've drawn an example image and posted it to the bottom of the post.)
Here is the current working code:
if(place_meeting(x, y, obj_wall))
{
if(!global.mute)
audio_play_sound(snd_bounce, 0, false);
the_wall = instance_place(x, y, obj_wall);
launch_dir = (2 * the_wall.image_angle) - launch_dir; // launch_dir is the direction the player is travelling
x += lengthdir_x(move_speed_x, launch_dir);
y += lengthdir_y(move_speed_y, launch_dir);
}
I understand the issue and why it does not work. One possible solution would be to calculate whether or not the player collides with the rectangle and it is in between the coordinates for either long side of the rectangle, but this is challenging because lots of the bouncy walls are positioned at an angle other than 0 or 90 degrees.
My apologies if my explanation is long winded or confusing. Please let me know if I can clear any confusion up, and thank you in advance for any help you may give!