Skip to main content
gamedev.net
🔒 Locked

Implementing 2d Vectors?

Started by FlorianapoliS Jul 22, 2002 at 5:40 AM 5 replies 600+ views
Original Post
FlorianapoliS
FlorianapoliS
Hey, I''m trying to make a 2d top down car game (GTA Style). I currently have my car moving, and was looking for some car physics, and all the tutorials required the car to move on vectors? The only code I use to move my car is:
Player.car.x -= Player.speed * sinlut[Player.ang];
Player.car.y += -Player.speed * coslut[Player.ang];
 
I was just wondering how I go about using vectors to move my car?
FlorianapoliS
FlorianapoliS
So would it be something along these lines?

    
struct vector
{
float x;
float y;
};

vector vel;

vel.x = Player.speed * sinlut[Player.ang];
vel.y = -Player.speed * coslut[Player.ang];

Player.car.x -= vel.x;
Player.car.y += vel.y;




[edited by - FlorianapoliS on July 22, 2002 8:21:08 AM]
siaspete
siaspete
Yes something like that.

If using C you'd write functions for manipulating vectors, or if using C++ you'd add some member functions (and optionally make the struct a class if you prefer).

You might as well store your car positions as vectors to make it more consistent.

Also you may want to use another struct name (like vector2d or whatever), as vector is part of the C++ standard library, and is a resizable array (not a vector like you are using now).


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions


[edited by - siaspete on July 22, 2002 8:53:31 AM]
Gee
Gee
Well, your still not really using vectors for the car physics... That would mean not even having an angle and speed variable. And when you - let''s say turn - you add a friction fource on the tire and etc that will make it rotate. Everything goes down to just adding fources (the fources in this case is vectors) to the car.

Read some vector tutorials and they''ll get familiar...
---GUI Programming Division Manager at Wildfire Gamesworking on the 0 A.D. project
FlorianapoliS
FlorianapoliS
I''ve read some vector tutorials, most of it I already new such as adding, subtracting and finding the dot point etc... I understand all the theory behind with vectors representing a certain force, and the direction in which it is acting, I just get stuck when it comes to implementing it in my game?

I just don''t understand how I''m going to make my car move purely on vectors?
siaspete
siaspete
Vectors are just a way of combining the x & y components of something and making it easier to operate on them.

If you understand the ideas of forces, mass, acceleration, you shouldn''t have any problems. You should know the mass of your car, you apply some force, and that gives you an acceleration.

You change the velocity of your car using the acceleration, the velocity changes the position on screen, and hey presto, a moving car!


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Topic Locked

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

Sign in to reply to this topic.