In the below code, is the method I use to move a bullet to its destination. But the problem is I want to create a cannonball bullet which it has a projectile motion. How do I create these tricks to make a cannon ball projectile motion like the image below.
public Vector2 getVelocity(Vector2 currentPosition, Vector2 targetPosition) { Vector2 targetDirection = targetPosition.cpy().sub(currentPosition); return targetDirection .nor(); } final float SPEED = 6; float pos = bulletPosition.len(); float des = bullet.destination.len(); float tol = Constants.TIME_STEP * SPEED; if(MathUtils.isEqual(pos,des, tol)) { body.setLinearVelocity(0,0); } else { body.setLinearVelocity(getVelocity(bulletPosition, destination).scl(SPEED)); } [EDIT]
To get the trajectory point, base on iforced example
public Vector2 getTrajectoryPoint(Vector2 startingPosition, Vector2 startingVelocity, float n) { Vector2 gravity = new Vector2(0, -9.8f); float t = 1 / 60.0f; // seconds per time step (at 60fps) Vector2 stepVelocity = startingVelocity.cpy().scl(t); // m/s Vector2 stepGravity = gravity.cpy().scl(t * t); // m/s/s Vector2 trajectoryPoint = new Vector2(); trajectoryPoint.x = (startingPosition.x + n * stepVelocity.x + 0.5f * (n * n + n) * stepGravity.x); trajectoryPoint.y = (startingPosition.y + n * stepVelocity.y + 0.5f * (n * n + n) * stepGravity.y); return trajectoryPoint; } 