Jump to content

View more

Image of the Day

Boxes as reward for our ranking mode. ヾ(☆▽☆)
#indiedev #gamedev #gameart #screenshotsaturday https://t.co/ALF1InmM7K
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

Rotate object to face other object

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 aikender   Members   


Posted 22 January 2014 - 06:59 AM

hi i have some problems to correctly rotate a cube to the other.

i have one cube that i can move and i need the other cube to rotate in his direction  when its moving

when run program, the cube rotates some in the dircection  but when i go behind or under it changes direction.


i think it has somthing to do whit the direction of the cube ,or the angle or the rotation direction


this is the code dat i update:


translationform1 =XMVectorSet (xaxis1, 0.0f, zaxis1,0.0f);
    cube1transform = XMVectorSet(3.0f, 0.0f, 4.5f, 0.0f);
    cube2transform = XMVectorSet(-5.0f , 0.0f, 4.5f, 0.0f);
    directioncube = cube1transform - cube2transform;
    cubetranslation1 = D3DXVECTOR3(xaxis1, 0.0f, zaxis1);
    cubelocation1 = D3DXVECTOR3(3.0f, 0.0f, 4.5f);
    cube2position = D3DXVECTOR3(-5.0f, 0.0f, 4.5f);

    cubeposition = cube1transform + translationform1;
    cubepos1 = cubelocation1 + cubetranslation1;

    cubeoldpos = cubepos1 - cubetranslation1;


    direction2 = direction2;        // i also tried cubeoldposition-cube2position  "to get the new direction whit eache update"..
    direction1 = cubepos1 - cube2position;

    D3DXVec3Normalize(&direction2Norm, &direction2);
    D3DXVec3Normalize(&direction1Norm, &direction1);
    directioncubeNorm = XMVector3Normalize(directioncube);

    renderrotate = D3DXVec3Dot(&direction1Norm, &direction2Norm);
    dot = renderrotate;
    renderrotate = (acos(renderrotate));

    if (dot < 0)
        renderrotate *=-1;

    renderrotate *=1;

    rotation = XMMatrixRotationY(renderrotate);

    translation1 = XMMatrixTranslationFromVector(cubeposition);
    cubeworld1 = translation1;

    forwardcube = cube2transform + directioncubeNorm ;

    translation2 = XMMatrixTranslation(-5.0f,0.0f,4.5f);

    cubeworld2 = rotation*translation2;

#2 Álvaro   Members   


Posted 22 January 2014 - 07:22 AM

You have vectors called translation, transform, location and position, all of which sound like pretty much the same thing. Then there are variables that contain "cube1" in the name and variables that contain "cube2" in the name, but then there is "cubeposition", which I don't know which cube it's referring to. This makes your code very hard to follow. If we were dealing with working code, I could try to look at what you are computing and deduce what things mean from there, but with non-working code it's just hopeless.


Can you either use better names or provide some comments explaining what you are trying to do?

#3 aikender   Members   


Posted 22 January 2014 - 08:08 AM

ok i hope this explains som things




translationform1 =XMVectorSet (xaxis1, 0.0f, zaxis1,0.0f); //the amount that cube1 has to move increses or decreses when i use the arrow keys;
    cube1transform = XMVectorSet(3.0f, 0.0f, 4.5f, 0.0f);  //is the start postion of the first cub that i can move
    cube2transform = XMVectorSet(-5.0f , 0.0f, 4.5f, 0.0f);// is the start position of the second move and stays put
    cubetranslation1 = D3DXVECTOR3(xaxis1, 0.0f, zaxis1); //does the same as translationform1;
    cubelocation1 = D3DXVECTOR3(3.0f, 0.0f, 4.5f);//is again the start position of the first cube;
    cube2position = D3DXVECTOR3(-5.0f, 0.0f, 4.5f);// is again the start position of the second cube;
                                                      // i use these to calculate the dot product XMVector3Dot returns a vector and i need a float;
    cubeposition = cube1transform + translationform1; // calculates the new position of cube1 when cube 1 is moved

    cubepos1 = cubelocation1 + cubetranslation1;  // does same as cubeposition but whit the D3XVECTOR3

    cubeoldpos = cubepos1 - cubetranslation1; // caluclates the old postion of cube1;

    direction2 = direction2;     // direction2 = (0.0f,0.0f,1.0f)  i also tried here direction2= cubeoldpos -cube2position to calulate the direction that he is now facing;
    direction1 = cubepos1 - cube2position;

    D3DXVec3Normalize(&direction2Norm, &direction2);
    D3DXVec3Normalize(&direction1Norm, &direction1);    // normalize direction1 and direction2

    renderrotate = D3DXVec3Dot(&direction1Norm, &direction2Norm);// the dot porduct of direction1 and 2
    dot = renderrotate;
    renderrotate = (acos(renderrotate)); // calculate the acos of dot

    if (dot < 0)
        renderrotate *=-1;

    renderrotate *=1;            //this i us to get the direction to rotate  but dont know if this is corect

    rotation = XMMatrixRotationY(renderrotate);  //rotation to cube 1

    translation1 = XMMatrixTranslationFromVector(cubeposition);  // traslation of the first cube using the xmvextors;
    cubeworld1 = translation1;


    translation2 = XMMatrixTranslation(-5.0f,0.0f,4.5f);

    cubeworld2 = rotation*translation2;    //the rotation and translation of second cube;

