Question

Basically, I am curious if xVel and yVel variables would work the best, or if I should use a velocity and a direction variable.

Also, this is how it would be handled on update: (psuedocode) For xVel and yVel :

x += xVel;
y += yVel;

For velocity and direction :

x += velocity * (cos(direction));
y += velocity * (sin(direction));

Note: I am not sure if the second one will work properly. I do not have trigonometry skills.

So, is the first way more effective, or the second? And am I doing the second one completely wrong?

Was it helpful?

Solution

It depends on your application. You can use an arctangent function to find the theta from any given moment, of course. However, if your have graphics which will rotate with the angle of the object, then you may want to stick to polar.

Basically, the rule of thumb I use is this: If you're dealing with X and Y accelerations as well as velocities, then you want to stick to rectangular coordinates and use delta-x and delta-y variables. If you want to handle object rotation, and the angle is computed upon regularly, then you may want to use polar.

It depends on the application.

OTHER TIPS

Both ways are equally effective, since they should give the same answer. However I would add an extra numerical-time-step term to make it a better numerical "simulation":

x += xVel * timeStep;
y += yVel * timeStep;

Or

x += totalVel * cos(direction) * timeStep;
y += totalVel * sin(direction) * timeStep;

Both ways would give the same results, however the calculation time will probably diverge. If the amount of calculations for one method would be much more than the other it will be very likely that the other one will be faster. But if you are unsure you could always run a test script in which you test each method quite a few times and track the amount of time the CPU needed to perform them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top