Sign in to follow this  

object movement

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

first off, i'm a bit new to directx. i'm trying to figure out how to make objects move, say, in the direction of a vector. my ultimate goal is to have two objects travel toward each other and collide. any point in the right direction will be appreciated. cheers

Share this post


Link to post
Share on other sites
A common solution would be to store a matrix (D3DXMATRIX) for each object. To move the object (translate), you would call D3DXMatrixTranslation(&matObject, 1.0f, 0.0f, 0.0f) to move it 1.0f along the direction of the X-Axis.
If you want to rotate your object, you would call D3DXMatrixRotation*() and multiply this matrix with your translation matrix.

Finally, you need to call device->SetTransform(D3DTS_WORLD, &matObject) to "move" your object (actually, just the matrix is being moved).

Collision could be done via a bounding box check.

Share this post


Link to post
Share on other sites
ok ... i'm using D3DXMatrixTranslation to set the initial position of my objects. i'm not sure how i would use it to make the objects move. i tried a second call but that didn't work.

Share this post


Link to post
Share on other sites
D3DXMATRIX matObject;
D3DXMatrixTranslation(1.0f, 0.0f, 0.0f);
pDevice->SetTransform(D3DTS_WORLD, &matObject);

DrawSpehere1();

D3DXMatrixTranslation(-1.0f, 0.0f, 0.0f);
pDevice->SetTransform(D3DTS_WORLD, &matObject);

DrawSpehere2();


This would move your first sphere 1.0f to the right and your second spehere 1.0f to the left


Doing the collision check is extremely simple eith two spheres - you just check whether the distance of v[sphere1] and v[sphere2] is lower than radius[sphere1] + radius[sphere2]

Share this post


Link to post
Share on other sites
i guess i'm missing something, because thats what i'm doing and it just puts the object at that position - i want to have a starting point and an endpoint for the movement (i guess). it should travel along this path until it hits something. here's what my code looks like:



LPD3DXMESH Sphere1 = NULL;
D3DXMATRIX trans;


void SetTranslation(float x, float y, float z)
{
D3DXMatrixTranslation(&trans, x, y, z);
}


void RenderScence()
{

.
.
.

SetTranslation(5.0f, 0.0f, 0.0f);
D3D_Device->SetTransform(D3DTS_WORLD, &trans);
Sphere1->DrawSubset(0);

}


btw, how do you post code samples?

Share this post


Link to post
Share on other sites
When you set the world transform of an object, it is with respect to the world origin of 0,0,0...not with respect to the previous frame. So translating the object 1 unit in the +x direction does not translate it from 1 to 2, then in the next frame 2 to 3, then in the next frame 3 to 4. You have to change the translation variable yourself each frame to move the object in an animation style.

For instance:

static float theta = 0;

theta += D3DX_PI/180;

if (theta == D3DX_PI*2)
theta = 0;

D3DXMATRIX Matrix;
D3DXMatrixRotationY(&Matrix, theta);

D3D_Device->SetTransform(D3DTS_WORLD, &Matrix);

As you can see, theta will continue to be updated each frame with a new value.

Share this post


Link to post
Share on other sites
D3DXVECTOR3 ball1Pos(-10, 0, 10), ball1Velocity(0.707, 0, -0.707);
D3DXVECTOR3 ball2Pos(10, 0, -10), ball2Velocity(-0.707, 0, 0.707);

void Render()
{
ball1Pos += ball1Velocity;
ball2Pos += ball2Velocity;

//Clear and call BeginScene

D3DXMATRIX mat;
D3DXMatrixTranslation(&mat, ball1Pos.x, ball1Pos.y, ball1Pos.z);
D3D_Device->SetTransform(D3DTS_WORLD, &mat);
//Draw Ball 1 here

D3DXMATRIX mat;
D3DXMatrixTranslation(&mat, ball2Pos.x, ball2Pos.y, ball2Pos.z);
D3D_Device->SetTransform(D3DTS_WORLD, &mat);
//Draw Ball 2 here

//Call EndScene and Present
}


voila. To juice it up try to add time based code using timeGetTime() or some equivilent. There are a billion tutorials around on that or I can explain it for you later if you're interested.

[Edited by - Coder on August 29, 2004 4:04:48 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by rom7L
btw, how do you post code samples?

You surround your code with [code][/code] tags if the code is small, or with [source][/source] tags if it's not. Source tags give you syntax-highlighted code, while code tags just give monospaced text.

For more info on formatting, please check GDNet Forums FAQ. And then, I recommend reading the Forum FAQ which answers a lot of common DirectX questions.

Share this post


Link to post
Share on other sites

This topic is 4853 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.

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