help: align a cone toward a target (solved)

GianCarloM
Posts: 4
Joined: Thu May 13, 2010 1:24 pm
Location: Rome, ITALY

help: align a cone toward a target (solved)

Post by GianCarloM »

Image
Hello everyone. In this image the larger cone is fallling but is oriented toward the cam position (where it is created).
I would like to orient or align a shape (a cone) toward a certain position.
The shape is created at cam position (let's say x:0, y:-10, z:80) facing down with the closed end looking down (i guess this is normal). It should point at (x:0,y:0,z:0);
How do I calculate the rotation along the 3 axis?
I think one should do something like:

Code: Select all

    Transform tt = myRigidBody.getWorldTransform(trnsfrm);
    tt.setRotation(etoq(0, phi, theta));
    myRigidBody.setWorldTransform(tt); 

// also the shape is accelerated with setLinearVelocity(linVel);
Usually I get the angles (in radians) doing teh following:

Code: Select all

    Vector3f linVel = new Vector3f(destination.x - initPosition.x, destination.y - initPosition.y, destination.z - initPosition.z);
    float theta = atan2(linVel.y,linVel.x); //y
    float phi = acos(linVel.z/(float) distance); //z
I miss the X angle. And also i see that setRotation() accept a Quaternion.
Here's my function wich transforms 3 angles into a Quat:

Code: Select all

 Quat4f etoq (float heading, float attitude, float bank) {
    // Assuming the angles are in radians.
    float c1 = cos(heading/2);
    float s1 = sin(heading/2);
    float c2 = cos(attitude/2);
    float s2 = sin(attitude/2);
    float c3 = cos(bank/2);
    float s3 = sin(bank/2);
    float c1c2 = c1*c2;
    float s1s2 = s1*s2;
    float w = c1c2*c3 - s1s2*s3;
    float x = c1c2*s3 + s1s2*c3;
    float y = s1*c2*c3 + c1*s2*s3;
    float z = c1*s2*c3 - s1*c2*s3;
    return new Quat4f(x,y,z,w);
  }
But it does not work.
Any help? :(

Thanks
GC
Last edited by GianCarloM on Wed May 26, 2010 12:38 pm, edited 1 time in total.
GianCarloM
Posts: 4
Joined: Thu May 13, 2010 1:24 pm
Location: Rome, ITALY

Re: help: align a cone toward a target (solved)

Post by GianCarloM »

As far as it concern the jBullet version, used within Processing, aligning a shape (a cone in this case) toward another point is easy but it took several days to work out, because my math is very bad.
Anyhow, let's assume we need to accelerate an object toward a point and align that toward that point, all we need is two PVectors one storing the origin XYZ, and another storing the target coords.

Code: Select all

//Processing's PVector math
PVector dest = new PVector(destination.x,destination.y,destination.z);
PVector orig = new PVector(origin.x,origin.y,origin.z);
PVector dir = PVector.sub(dest,orig); // direction
float a = PVector.angleBetween(dest,orig);
float d = dest.dist(orig);
dir.normalize();
dir.mult(100.0);

//jBullet
Transform tt = myRigidBody.getWorldTransform(trnsfrm);
tt.setRotation(new Quat4f(orig.x, orig.y-d, orig.z, a));
myRigidBody.setWorldTransform(tt);
myRigidBody.setLinearVelocity(new Vector3f(dir.x,dir.y,dir.z));
myRigidBody.setCenterOfMassTransform(tt);
And voilà. The bullet (cone) is aligned toward the direction of motion.
A live Applet:
http://www.openprocessing.org/visuals/?visualID=9705
SPACEBAR to shoot.