Jump to content
  • Advertisement
Sign in to follow this  

Need Help to move (Solved)

This topic is 4453 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

I want to add some physics to my car, at the moment i am just moving it by its forwardVector by a constant move_rate (#define MOVE_RATE 0.5f). This is my 1st time trying to add any physics to an object and i most learn by example, so if anyone could help me set up my movement functions for the car properly i can then play around and add friction to slow the car down. I know that: ======================================================= my forwardVector is my Velocity; Velocity = Velocity + accelerationVector * time; positionVector = positionVector + Velocity; acceleration = (currentVelocity - lastVelocity) / (currentTime - lastTime); ======================================================== Car.cpp
#define MOVE_RATE 0.5f

	//magnitude = 0.0f;
	ForwardVector.x = MOVE_RATE;
	turningRate = 2.0f;
	accelerationVector.x = 0.0f;
	accelerationVector.y = 0.0f;
	accelerationVector.z = 0.0f;



bool CCar::create(LPDIRECT3DDEVICE9 device)
	positionVector.x = 0.0f;
	positionVector.y = 0.0f;
	positionVector.z = 0.0f;

	ForwardVector.x = 0.0f;
	ForwardVector.y = 0.0f;
	ForwardVector.z = 1.0f;

	Model = new CModel();
	return Model->loadModel(device, "./models/car.x");

void CCar::setPosition(D3DXVECTOR3 newPosition)
	positionVector = newPosition;
void CCar::move(int direction)
	switch (direction)
	case FORWARD:
		ForwardVector.x = -MOVE_RATE;
		positionVector.x -= ForwardVector.x * -sin(D3DXToRadian(ForwardVector.y));
		positionVector.z += ForwardVector.x * cos(D3DXToRadian(ForwardVector.y));

		ForwardVector.x = MOVE_RATE;
		positionVector.x -= ForwardVector.x * -sin(D3DXToRadian(ForwardVector.y));
		positionVector.z += ForwardVector.x * cos(D3DXToRadian(ForwardVector.y));

	case LEFT:
		ForwardVector.y -= turningRate;

	case RIGHT:
		ForwardVector.y +=turningRate;

void CCar::update()
	/*Calculate the cars velocity by its magnitude (velocity = length of its vectors)
	// magnitude = (sqrt)((ForwardVector.x * ForwardVector.x) + (ForwardVector.y * ForwardVector.y) + (ForwardVector.z * ForwardVector.z));
	// D3DXVec3Normalize(&normVelocity,&ForwardVector);
	// ForwardVector = ForwardVector + accelerationVector * time;
	// positionVector = positionVector + ForwardVector;
	// acceleration = (currentVelocity - lastVelocity) / (currentTime - lastTime);

void CCar::render(LPDIRECT3DDEVICE9 device)
	D3DXMATRIX transMatrix;				// the translation matrix
	D3DXMATRIX rotMatrix;				// the rotation matrix
	D3DXMATRIX scaleMatrix;				// the scale matrix

	// create the translation matrix
	D3DXMatrixTranslation(&transMatrix, positionVector.x, positionVector.y, positionVector.z);

	// create the rotation matrix for the object

	// Scale the Car by the size amount
	D3DXMatrixScaling(&scaleMatrix, size, size, size);

	// Multiply the translation matrix by the rotation matrix
	// The resulting matrix is stored in the transMatrix
	D3DXMatrixMultiply(&transMatrix, &rotMatrix, &transMatrix);

	// Multiply the translation matrix by the scale
	D3DXMatrixMultiply(&transMatrix, &scaleMatrix, &transMatrix);

	// Transform the object into world space
	device->SetTransform(D3DTS_WORLD, &transMatrix);
	// render the model for this car
void CCar::setSize(float carSize)
	size = carSize;

class CModel;

class CCar : CGameObject

	void setSize(float carSize);

	// enumeration controlling the direction of the car
	enum { FORWARD=0, BACKWARD=1, LEFT=2, RIGHT=3 };

	// Overridden methods from the parent class
	bool create(LPDIRECT3DDEVICE9 device);
	void render(LPDIRECT3DDEVICE9 device);

	void setPosition(D3DXVECTOR3 newPosition);
	void move(int direction);
	void update();
	void create_Bounding_Box(LPDIRECT3DDEVICE9 device);

	// The current position of the car
	D3DXVECTOR3 positionVector;
	// The car's forward vector, what direction it is facing
	D3DXVECTOR3 ForwardVector;
	// The current acceleration of the car
	D3DXVECTOR3 accelerationVector;
	D3DXVECTOR3 normVelocity;

	float magnitude;
	float currentVelocity;
	float lastVelocity;
	float currentTime;
	float lastTime;

	CModel *Model;
	float	turningRate;

	// The size of the car
	float size;

	// The movement rate of the car
	float  moveRate;

[Edited by - Prog101 on November 10, 2006 4:00:21 AM]

Share this post

Link to post
Share on other sites
now what sort of physics do you want? have you tried rotation?

usually, for the sake of simplicity, i have a position vector, and velocity vector


then in the onFrame, step, or whatever you want to call it:

pos += vel;

for simple friction, i use a quick damping method in the step as well.




You can initialize vel to be the driection you want like:




I'm not sure if this is too baisc or inacurate for what you want?

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!