How to get the tangents for a point on a spherical surface?
Posted: Thu Nov 27, 2014 9:57 am
Hello,
I am trying to implement a game engine where a character can run and jump on the surface of a planet, with the gravity vector points towards the planet center.
I have managed to calculate the gravity vector towards the planet center with this formula:
Now I want to rotate the capuse/cylinder (that represents the player character) so that the base always faces the surface of the sphere e.g. the character needs to stand with his or her feet on the ground on the planet:
This does not seam to work unless movement only occurs in the x-direction, but in other directions the capsule rotated incorrectly.
I suppose the capsule needs to be rotated according the the basis that is formed by the normal and the tangent plane for the surface of the sphere where collision occurs, but I don't know how to calculate it (I only have the normal e.g. the gravitational vector). I have been searching on the internet and found some information about using btPlaneSpace1(n, p, q) but that produces strange results when used directly, so I only used the part where the vector "p" lies in the x-y plane:
- Can someone please help me and describe how to obtain the vectors for the tangent plane of the sphere (representing the planet)?
- Does bullet provide a method for that, I have not found anything in the documentation?
PS. The full code can be found at: https://github.com/phoboz/tuxgal/blob/m ... Object.cpp
I am trying to implement a game engine where a character can run and jump on the surface of a planet, with the gravity vector points towards the planet center.
I have managed to calculate the gravity vector towards the planet center with this formula:
Code: Select all
body->getMotionState()->getWorldTransform(trans);
btVector3 n = m_gravityCenter - trans.getOrigin();
n.normalize();
body->setGravity(9.8 * n);
Code: Select all
btVector3 p, q;
p.setValue(-n.getY(), n.getX(), 0);
q = p.cross(n);
btMatrix3x3 basis(
p.getX(), p.getY(), p.getZ(),
n.getX(), n.getY(), n.getZ(),
q.getX(), q.getY(), q.getZ()
);
btTransform objTransform;
body->getMotionState()->getWorldTransform(objTransform);
objTransform.setBasis(basis);
obj->setWorldTransform(objTransform);
I suppose the capsule needs to be rotated according the the basis that is formed by the normal and the tangent plane for the surface of the sphere where collision occurs, but I don't know how to calculate it (I only have the normal e.g. the gravitational vector). I have been searching on the internet and found some information about using btPlaneSpace1(n, p, q) but that produces strange results when used directly, so I only used the part where the vector "p" lies in the x-y plane:
Code: Select all
p.setValue(-n.getY(), n.getX(), 0)
- Does bullet provide a method for that, I have not found anything in the documentation?
PS. The full code can be found at: https://github.com/phoboz/tuxgal/blob/m ... Object.cpp