• Create Account

## Max Speed configuration

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1jtecin  Members

122
Like
Likes
Like

Posted 20 December 1999 - 11:52 AM

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.

### #2Mezz  Members

571
Like
Likes
Like

Posted 19 December 1999 - 08:32 AM

Thats a wierd way of spelling velocity....

### #3jtecin  Members

122
Like
Likes
Like

Posted 19 December 1999 - 11:59 AM

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

### #4Starfall  Members

122
Like
Likes
Like

Posted 19 December 1999 - 12:10 PM

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

### #5jtecin  Members

122
Like
Likes
Like

Posted 19 December 1999 - 12:18 PM

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?

### #6mhkrause  Members

122
Like
Likes
Like

Posted 19 December 1999 - 05:45 PM

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.)

### #7Starfall  Members

122
Like
Likes
Like

Posted 20 December 1999 - 11:52 AM

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

Regards

Starfall

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.