question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Predict projectile motion

See original GitHub issue

I am using Phaser3 to make an Angry Birds like game, where I throw a ball. I am not totally sure I need this feature yet in my game, but I was trying to find the prediction of the trajectory of the ball, after I set a velocity to it. I saw that the matterj.s code does first this on the body update: body.force.x += body.mass * gravity.x * gravityScale; and then this on the engine: body.velocity.y = (velocityPrevY * frictionAir * correction) + (body.force.y / body.mass) * deltaTimeSquared;

When the slingshot fires, and the drag ends, I do:

gameObject.setVelocity((gameObject.startPos.x - gameObject.x) * SPEED,
                       (gameObject.startPos.y - gameObject.y) * SPEED)

While dragging occurs, I try to predict the various positions, with my knowledge and memory of school-level physics.

...
gameObject.x = dragX;
gameObject.y = dragY;

const velocityX = (gameObject.startPos.x - dragX) * SPEED;
const velocityY = (gameObject.startPos.y - dragY) * SPEED;

let index = 1;
for (let trajectoryPoint of trajectoryPoints.getChildren()) {
  trajectoryPoint.x = gameObject.x + index * velocityX;
  trajectoryPoint.y = gameObject.y + index * velocityY + GRAVITY * index * (index + 1) / 2;
  index++;
}
...

with GRAVITY being the matter.js gravity, and gameObject being the ball. This gives me a nice trajectory, similar to what matter.js does, but not quite there… Any idea what I am missing?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
stathismorcommented, Jul 30, 2018

@liabru and for whoever else comes across this, I just figured it out! You need to also clear the forces, at the end of the update, as this is what Engine.update does, it calls _bodiesClearForces at the end. So, you need to do 3 main things:

  1. Update the force
  2. Run the body’s update
  3. Clear its forces
2reactions
liabrucommented, Mar 26, 2018

That doesn’t look correct to me, you’re missing the airFriction term for one thing?

I’d suggest the easiest way to do this is to take note of the projectile’s initial position and angle, then literally call Body.update(...) on it in a loop, taking a clone of its body.position vectors and putting them into an array, then restoring it back to the original position?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Projectile Motion
Part 2. Projectile Motion. The purpose of this experiment is to predict and verify the range and the time-of-flight of a projectile launched...
Read more >
X-Labs "The Future of Physics" - Projectile Motion
In this experiment, you will predict the horizontal range of a projectile using measurements of height. Materials meter stick; masking tape; inclined ramp; ......
Read more >
Projectile Motion Lab - Physics by B. Karpowicz
The purpose of this lab was to predict the range of a projectile and find the accuracy of our prediction. In order to...
Read more >
projectile
calculate the distance of an actual projectile and test prediction. Goals, apply knowledge about projectile motion, see physics laws actually work.
Read more >
Horizontally Launched Projectile Problems
Combining the two allows one to make predictions concerning the motion of a projectile. In a typical physics class, the predictive ability of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found