Spherical coordinates

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
pippy
Posts: 49
Joined: Sun Jul 08, 2007 11:31 pm

Spherical coordinates

Post by pippy »

I'm trying to get an objects position to extrude from another objects rotation.

The meshes position should be where it faces plus a radius. The only Cartesian coordinate based spherical code on the internet I can find presumes that there are two values (phi and lowercase phi) used to get the point on the sphere.

This is what I have so far:

Code: Select all

core::vector3df sphareToPosition(const core::vector3df &rot,
              const core::vector3df &pos, u32 radius){
    // out put position
    core::vector3df cam(0.0f,0.0f,0.0f);
    
    // degrees to radians
    f32 rotX = (rot.X + 180) * core::PI / 180.0;
    f32 rotY = -(rot.Y + 135) * core::PI / 180.0;
    
    // place the 'camera' above the LOS 
    cam.X = pos.X + radius * cos(rotY) * sin(rotX);
    cam.Y = pos.Y + radius * sin(rotY) * sin(rotX) ;
    cam.Z = pos.Z + radius * cos(rotX);
 
    return cam;     
}
As you probably know Irrlicht Engine uses three values to calculate rotation.

How would I extrude the position of a rotation?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

How do you define "where it faces"? Except for cameras this does not make sense, because the usual scene node does not have a lookat vector. For cameras you just have to take the normalized lookat vector, multiply it by your radius, and add it to the cam's position. Otherwise you can get the lookat vector by defining your "initial facing direction", e.g. (0,0,1), and rotate it by the absolute rotation of the node. Then proceed as before.
pippy
Posts: 49
Joined: Sun Jul 08, 2007 11:31 pm

Post by pippy »

"Where it faces" I mean the vector3df returned by mesh->getRotation();

Code: Select all

normalized lookat vector, multiply it by your radius, and add it to the cam's position. 
I'll try this with my scene node (then your other method)

thanks for the help =D
Last edited by pippy on Fri Feb 08, 2008 10:30 am, edited 1 time in total.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

getRotation() does not directly tell you where something faces. It tells you the amount that object is rotated on each of its axes. You can use this to get a direction, but that vector is most definitely not a location in 3d space or a direction.

Travis
pippy
Posts: 49
Joined: Sun Jul 08, 2007 11:31 pm

Post by pippy »

vitek: thats what I'm trying to do, convert rotation to a position given a radius.

Code: Select all

// to show positon of where you are looking
core::vector3df sphareToPosition(core::vector3df rot, const core::vector3df &pos, u32 radius){
    core::vector3df lambdaPos;
    lambdaPos = rot.normalize ();
    lambdaPos *= radius;
    lambdaPos += pos;
    
    return lambdaPos;
}
Sadly this does not work. I'll read more about quaternions and rotations and post the code when I get it.
pippy
Posts: 49
Joined: Sun Jul 08, 2007 11:31 pm

Post by pippy »

What I'm trying to do looks like a slerp

Such a weird name, all I'm trying to do is project a rotation to a 3d position.
Wiki has an article about slerp here. One way to solve my problem is to convert my rotation to a quaternion and then slerp it. Spherical linear interpolation sounds much more nerdy.

I'll try it over the weekend, probably Sunday because I'm drinking right now.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

No, you don't want a slerp. That is for interpolating between two quaternions.

You want to find a point that a rotated node would be facing. Hybrid described exactly how to do that above...

Code: Select all

// build a rotation matrix from the rotation values
core::matrix4 m;
m.setRotation (rot);

// get a vector that points forward, and rotate it using the matrix
core::vector3df dir (0, 0, 1);
m.rotateVect (dir);

// get a position that is radius units from pos in the direction dir
core::vector3df r (pos);
r += (dir * radius);
Travis
pippy
Posts: 49
Joined: Sun Jul 08, 2007 11:31 pm

Post by pippy »

poop, cheers vitek! that works perfectly!


I can't thank you enough, I've been working on this problem for the past 4 days. I have been using converting a circle into 3d space above where I want to look at and it looked really tacky, but now the camera movement is smooth and looks great :D

Here's the function:

Code: Select all

// to show position of where you are looking
core::vector3df sphereToPosition(core::vector3df rot, const core::vector3df &pos, u32 radius){
    // build a rotation matrix from the rotation values
    core::matrix4 m;
    m.setRotationDegrees  (rot);
    
    // rotate the vector using a matrix that points forward
    core::vector3df dir (0, 0, 1);
    m.rotateVect (dir);
    
    // add the position of the object to the projected radius 
    core::vector3df r (pos);
    r += (dir * radius); 

    return r;
}
Post Reply