🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Max Speed configuration

Started by
5 comments, last by jtecin 24 years, 6 months ago
Thats a wierd way of spelling velocity....

Advertisement
I know now I spelled it wrong, but that still doesn't answer my question ...

Actually, the sin and cos functions are probably more likely to slow it down than a few multiplications, but overall if this is only done once every game loop it should not make a lot of difference. There's a few optimisations you could make to your function no doubt, and you could use a lookup table for sin and cos if you wanted to speed that up. You could also precalculate 10 * 3.14159 / 180, if your compiler doesn't automatically do this. Post a reply if you need any help with these concepts.

Anyhow, the way I usually think about the speed of things is by comparing them to a software blitter for an image. In this case I suspect that the time it takes for your ship to be drawn onscreen is a lot longer than for this little calculation - though there's still room here for improvement.

Good luck

Starfall

Okay, thanks. I am going to precalculate 10*3.14159/180. I'll try the look-up for sin and cos, but I'm still a little unsure on how to do this. Just hold the different values in an array and use the one based on Player.direction?
Don't bother, the compiler already precalculated that for you.

Sin and cosine lookup will help the most, try to minimize the size of the table so it caches better (like store only 90 degrees of the table, which has all the values you need.)

But don't go crazy on this yet, just keep it simple for now, and then profile later to see if this is an area taking a huge chunk out of performance (and I'm guessing it probably won't be.)

Okay, I am making a 4-way scroller where you are in space. For the most part the physics and stuff are fairly accurate. Anyway, I want to make the ship have a max speed that it can't go over. You don't want to put a limit on either the x or y volocity alone because they will go faster diagnolly than they should. Here is what I have right now:

if(Player.x_vol*Player.x_vol+Player.y_vol*Player.y_vol < Player.Max_Speed*Player.Max_Speed)
{
Player.x_vol += ((float)sin((Player.Direction-1)*10 * 3.14159/180)*Player.Acceleration);
Player.y_vol -= ((float)cos((Player.Direction-1)*10 * 3.14159/180)*Player.Acceleration);
}

There are two problems. First of all, they can go slightly over the max speed. However, I am not really worried about that. Second, every time they hold down the up key it has to use multiplication. Will this reduce the speed on slower machines significantly? If so, what is another alternative? Thanks.

Surprisingly enough, not all compilers do precalculate those things, which is why I suggested it.

Regards

Starfall

This topic is closed to new replies.

Advertisement