Last active
August 12, 2016 07:40
-
-
Save andikan/d8b24ddf5553b5e8ef9605fedfe6e9fa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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