#4 haegarr   Members   


Posted 22 January 2014 - 09:05 AM

1.) Why is nearly all declared or computed twice?


2.) Why are variable names chosen so that they are not distinguishable from others?


3.) Why are variable names for semantically the same thing so different as for cubepos1 and cube2position?


4.) Why the redundant computation here (2nd line)?

   cubepos1 = cubelocation1 + cubetranslation1

   cubeoldpos = cubepos1 - cubetranslation1


5.) The dot-product gives a negative result if the angle between the 2 arguments is greater than 90°. The acos gives values in range [0,pi]. This means that your computation makes no difference between rotation to the left and rotation to the right, and it has discontinuities at +90° and -90°.


In general: Clean up your code, drop duplicates, bring calculations in a meaningful order, choose meaningful names, choose similarly looking name schemes for semantically equal variables. This makes things easier to read for you and anyone you show the code, and it makes debugging easier. Not doing so is part of the problem, really! Mistakes have much less chance to hide in a clearly structured code.


Then read about the cross-product as a possibility to distinguish whether to rotate to the left or to the right: Use the sign of the cross-product to mirror the angle.

#5 Eck   Members   


Posted 23 January 2014 - 08:58 AM

2.) Why are variable names chosen so that they are not distinguishable from others?
3.) Why are variable names for semantically the same thing so different as for cubepos1 and cube2position?


My reply isn't a slam on aikender. It's meant to serve as encouraging new developers to 


The reason is because new developers don't think good consistent names are important. But truthfully, this is one of my baseline criteria for judging whether or not someone is a good programmer.


Again, I'm not ripping on aikender. I was worse back in the olden-days when I was in college (like a million years ago). I used single and dual character variable names all the time starting out. Typing out a long variable name was "a wast of time" because I was smart enough to keep track of everything just fine. But I was an ignorant arrogant student! :) Sure I could keep track of several variables in an insignificant little program. But in the real world, projects can have millions of lines of code. You might not see a bit of code for MONTHS at a time. If your code isn't named correctly, you'll waste hours trying to figure out exactly what's going on.


Not to mention, at your first real job you'll probably be fixing other people's crapily written code. And one of the reasons why his code is buggy is because he didn't take a few minutes to think about a proper name! angry.png


I think you can see our point aikender, because already, you're having trouble making sense of the code yourself. Games are one of the most complex pieces of software you can write. 


Anyway. One thing you could do to debug your problem is loop through the points of a circle, and output the rotation that you calculate and compare it to what you expect to get. Say from -2pi to 2pi, stepping through at pi/6. This should point you to where your calculations are wrong, but haegarr already pointed out the problem in his #5. If you have any more problems getting it right, I recommend doing this as you work the kinks out of your code.


- Eck

EckTech Games - Games and Unity Assets I'm working on
Still Flying - My GameDev journal
The Shilwulf Dynasty - Campaign notes for my Rogue Trader RPG

#6 ankhd   Members   


Posted 25 January 2014 - 08:39 PM

Hello. when you get your code fixed up, you will want to look up atan2 function on google.


used like this.


v3 unitfacingdirection(0,0,1);//facing the z direction this is the meshes exported facing

v3 targetpos;//the object you want to face at position

v3 totargetdirection = targetpos - unitfacingdirection;




double angle = atan2(totargetdirection.x, totargetdirection.z);//if your terrain is on the x , z plane.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.