Math: Creating Physics Trajectory

R

Robert

Guest
Hello, I am hoping to get some help with creating a physics trajectory. For example in Angry Birds when you aim it draws a line showing where your object will go. I want to add something similar to my game, but the object is moving in physics. I did some research and wasn't able to find anything for Game Maker specifically, however i did find the link below. I wasn't sure how to convert it to GML, I tried but I couldn't get it to work. Any idea how I would do this?

http://www.iforce2d.net/b2dtut/projected-trajectory

Thanks
 

jo-thijs

Member
That doesn't use the physics system.

Now, may I ask, do you just want to calculate the initial parabola?
Or do you want to calculate some bounces as well?

The first one is easy, the second one isn't.
 
R

Robert

Guest
Alexx I'll take a look at that script and see if there isn't anything I can use from it.

Jo-thijs to start just simple trajectory would be okay, but I will need bounce trajectory as well because the ball might bounce off a wall.
 

jo-thijs

Member
Hm, there's apparently no function to get the gravity of the physics world, nor the pixeltometer scale, nor a function to simulate iterations yourself.
This makes an automated script to predict the trajectory impossible.

To predict it yourself, you would need to know the gravity, initial impuls, the linear damping and the initial position.
With this, you can calculate the initial trajectory, ignoring bounces.

To calculate bounces, you would need to know the angular velocity, the angular damping, the shapes of the colliding objects, ...
Too much to take into account really.
 
R

Robert

Guest
Ho Jo, thanks for the great response. Maybe the reason I can't seem to find anything on this with GML and physics is because it's not possible? Reading through that article I linked it appears there are two ways to create this effect, the plugin method or the emulated method. The plugin method is basically just running the physics properties through a formula and predicting the trajectory on it's result. The emulated method requires actually making of copy of the current physics world and manually stepping through it to figure out the future trajectory.

Now, regarding the plugin method, let me ask a few things. Why can't I get the current gravity and the pixel to meters? I set those as static values in the room, so wouldn't they just be the values that I set? If I made the y gravity 100, wouldn't the gravity be 100? Sorry if this is a super stupid question lol, I haven't really looked into this kind of stuff since high scool which was a long time ago. Also you mention simulating iterations, but wouldn't I only need that for the emulated method?

Here is what I have right now, it works, but not really. I can get the line to look like a sloped line, but it is nothing to do with the objects actual path. I feel like I am really missing something here.

Code:
///scr_trajectory
var t = 1/60;  //60fps
var stepVelocity = t*argument[1];
var stepGravity = t*t*120;
return argument[0] + argument[2] * stepVelocity + 0.5 * (argument[2]*argument[2]+argument[2]) * stepGravity;
Code:
///physics object that is drawing this line
var cur_x = x;
var cur_y = y;
draw_set_color(c_lime);
draw_set_alpha(1);
for (var i = 0; i < 180; i++) { // three seconds at 60fps
    //show_debug_message()
    var new_x = scr_trajectory(x,222,i); //why 222 work here I dont know....
    var new_y = scr_trajectory(y,-444,i); //neither -444
    draw_line(cur_x,cur_y,new_x,new_y);
    cur_x = new_x;
    cur_y = new_y;
}
How does this know what the angle is supposed to be?
 

jo-thijs

Member
That's all correct, except the 100 would need units.

I'm not sure what you're trying to do in the code you gave though.
To explain how to calculate the trajectory, it would be handy though t know if you're using linear damping.
 
R

Robert

Guest
No, thankfully, since I would have to calculate that into it. Regarding the code I posted, I had some more thought on it and I think its working better now, though I am still uncertain on some things. For example, I changed the code to this:

Code:
///scr_trajectory (added gravity as argument[3])
var t = 1/60;
var stepVelocity = t*argument[1];
var stepGravity = t*t*argument[3];
return argument[0] + argument[2] * stepVelocity + 0.5 * (argument[2]*argument[2]+argument[2]) * stepGravity;
Code:
var pd = point_direction(x,y,device_mouse_x(0),device_mouse_y(0));
draw_sprite_ext(spr_ball_handle,0,x,y,1,1,pd,c_white,1);

var cur_x = x;
var cur_y = y;
draw_set_color(c_lime);
draw_set_alpha(1);
for (var i = 0; i < 120; i++) { // three seconds at 60fps
    var new_x = scr_trajectory(x,-lengthdir_x(500,pd)*trajectory,i,0);
    var new_y = scr_trajectory(y,-lengthdir_y(500,pd)*trajectory,i,120);
    draw_line(cur_x,cur_y,new_x,new_y);
    cur_x = new_x;
    cur_y = new_y;
}
and now my trajectories look closer to what they are, here is an example (the green line):



However, it isn't perfect. The goal is you pull down on the ball like a sling shot and then the green line is drawn as a trajectory as where the ball will go. In my code I changed it so that I pass the gravity to the scr as it was applying the same gravity to both axis, but there is only a y grav. I also added a trajectory variable that normalizes between 0 and 1, 1 being pulled all the way down and 0 being idle.I also added lengthdir_x/y to the stepVelocity as I realized that is what gives it the angle. So it seems to work, but its not very accurate. Notice that I put 500 in the lengthdir... Why 500? What should this value be? What is it? The actual force that I apply to the ball is only 20 (whatever 20 is when passing that value to the force function).
 
Top