Sign in to follow this  
Prog101

Need Help to move (Solved)

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

CCar::CCar(void)
{
	//magnitude = 0.0f;
	ForwardVector.x = MOVE_RATE;
	turningRate = 2.0f;
	accelerationVector.x = 0.0f;
	accelerationVector.y = 0.0f;
	accelerationVector.z = 0.0f;
}

CCar::~CCar(void)
{

}

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));
		break;

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

	case LEFT:
		ForwardVector.y -= turningRate;
		break;

	case RIGHT:
		ForwardVector.y +=turningRate;
		break;
	}
}

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
	D3DXMatrixRotationY(&rotMatrix,D3DXToRadian(ForwardVector.y));

	// 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
	Model->render(device);
}
void CCar::setSize(float carSize)
{
	size = carSize;
}





Car.h
class CModel;

class CCar : CGameObject
{
public:
	CCar(void);
	~CCar(void);

	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;

private:
	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

VECTOR pos;
VECTOR vel;

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.

vel*=0.95;

or

vel*=0.8

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

vel.x=1;
vel.z=0.3;
vel.y=0;

or

vel.x=cos(angle)*speed;
vel.z=sin(angle)*speed;
vel.y=0;

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this