Skip to content

Instantly share code, notes, and snippets.

@andikan
Last active August 12, 2016 07:40
Show Gist options
  • Save andikan/d8b24ddf5553b5e8ef9605fedfe6e9fa to your computer and use it in GitHub Desktop.
Save andikan/d8b24ddf5553b5e8ef9605fedfe6e9fa to your computer and use it in GitHub Desktop.
function line(x1, y1, x2, y2) {
game.debug.context.beginPath();
game.debug.context.moveTo(x1, y1);
game.debug.context.lineTo(x2, y2);
game.debug.context.stroke();
}
function getTrajectoryPoint(startX, startY, velocityX, velocityY, n) {
//velocity and gravity are given per second but we want time step values here
var t = 1 / 60.0; // seconds per time step (at 60fps)
var stepVelocityX = t * game.physics.box2d.pxm( -velocityX ); // m/s
var stepVelocityY = t * game.physics.box2d.pxm( -velocityY );
var stepGravityX = t * t * game.physics.box2d.pxm( -game.physics.box2d.gravity.x ); // m/s/s
var stepGravityY = t * t * game.physics.box2d.pxm( -game.physics.box2d.gravity.y );
startX = game.physics.box2d.pxm(-startX);
startY = game.physics.box2d.pxm(-startY);
var tpx = startX + n * stepVelocityX + 0.5 * (n*n+n) * stepGravityX;
var tpy = startY + n * stepVelocityY + 0.5 * (n*n+n) * stepGravityY;
tpx = game.physics.box2d.mpx(-tpx);
tpy = game.physics.box2d.mpx(-tpy);
return { x: tpx, y: tpy };
}
function render() {
var launchVelocity = getLaunchVelocity();
game.debug.start();
var lastPos = null;
for (var i = 0; i < 180; i += 6)
{
var trajectoryPoint = getTrajectoryPoint(launchX, launchY, launchVelocity.x, launchVelocity.y, i);
if (lastPos && i % 12 == 0)
{
line(lastPos.x, lastPos.y, trajectoryPoint.x, trajectoryPoint.y);
}
lastPos = trajectoryPoint;
}
game.debug.stop();
//
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment