# Stumped with vectors

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

## Recommended Posts

Hi, Im still working on my 2D top down golf game. I have been trying for ages to try and get the ball to move in a decent manor. I did with some success get the ball to move in the direction that the aiming line behind the ball was aiming. However it was not always exact and did sometime go in a straight line when it should not have been so. I have created a class that can store a vector in it and this is to be used as a velocity vector. Something that i add onto the x and y co-ords of the ball. (I read article 1019 i think for help on that). The trouble is with the game I want to have a maximum amount of power that the ball can be hit so presumably this either need to limit the length of the line or limit the x and y of the velocity vector (I think). Both of which i have not had good results implementing. Does anybody know what would be a good way to implement this? I am so close with this game its. I have the collision detection working for different parts of the hole etc(Still a little ropey mind, (Think its due to how fast the ball is travelling that it misses the actual walls on the hardness map, but i can tinker with that in a bit)) The main problem that I have is with the calculation of the vector to move the ball. I have looked on the net but nothing except that article was useful really and the forum search is currently down.(I bet this has been asked loads before) If someone could shine a light on my query be it a link or a piece of their wisdom it would be greatly appreciated. Cheers in advance matt

##### Share on other sites
Quick home-for-lunch answer. You don't want to limit the x and y components of the velocity vector independently, but rather (as you said) limit the length of the vector itself. The easiest way to do this is simply to normalize the vector, and then multiply it by the desired length. With a little math, you can do it a little more efficiently than that, though.

The Doom 3 math library (available with the sdk) has a 'truncate' function in the vector class, if you'd like to see an implementation.

##### Share on other sites
vector approach:
speed: float/int whatever
direction: normalised vector

calculating new position vector just add speed * direction

polar coord approarch:
speed: float/int whatever
direction: is an angle (relative to x-as)

now you have actually to construct a vector (speed*cos(angle), -speed*(sin)),
something like that (relative to your axiss, understanding of where 0 degrees is on your axises)

##### Share on other sites
Cheers guys i'll look into that now.

Thanks

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
JoeJ
18

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632998
• Total Posts
3009805
• ### Who's Online (See full list)

There are no registered users currently online

×