Orientating a car object to face a point

Started by dunkedbiscuit, November 23, 2011, 12:31:58 AM

Previous topic - Next topic

dunkedbiscuit

Hello,

I would like a function which makes one object face another object, in that object A "points towards" object B. This would allow me to make some racing car pathfinding, since they need to move in the direction of successive point vectors to simulate driving along a road. I suppose I should be more precise with "pointing"; maybe the vertical axis of object A should be aligned with a vector travelling from the centre of object A to the centre of object B?

Many thanks!

EgonOlsen

You could try this:


  • calculate the direction vector between the two cars
  • use getRotationMatrix() in SimpleVector to create a rotation matrix for this vector
  • set this matrix as the new rotation matrix for the car in question

dunkedbiscuit

Regarding step 1:

I have a cone (called pointy) and a point to aim it at, called picked, which is just a SimpleVector from an array called nodes. I then use this code to find the direction vector:

picked = nodes[i];
                    SimpleVector directionVector = new SimpleVector(
                            picked.centre.x - pointy.getCenter().x,
                            picked.centre.y - pointy.getCenter().y,
                            picked.centre.z - pointy.getCenter().z );
                    directionVector.add(
                            new SimpleVector(
                            pointy.getCenter().x,
                            pointy.getCenter().y,
                            pointy.getCenter().z )
                            );


Regarding steps 2 and 3:

I then get the rotation matrix from the rotation vector and apply this to the cone pointy.

                    Matrix rotationMatrix = directionVector.getRotationMatrix();
                    pointy.setRotationMatrix(rotationMatrix);


However, pointy doesn't seem to face the direction vector. It seems to rotate a bit on one of its axes; the pointy bit of the cone does not face the point. So maybe I need to do some kind of transformation to make it rotate properly?

EgonOlsen

That code makes no sense. You are adding the same values that you've subtracted before. The result is simply picked.centre, which isn't a direction vector. Apart from that, getCenter() returns the center in object space. What you want is the center or position in world space, so either use getTransformedCenter or getPosition instead.

dunkedbiscuit

Hm. I've made the changes you suggested, and the cone does indeed seem to orientate to the points now. However, it seems to orientate its base with the direction of the vector, if you know what I mean. I need the actual mesh to face the point "point on", so I tried using rotateMesh() on the cone after setting the rotation matrix. Needless to say, it didn't work; it just doesn't aim at the point. Any thoughts?

SimpleVector directionVector = new SimpleVector(
                            picked.centre.x - pointy.getTransformedCenter().x,
                            picked.centre.y - pointy.getTransformedCenter().y,
                            picked.centre.z - pointy.getTransformedCenter().z );

                    Matrix rotationMatrix = directionVector.getRotationMatrix();
                    pointy.setRotationMatrix(rotationMatrix);
                    pointy.rotateMesh();


I should have mentioned that picked is derived from a class that holds a SimpleVector as an attribute called centre; getting centre retrieves the SimpleVector.

EgonOlsen

That's because the method aligns the cone in a way that it's z-axis will match the direction vector. But your cone most likely points up. Remove this rotateMesh() and add something like


cone.rotateX((float) Math.PI/2f); // maybe -2f...not sure here
cone.rotateMesh();
cone.clearRotation();

dunkedbiscuit

That seemed to do the trick. Thanks for your help - again!

lapusneanugabi

Hi!
I have the same problem. I want to rotate a car from the old position to the next.
I have the following code:

SimpleVector carOldPosition = car.getTransformedCenter();   
SimpleVector tr = new SimpleVector(-locO.x + loc.x,  -locO.y + loc.y, -3.059705E-7f);
car.translate(tr);
SimpleVector directionVector = new SimpleVector(
                  carOldPosition.x - car.getTransformedCenter().x
              ,   carOldPosition.y -  car.getTransformedCenter().y
              ,   carOldPosition.z -  car.getTransformedCenter().z);

Matrix rotationMatrix = directionVector.getRotationMatrix();
car.setRotationMatrix(rotationMatrix);
car.rotateX((float) Math.PI/2f);
car.rotateMesh();


Is not the desired behavior

EgonOlsen

This
   
   
    car.rotateX((float) Math.PI/2f);
    car.rotateMesh();
   

     
should be changed. You are not supposed to call rotateMesh() at runtime if not for a very good reason.
And this isn't one. If that is in you rotate the car into it's initial position, just do it once at setup time.
And don't forget the car.clearRotation() afterwards. At runtime, just use the call to setRotationMatrix(...);

AGP

Plus, and this is a small but relevant detail: multiply by .5f instead of dividing by 2f when dealing with floats. Multiplication is twice as fast as division.

Peter Dym

Hi All,
I have a similar problem. I have a rocket launched from the plane. And I would like this rocket to slowly turn to the target. Actuall to have a guided rocket. In the discussion above is decribed how to get the right rotation matrix - but I would like to turn the rocket to the target in steps. Any ideas?

http://www.youtube.com/watch?v=DrPFrDcffc4&feature=youtu.be

EgonOlsen

You could try to interpolate the direction vector of the rocket and set the rotation matrix of the rocket to the result from getRotationMatrix() of your interpolated vector. I'm not sure how this will work out though...it's just an idea that came to my mind. You you can interpolate the rotation matrices directly. But both variants might not look good if the difference between two values to pretty large.

Peter Dym

Thanx, the problem is that I do not know how to interpolate neither vector nor matrix.
I'm trying  but no joy.
- If I create a test object and
- if I calculate the direction vector to the target and
- the rotation matrix of the vector is then used for the the test object,

it works well - always pointing to my target.
But how to do do it incrementally? I do not know. This is the code:

// reset the testObject - which is flying  in the world
testObject.clearTranslation();
testObject.clearRotation();
// returns a vector from the source position to the target pos.
SimpleVector dir=Chaser.getDirection(sourceObj, targetObj);
// set the directon to point to the target
testObject.setRotationMatrix(dir.getRotationMatrix());
// and put the testObject to the source to seeit
testObject.translate(sourceObj.getTranslation());
// and a liitle bit up
testObject.translate(0, 5,0);

Here I cannot attache pictures so I added them to my original thread related to the propject I'm working on:

http://www.jpct.net/forum2/index.php/topic,2592.msg19344.html#msg19344