Objective
- To move the bullet with projectile motion to specific destination by touch point.
In the below image is an example of a bullet which has a projectile motion. On my observation, the bullet speed and angle is calculated base on distance.
(source: pixwerk.net)
Where I'm at? (How do I move the bullet to its destination?)
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();
}
What I have done? (Currently I could move the bullet straight to its destination.)
- Bullet is moving straight (It's not problem).
[EDIT]
Drawing a projected trajectory
To get the trajectory point, base on iforced example. I've already have the libgdx box2d version of this.
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;
}
Rendering the trajectory point
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();
[Solve]
The answer is to find the angle and speed by the distance by s.tarik.cetin
Another issue encountered (this must be move to another question)
This is the problem, I don't know how do I set the starting velocity. Currently in the below code, to set the startingVelocity
and startingPosition
where the underlined code is the part that I'm not sure.
touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPoint);
Vector2 touch = new Vector2(touchPoint.x, touchPoint.y);
Vector2 touchDirection = touch.cpy().sub(bulletPosition).nor(); // get the the direction from bullet position to touch point and normalize the value.
float distance = touchDirection.dst(startingPosition);
float angle = touch.angle();
float speed = 6.0f; // currently the speed is fixed. I need to know the speed base on distance
startingPosition.set(bulletPosition); // current position of the bullet
startingVelocity.set(touchDirection.scl(speed)); // where the bullet should be