Showing trajectory indicator
Im drawing a ball trajectory using an equation from a SO question, this is
modified to take into consideration the box2d steps of 30 frames per
second. This does calculate a valid trajectory but it does not match the
actual trajectory of the ball, the ball has a smaller trajectory. I am
applying a box2d force to the ball, this also has a density set and a
shape. The shape radius varies depending on the type of ball. Im setting
the start velocity in the touchdown event.
public class ProjectileEquation {
public float gravity;
public Vector2 startVelocity = new Vector2();
public Vector2 startPoint = new Vector2();
public Vector2 gravityVec = new Vector2(0,-10f);
public float getX(float n) {
return startVelocity.x * (n * 1/30f) + startPoint.x;
}
public float getY(float n) {
float t = 1/30f * n;
return 0.5f * gravity * t * t + startVelocity.y * t + startPoint.y;
}
}
@Override
public void draw(SpriteBatch batch, float parentAlpha) {
float t = 0f;
float width = this.getWidth();
float height = this.getHeight();
float timeSeparation = this.timeSeparation;
for (int i = 0; i < trajectoryPointCount; i+=timeSeparation) {
//projectileEquation.getTrajectoryPoint(this.getX(),
this.getY(), i);
float x = this.getX() + projectileEquation.getX(i);
float y = this.getY() + projectileEquation.getY(i);
batch.setColor(this.getColor());
if(trajectorySprite != null) batch.draw(trajectorySprite, x,
y, width, height);
// t += timeSeparation;
}
}
public boolean touchDown (InputEvent event, float x, float y, int pointer,
int button) {
if(button==1 || world.showingDialog)return false;
touchPos.set(x, y);
float angle = touchPos.sub(playerCannon.position).angle();
if(angle > 270 ) {
angle = 0;
}
else if(angle >70) {
angle = 70;
}
playerCannon.setAngle(angle);
world.trajPath.controller.angle = angle;
float radians = (float) angle * MathUtils.degreesToRadians;
float ballSpeed =
touchPos.sub(playerCannon.position).len()*12;
world.trajPath.projectileEquation.startVelocity.x =
(float) (Math.cos(radians) * ballSpeed);
world.trajPath.projectileEquation.startVelocity.y =
(float) (Math.sin(radians) * ballSpeed);
return true;
}
No comments:
Post a Comment