Skip to main content
gamedev.net
🔒 Locked

Asteroids Movement

Started by toXic1337 1 reply 700+ views
Original Post
toXic1337
toXic1337
Hey everybody, I'm currently trying to make an asteroids clone and I'm at the moment trying to create some sort of movement system. I know its movement direction is based on the angle the ship is facing and then when pressing a "thrust" button, the game uses sin(radian) and cos(radian) to get the y and x velocity, respectively. Basically, I'm just looking for the common way of doing this. Here's my dilemma. My ship class's (x,y) values and even its velocity and acceleration variables are set up as integers and I want to keep it that way. BUT, when you take the cos(pi/4) you are going to get a decimal. And if I cast this to my integer velocities, it either converts to 1 or 0. (Because nothing on the unit circle is > 1. SO, I'm thinking... what should I do. I believe I could just multiply whatever I get from my trig calculations by a common value and then apply that value to the velocity but like I've said I'm looking for a common way of doing this. I'm kind of worried that between rotation, particle_systems, and these calculations, I may end up with a considerable performance decrease. I hope this doesn't sound confusing at all. Thanks in Advance for Advice, toXic1337 [EDIT] Not that I think it matters for this is mainly theory, but just in case Using SDL with Dev-C++
toXic1337
doynax
doynax
Yes, that should work. Pick a power-of-two scaling factor to get some performance out of it.
The technique itself is called fixed-point math.

Be sure to precalculate the sine/cosine tables if you're worried about the performance of using fixed-point integers. A single such call, especially on a machine without floating-point support (I assume that's why you want to avoid them), would outweight any integer operations you'll have to perform by an order of magnitude or two.
toXic1337
toXic1337
Thanks doynax,

[lol] It's good to know that without researching the topic I'm heading in a common direction :D.
toXic1337

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.