• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

how do I do top down ball physics?

I am coding a multiplayer top-down golf game. I already understand how to add collisions with 90-degree angles. I would also like to add some 45-degree angles but I have no idea how to do this. I do not want to use the built-in physics engine for this project.
 

curato

Member
you will need more control than the built in bounce collision if you want what to do fake physics instead of using the physics engine. Just off the top of my head you would need to look when I collision happens at the criteria you want to control what angle you want it to bounce off at change the direction of the ball to match.
 
Thanks I am curently using a system like this:

//
if(place_meeting(x + hsp, y, Wall))
{
x += hsp;
}
else
{
while(!place_meeting(x + sign(hsp), y, Wall))
{
x += sign(hsp);
}
hsp *= 0.8;
}
//

and then the same for the y axis
I want to make it so that the ball bounces on diagonals too-
 

curato

Member
using movement code like that you are going to have to calculate the bounce yourself. If you hit a flat block reversing the direction of x and y would work fine where a 45 angle block would reverse the direction but relative to the angle of the you are hitting. You will need a little trig if you not going to use physics.
 

Yal

🐧 *penguin noises*
GMC Elder
The fundamental theory of optics is "The incoming and outgoing angle relative to the surface's normal are the same". Because light behaves as particles, this means objects that bounce without deforming will also bounce according to this rule.

So, when there's a collision:
  1. Get the angle_difference between the surface normal (45, 135, 225 or 315 degrees for the four types of 45-degree slopes) and the direction the object is currently moving in plus 180 degrees (to get an outwards direction)
  2. Add a minus sign to that to get the "inverted angle"
  3. The object now moves in direction Surface Normal + Inverted Angle.
The tricky part is figuring out what the surface normal is. You could have an array of 4 values (approaching from the left, right, up and down directions... a cube would have 180, 0, 90 and 270 for these, while a diagonal /| slope would have 135, 0, 135 and 270 - i.e., the left and up normals are the same). Or you could do it properly and mess around with raycasts and stuff.
 
The fundamental theory of optics is "The incoming and outgoing angle relative to the surface's normal are the same". Because light behaves as particles, this means objects that bounce without deforming will also bounce according to this rule.

So, when there's a collision:
  1. Get the angle_difference between the surface normal (45, 135, 225 or 315 degrees for the four types of 45-degree slopes) and the direction the object is currently moving in plus 180 degrees (to get an outwards direction)
  2. Add a minus sign to that to get the "inverted angle"
  3. The object now moves in direction Surface Normal + Inverted Angle.
The tricky part is figuring out what the surface normal is. You could have an array of 4 values (approaching from the left, right, up and down directions... a cube would have 180, 0, 90 and 270 for these, while a diagonal /| slope would have 135, 0, 135 and 270 - i.e., the left and up normals are the same). Or you could do it properly and mess around with raycasts and stuff.
Can you give me an example of what the code would look like? I am still kind of new to game maker.
 

HayManMarc

Member
I've done this in the past, but it took a LOT of effort. Just a warning to you. It may be more work than you want to put in, or even beyond your capabilities, since you say you're new. Don't let it discourage you, but don't let it intimidate you either. Good luck and have fun.
 
I've done this in the past, but it took a LOT of effort. Just a warning to you. It may be more work than you want to put in, or even beyond your capabilities, since you say you're new. Don't let it discourage you, but don't let it intimidate you either. Good luck and have fun.
I have coded on game maker studio for about a year now however I still do not know very much so I am not completely new. I want to learn how to do this so that I can learn more advanced things about coding.
 
Last edited:

Yal

🐧 *penguin noises*
GMC Elder
Can you give me an example of what the code would look like? I am still kind of new to game maker.
First, make four macros dir_R = 0, dir_U = 1, dir_L = 2 and dir_D = 3.


Create an array in every wall object:
GML:
normal[dir_R] = 0
normal[dir_U] = 90
normal[dir_L] = 180
normal[dir_D] = 270

Now the collision event would look like this:
GML:
//Find the direction we're approaching from
minad = 99999
mindir= 0
for(c = dir_R; c <= dir_D; c++){
  ad = abs(angle_difference(direction + 180,c*90));
  if(ad < minad){
    minad = ad;
    mindir = c;
  }
}

//Bounce against the direction we "came the most" from
ang_atk = angle_difference(direction + 180,other.normal[mindir]);
direction = other.normal[mindir] - ang_atk;
 
Top