Tracking coordinates

Started by
5 comments, last by TehProgNub 15 years, 3 months ago
Hey guys, I have been learning Direct3D for about a month now and I have begun to try collision detection; however I have a small problem...lol What I am doing at the moment, is rotating and translating two sphere meshes (standard in directx) on each end of a cylinder (also standard). When translation occurs, the spheres and cylinder move "as one", and upon rotation, the spheres move around the outside of the cylinder (staying on the ends). I have made a class to keep track of the coordinates, and a class to hold each object (I am able to make more by clicking a sprite that I have made). I store each of the objects in a vector. Anyway, my problem is that I am not able to store the positions of the spheres properly. When I test for collision by calling their coordinates, the detection is made in the center of each cylinder (so that a collision is detected when they cross like a lower case 't', not when the spheres touch each other). This has something to do with the fact that so far I have not been able to find a function that can apply my transformation matrices on the coordinates of my spheres. They are able to be drawn in their correct positions on the screen with the SetTransform() function (and applying the transformation matrices), but what could I use to apply such transformations AND store the coordinates (as I am not able to store the result of SetTransform()). I have tried using D3DXVec3TransformCoord and D3DXVec3TransformNormal, but they dont seem to be working properly. All help is greatly appreciated. Thanks :D
Advertisement
If you have a world matrix (transformation matrix) for a specifc object, and the the object's vertices are centered at <0,0,0>, then you can extract the translation component of the matrix by getting the values of the _41, _42, and _43 elements of the matrix (_41 is x, _42 is y, _43 is z). This would be the same as transforming the vector <0,0,0> by the world matrix using D3DXVec3TransformCoord (D3DXVec3TransformNormal only applies the rotation portion of the matrix and not the translation, since it sets to w = 0 before transforming).
Thanks for the reply.

Seeing as im new to this, Im probably asking a stupid question, but when you say "the objects vertices are centered at <0,0,0,>" do you mean that I rotate them when they are at the origin, and then translate them? If so, I dont. I have to rotate one sphere from the position <0,0,3.5> and the other from <0,0,-3.5>, so that they are rotated on the end of the cylinder.

My code is: (with "cylinders" being the name of the class storing cylinder coordinates)
D3DXMatrixScaling( &tubehighlight, 1.5f, 1.5f, 1.5f ); // scales the selected "tube"
D3DXMatrixTranslation( &sphereTranslate, 0.0f, 0.0f, 3.5f ); // translate one sphere ready for rotation
D3DXMatrixRotationX( &sphereMatX, D3DXToRadian( cylinders->getS1RotationX() ) ); //rotation
D3DXMatrixRotationY( &sphereMatY, D3DXToRadian( cylinders->getS1RotationY() ) ); //rotation
D3DXMatrixTranslation( &sphereTranslate3, cylinders->getCoordinateX(), cylinders->getCoordinateY(), 0); // translate it to the coordinates of the cylinder
d3ddev->SetTransform( D3DTS_WORLD, &( sphereTranslate*
tubehighlight *
sphereMatX *
sphereMatY *
sphereTranslate3 ) ); // apply transformations
sphere1->DrawSubset(0); //draw the sphere

I hope that helps.
Ok, maybe I am going about asking this the wrong way.

Are you able to translate AND rotate using either D3DXVec3TransformCoord or D3DXVec3TransformNormal? Because I need a function that can translate AND rotate my sphere AND allow me to store the coordinates that the matrix multiplication produces.

Am I trying to use the wrong functions?

Thank you.
Here you just need to keep a copy of the final product of your matrix multiplications, since that is the world matrix for your object. So in the case of your one sphere:

D3DXMATRIX worldMatrix = sphereTranslate*tubehighlight *sphereMatX *sphereMatY *sphereTranslate3;d3ddev->SetTransform(D3DTS_WORLD, &worldMatrix);


Then to get the position of the sphere...
D3DXVECTOR3 spherePosition;spherePosition.x = worldMatrix._41;spherePosition.y = worldMatrix._42;spherePosition.z = worldMatrix._43;


Or alternatively...
D3DXVECTOR3 sphereCenter = D3DXVECTOR3(0, 0, 0);D3DXVECTOR3 spherePosition;D3DXVec3TransformCoord(&spherePosition, &sphereCenter, &worldMatrix);
Thanks for that :)

However, this code has the same problem as the other that I tried to use, in that when I test for collision on the X axis, if I have rotated my spheres around the Y axis, no detection is made, however, should I rotate them back to their original position it will detect a collision (this position is where the two spheres are overlapping. Their position in regards to each other should not matter, right?). The strange thing is that I can rotate my objects around the X axis and a collision is still detected ( in this case the two spheres are both in line with the x coordinate being tested, with different y values ). The inverse also occurs when testing for y axis collision.

How could I rectify this issue?

Thanks for your persistence

EDIT: I take it that there is something wrong with my rotation matrix/rotation method. for the rotation i use an infinitely increasing/decreasing number and set it for my sphere:

Pipe* pipe = collection.getCylinderID( CylinderID ); //get the ID of the objects being altered
rotateY = pipe->getRotationY() + 4; // get previous rotation and add 4
pipe->setRotationY( rotateY ); // set rotation for cylinder
pipe->setS1RotationY( rotateY ); // set rotation for sphere1
pipe->setS2RotationY( rotateY ); // set rotation for sphere2

[Edited by - TehProgNub on January 18, 2009 9:04:11 PM]
God, I am so stupid!...

When I was returning the sphere coordinates for comparison, I was returning floats... This was really stupid of me because when I rotate the spheres, they could have ANY number after the decimal point. To rectify my problem, I simply changed my function to return integer values and it works fine!!

Thank you sooo much for your help!!

This topic is closed to new replies.

Advertisement