Skip to content
Advertisement

Java – Asteroids – acceleration in two dimensions

I am currently writing Asteroids for extra credit in my computer science class, following a very loose guide that provided some starter code. I have a ship and I can use key presses to change its position. The starter code provided a method of drawing the ship based on a Point position. I was then given this

public void accelerate(double acceleration) {
    pull.x += (acceleration * Math.cos(Math.toRadians(rotation)));
    pull.y += (acceleration * Math.cos(Math.toRadians(rotation)));
}

How am I supposed to implement this with position? I can increase both position.x and position.y, but how exactly am I supposed to use this method? Also, can I implement acceleration for turning? There is a variable rotation that is supposed to go from 0 to 360.

Advertisement

Answer

I’m not exactly sure how to answer this without seeing more of your code, but make sure that you understand how position, velocity, and acceleration are related. Acceleration, over time t, equals the derivative of velocity (dv/dt). Velocity equals the derivative of position (dx/dt). It also works the other way around with integration. Acceleration and Velocity are vectors, meaning they have magnitude and direction. You need to change the position based on the velocity, and the velocity based on the acceleration. Does this help at all?

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement