Legacy GM Box2D trajectory - help getting code to work

G

Geoff Moore

Guest
I'm making a 2d game using the built in Box2D physics where you throw a ball around and I want to draw a simple trajectory to show the player where the ball will go.

I found this page on how to do this with Box2D: http://www.iforce2d.net/b2dtut/projected-trajectory

This page goes into a lot of detail on how to do more complex things, but I want the simplest scenario, a projected path ignoring bounces and any dynamic objects, that simply shows the player where the ball would go if there was nothing blocking it. The page gives this code to achieve that:

Get trajectory:
Code:
b2Vec2 getTrajectoryPoint( b2Vec2& startingPosition, b2Vec2& startingVelocity, float n )
  {
      //velocity and gravity are given per second but we want time step values here
      float t = 1 / 60.0f; // seconds per time step (at 60fps)
      b2Vec2 stepVelocity = t * startingVelocity; // m/s
      b2Vec2 stepGravity = t * t * m_world->GetGravity(); // m/s/s
 
      return startingPosition + n * stepVelocity + 0.5f * (n*n+n) * stepGravity;
  }
Draw trajectory:
Code:
  glColor3f(1,1,0);
  glBegin(GL_LINES);
  for (int i = 0; i < 180; i++) { // three seconds at 60fps
      b2Vec2 trajectoryPosition = getTrajectoryPoint( startingPosition, startingVelocity, i );
      glVertex2f(trajectoryPosition.x, trajectoryPosition.y );
  }
  glEnd();
Unfortunately I'm a novice coder, and I have no idea how to go about converting this code into GML. Would anybody be able to help me, or point me in the right direction? Thanks for reading!
 

Bingdom

Googledom
For the drawing bit, you will want to look at primitives. That would be the easy bit.

For the main script:

scr_getTrajectoryPoint(startX, startY, startvelX, startvelY, gravity, time (point on line))
Code:
///scr_getTrajectoryPoint(startX, startY, startvelX, startvelY, gravity, time (point on line))
var startX = argument0, startY = argument1, startvelX = argument2, startvelY = argument3, grav = argument4, pol = argument5;
//velocity and gravity are given per second but we want time step values here

//float t = 1 / 60.0f; // seconds per time step (at 60fps)
var t = 1/room_speed;

//b2Vec2 stepVelocity = t * startingVelocity; // m/s
startvelX *= t;
startvelY *= t;

//b2Vec2 stepGravity = t * t * m_world->GetGravity(); // m/s/s
var stepGravityY = sqr(t) * grav;

//return startingPosition + n * stepVelocity + 0.5f * (n*n+n) * stepGravity;
var pos;
pos[0] = startX + pol * startvelX
pos[1] = startY + pol * startvelY + 0.5 * (sqr(pol)+pol) * stepGravityY;
return pos;
By the looks of the original code, it works for all directions of gravity. For this setup though, it would only work on the y axis. A small modification would be required if you wanted gravity along the x-axis.

This script will return an array. The first index returns the x-coordinate and the second index is the y-coordinate.

I'm not exactly familiar with GM's physics engine. It may behave a bit differently than what this script will return (but I doubt it).
 
Top