• Advertisement
Sign in to follow this  

Spaceship movement parameters update

This topic is 2073 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello!

I'm making a pretty basic spaceship game. Every time I update or change a main parameter, that affects others(acceleration, acceleration change speed, direction change speed) then I want to recalculate all parameters.

I have the following parameters at the beginning of the movement:


long currentTime = System.currentTimeMillis();
double timeDelta = (currentTime - lastUpdate) / 1000.0;
lastUpdate = currentTime;
double posX = params.get(Param.POS_X);
double posY = params.get(Param.POS_Y);
double accX = params.get(Param.SPEED_X_PIX_SEC); // ship acceleration x axis.
double accY = params.get(Param.SPEED_Y_PIX_SEC); // ship acceleration y axis.
double dir = params.get(Param.DIRECTION_RAD);
double dirSpeed = params.get(Param.DIRECTION_SPEED_RAD_SEC);
double thrust = params.get(Param.THRUST_PIX_SEC); // acceleration change speed. Depends on direction change.


And now I want to set the parameter values to what they should be after a certain amount of time has passed(timeDelta). Two parameters don't need updating since they are there only to affect other parameters - thrust and dirSpeed.
It would be a piece of cake to figure it out when there would not be any direction change...

Share this post


Link to post
Share on other sites
Advertisement


double accX = params.get(Param.SPEED_X_PIX_SEC); // ship acceleration x axis.
double accY = params.get(Param.SPEED_Y_PIX_SEC); // ship acceleration y axis.
double dir = params.get(Param.DIRECTION_RAD);
double dirSpeed = params.get(Param.DIRECTION_SPEED_RAD_SEC);
double thrust = params.get(Param.THRUST_PIX_SEC); // acceleration change speed. Depends on direction change.



I suspect you are confusing acceleration with velocity (speed) since you're assigning "speed" values to accX and accY?

Anyway:

pos2x = posX + (velX * timeDelta) + (accX * timeDelta * timeDelta) / 2;

vel2x = velX + (accX * timeDelta);

And dito for Y.

Note - you may want to treat thrust as a two-dimensional vector as well (as you do with pos and acc) and derive accX, accY from it via F = m * a.

Share this post


Link to post
Share on other sites
Yes, sorry about this, I meant velocity under "acceleration" and thrust is the same as acceleration.
I thought that I would calculate the acceleration of the thrust first(accelerating acceleration) in both axes:


double dirAtEnd = dir + dirSpeed * timeDelta;
double thrustAccX = thrust * (Math.sin(dirAtEnd) - Math.sin(dir)) / timeDelta;
double thrustAccY = thrust * (Math.cos(dirAtEnd) - Math.cos(dir)) / timeDelta;


And now I should be able to calculate the x and y position using the derivative of acceleration?

Share this post


Link to post
Share on other sites
Let me fix my last post:
I calculated the velocity of the thrust:


double dirAtEnd = dir + dirSpeed * timeDelta;
double thrustVelX = thrust * (Math.sin(dirAtEnd) - Math.sin(dir)) / timeDelta;
double thrustVelY = thrust * (Math.cos(dirAtEnd) - Math.cos(dir)) / timeDelta;

Share this post


Link to post
Share on other sites
Okay, I think I figured it out correctly:


private void updateParameters() {
long currentTime = System.currentTimeMillis();
double timeDelta = (currentTime - lastUpdate) / 1000.0;
lastUpdate = currentTime;
if(timeDelta == 0.0)
return;
double posX = params.get(ShipPhysicalParam.POS_X);
double posY = params.get(ShipPhysicalParam.POS_Y);
double velX = params.get(ShipPhysicalParam.SPEED_X_PIX_SEC);
double velY = params.get(ShipPhysicalParam.SPEED_Y_PIX_SEC);
double dir = params.get(ShipPhysicalParam.DIRECTION_RAD);
double dirSpeed = params.get(ShipPhysicalParam.DIRECTION_SPEED_RAD_SEC);
double thrust = params.get(ShipPhysicalParam.THRUST_PIX_SEC);
double dirAtEnd = dir + dirSpeed * timeDelta;
double thrustX = thrust * Math.sin(dir);
double thrustY = thrust * Math.cos(dir);
double thrustAtEndX = thrust * Math.sin(dirAtEnd);
double thrustAtEndY = thrust * Math.cos(dirAtEnd);
double thrustVelX = (thrustAtEndX - thrustX) / timeDelta;
double thrustVelY = (thrustAtEndY - thrustY) / timeDelta;
posX += velX * timeDelta + thrustX * timeDelta * timeDelta / 2.0 +
thrustVelX * timeDelta * timeDelta * timeDelta / 6.0;
posY += velY * timeDelta + thrustY * timeDelta * timeDelta / 2.0 +
thrustVelY * timeDelta * timeDelta * timeDelta / 6.0;
velX += thrustX * timeDelta + thrustVelX * timeDelta * timeDelta / 2.0;
velY += thrustY * timeDelta + thrustVelY * timeDelta * timeDelta / 2.0;
params.put(ShipPhysicalParam.POS_X, posX);
params.put(ShipPhysicalParam.POS_Y, posY);
params.put(ShipPhysicalParam.SPEED_X_PIX_SEC, velX);
params.put(ShipPhysicalParam.SPEED_Y_PIX_SEC, velY);
params.put(ShipPhysicalParam.DIRECTION_RAD, dirAtEnd);
}


Tested and seems to work pretty nicely.
Problem solved!

Share this post


Link to post
Share on other sites
I'm curious to know the reasoning behind the use of factors like timeDelta^2, timeDelta^3, 1/2 and 1/6:

posX += velX * timeDelta + thrustX * timeDelta * timeDelta / 2.0 +
thrustVelX * timeDelta * timeDelta * timeDelta / 6.0;
posY += velY * timeDelta + thrustY * timeDelta * timeDelta / 2.0 +
thrustVelY * timeDelta * timeDelta * timeDelta / 6.0;

velX += thrustX * timeDelta + thrustVelX * timeDelta * timeDelta / 2.0;
velY += thrustY * timeDelta + thrustVelY * timeDelta * timeDelta / 2.0;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement