Apply a vector

Started by
3 comments, last by Fahrenheit451 18 years, 10 months ago
How can apply a vector to a geometric figure, his direction and velocity?
Advertisement
Can you clarify the question? Perhaps describe more specifically what you are trying to do, and what you have already tried.
I'm trying to write a routine for a topdown racing game. I'm in trouble
to start with the vehicle management. I don't want slips or any vehicle
physics, only fixed speed and simple turning. I'm tryin' to learn some
car physics on Monstrous site, but it's a little bit too hard :( .
It explains very good how a car physics works, but he says nothing
about applyin' in gameprogramming.
Tnx for the answer jyx
Maintain a couple vectors

D3DXVECTOR3 m_vPos, m_vVel, m_vAccel;

In your update function (called each frame), do the following:

void Update( float fDt ){ m_vVel += m_vAccel * fDt; m_vPos += m_vVel * fDt;}


So you can apply an acceleration to your car (or it can be 0,0,0 if you want constant velocity). Then your velocity will modify your position. The velocity is in units/sec so that's why you multiply it by the timestep for the current frame (fDt) -- same with the acceleration.

You could modify your velocity and/or acceleration vectors (along with the models direction vector) somehow to have it turn.

I'm not sure if that's what you were asking about but hopefully it helped?
Helix has it right. Similar to a particle system. In a particle system you initialise each particle with a start position, possibly random, say 0,0,0:

// start positionParticle->PartInfo.Pos.Set(0.0f,0.0f,0.0f);


You also setup a velocity vector - basically a direction, again it might be random:
// setup particle velocityParticle->PartInfo.Velocity.x = GetFloat3(0.5);  // slight x randomnessParticle->PartInfo.Velocity.y = 8.0f-sfRAND;     // slight y randomness on 8.0Particle->PartInfo.Velocity.z = GetFloat3(0.5);  // slight z randomness


Then in the update routine you would call each frame, you simply add to the postion the velocity of the particle. You can also apply a speed, acceleration, and a time value to that. Now we have something like:

Particle->PartInfo.Pos.x += Particle->PartInfo.Velocity.x * timeStep;Particle->PartInfo.Pos.y += Particle->PartInfo.Velocity.y * timeStep;if ( Particle->PartInfo.Pos.y < 0.0f ) {   float adjust = (rand()%1)/10.f;		// random height bounce   // adjust y direction with bounce power loss (*0.1+adjust)  Particle->PartInfo.Velocity.y = (-(Particle->PartInfo.Velocity.y))*(0.1f+adjust);  Particle->PartInfo.Pos.y += Particle->PartInfo.Velocity.y * timeStep;   // shorten its life considerably  Particle->PartInfo.Life /= 2;}Particle->PartInfo.Pos.z += Particle->PartInfo.Velocity.z * timeStep; 


The postion of the particle is updated by means of manipulating the velocity vector and other things like acceleration, speed, time etc. You can factor in many more things like friction, gravity and so on to make it more realistic. The basic idea is that you let those external forces dictate the position, and you only have to check the current position of the object for things like collision detection etc. The examples about are from my simple fountain simulation using a particle system. It's not completely proper physics because friction, gravity, etc are missing, but even with a few simply values and a velocity vector you can get a good effect. I highly recommend you apply some basic physics to your model/game. It will make it so much more interesting! Read up on particle systems.

hth
F451

[Edited by - Fahrenheit451 on June 19, 2005 10:56:36 PM]

This topic is closed to new replies.

Advertisement