Author Topic: Orientating a car object to face a point  (Read 2788 times)

Offline dunkedbiscuit

  • byte
  • *
  • Posts: 20
    • View Profile
Orientating a car object to face a point
« on: November 23, 2011, 12:31:58 am »
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!

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11983
    • View Profile
    • http://www.jpct.net
Re: Orientating a car object to face a point
« Reply #1 on: November 23, 2011, 06:54:20 am »
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

Offline dunkedbiscuit

  • byte
  • *
  • Posts: 20
    • View Profile
Re: Orientating a car object to face a point
« Reply #2 on: November 23, 2011, 03:36:52 pm »
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:

Code: [Select]
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.

Code: [Select]
                    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?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11983
    • View Profile
    • http://www.jpct.net
Re: Orientating a car object to face a point
« Reply #3 on: November 23, 2011, 05:07:44 pm »
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.

Offline dunkedbiscuit

  • byte
  • *
  • Posts: 20
    • View Profile
Re: Orientating a car object to face a point
« Reply #4 on: November 23, 2011, 08:43:12 pm »
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?

Code: [Select]
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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11983
    • View Profile
    • http://www.jpct.net
Re: Orientating a car object to face a point
« Reply #5 on: November 23, 2011, 11:40:55 pm »
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

Code: [Select]
cone.rotateX((float) Math.PI/2f); // maybe -2f...not sure here
cone.rotateMesh();
cone.clearRotation();

Offline dunkedbiscuit

  • byte
  • *
  • Posts: 20
    • View Profile
Re: Orientating a car object to face a point
« Reply #6 on: November 24, 2011, 06:48:23 pm »
That seemed to do the trick. Thanks for your help - again!

Offline lapusneanugabi

  • byte
  • *
  • Posts: 5
    • View Profile
Re: Orientating a car object to face a point
« Reply #7 on: February 27, 2012, 09:38:49 pm »
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

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11983
    • View Profile
    • http://www.jpct.net
Re: Orientating a car object to face a point
« Reply #8 on: February 28, 2012, 06:58:24 am »
This
   
   
Code: [Select]
    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(...);

Offline AGP

  • quad
  • ******
  • Posts: 1620
    • View Profile
Re: Orientating a car object to face a point
« Reply #9 on: February 29, 2012, 12:51:26 am »
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.

Offline Peter Dym

  • byte
  • *
  • Posts: 24
    • View Profile
Re: Orientating a car object to face a point
« Reply #10 on: April 06, 2012, 11:09:08 am »
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

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11983
    • View Profile
    • http://www.jpct.net
Re: Orientating a car object to face a point
« Reply #11 on: April 07, 2012, 08:36:30 pm »
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.

Offline Peter Dym

  • byte
  • *
  • Posts: 24
    • View Profile
Re: Orientating a car object to face a point
« Reply #12 on: April 08, 2012, 05:31:42 pm »
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