Jump to content
  • Advertisement
Sign in to follow this  
acematti

Stumped with vectors

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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)
now just add this vector to your pos

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!