Hello all,
For context - here's what I'm trying to do. I have this deep-sea diver that's shooting a spear underwater. The spear is a rigid body, and invisible, and when the user performs the (directional) shoot command, I unveil the spear, and start moving it in the correct direction. This is all fine and dandy. What I want, is to rotate the spear in the direction of movement.
Or simply: If I'd like to rotate an object by 30 degrees around let's say the X axis - How would I accomplish that? I know I can set the Angular velocity, but I don't want that - I want to simply "set" the rotation, or just rotate the object by a certain degree around a certain axis.
Thanks in advance.
Forcing a rigid body's orientation?
-
KulSeran
- Posts: 9
- Joined: Thu Dec 10, 2009 10:32 pm
Re: Forcing a rigid body's orientation?
1) The correct way would probably be to just spawn a new spear rigid body when you press shoot. Your spawned RBody would just get the correct matrix when you shoot it.
2) You can always just call setWorldTransform() on the RBody to move it.
Though you may end up with artifacts from just "teleporting" the rigid body around. You may also need to clear the velocity and forces on the RBody after teleporting it.
2) You can always just call setWorldTransform() on the RBody to move it.
Though you may end up with artifacts from just "teleporting" the rigid body around. You may also need to clear the velocity and forces on the RBody after teleporting it.
-
Muller
- Posts: 6
- Joined: Fri Oct 16, 2009 12:20 pm
Re: Forcing a rigid body's orientation?
Here's our solution:
Code: Select all
btVector3 xAxis(1,0,0);
float angle = normal.angle(xAxis); // Normal is the direction we want to align the rigid body with
if(normal.z() > 0)
angle = -angle;
btQuaternion qt(angle, 0, 0);
_btRigidBody->getWorldTransform().setRotation(qt);
-
ejtttje
- Posts: 96
- Joined: Mon Nov 03, 2008 9:57 pm
Re: Forcing a rigid body's orientation?
FYI I found teleports much more stable (in particular if it might be interacting with other bodies immediately before or after the teleport) by removing the rigid body from the dynamics world and then adding it back in the correct pose... avoids issues with velocity/momentum type stuff, at least for what I was doing.
-
kamikaze999
- Posts: 5
- Joined: Wed May 05, 2010 6:31 am
Re: Forcing a rigid body's orientation?
float angle = normal.angle(xAxis); // Normal is the direction we want to align the rigid body with
normal is what??? i'm trying to figure out how to set the rotation given a rotation from irrlicht for AI chasing movement, so if this works it would be wonderful. im assuming normal is just a vector3. here is what i have from irrlicht
vector3df boypos(boy->getPosition());
vector3df chasepos(node->getPosition());
vector3df diff = boypos - chasepos;
vector3df targetangle = diff.getHorizontalAngle();
so normally in irrlicht i would set the rotation, but im doign my rotations in bullet.... i see your code only utilizes 1 axis....
-
ejtttje
- Posts: 96
- Joined: Mon Nov 03, 2008 9:57 pm
Re: Forcing a rigid body's orientation?
'normal' doesn't matter to you, the point is you have a target orientation and you want to apply it.
Your two options are 'non-physics based', where you control the position and orientation directly, or 'physics based', where you apply forces and torques to steer in the desired direction. The choice depends on whether you expect your AI vehicle to interact with the environment... obviously if you're doing the non-physics thing, you can't expect very good physics response. As I mention above, if you remove and re-add to the physics world, it tends to be reasonable.
Otherwise, apply forces and torques directly, or use a constraint tied to the world and set the constraint parameters to do the steering for you.
Your two options are 'non-physics based', where you control the position and orientation directly, or 'physics based', where you apply forces and torques to steer in the desired direction. The choice depends on whether you expect your AI vehicle to interact with the environment... obviously if you're doing the non-physics thing, you can't expect very good physics response. As I mention above, if you remove and re-add to the physics world, it tends to be reasonable.
Otherwise, apply forces and torques directly, or use a constraint tied to the world and set the constraint parameters to do the steering for you.
-
kamikaze999
- Posts: 5
- Joined: Wed May 05, 2010 6:31 am
Re: Forcing a rigid body's orientation?
so your saying i should drop and add the player continously plugging in the new position/rotation? wouldn't that look choppy? how would that affect game performance?
-
ejtttje
- Posts: 96
- Joined: Mon Nov 03, 2008 9:57 pm
Re: Forcing a rigid body's orientation?
I don't see why this would look any more choppy than any other method to control the position/orientation of an object. Whether or not it looks choppy depends on the positions/orientations you use, not the technical details of which function(s) you use to get it into that position... assuming the graphics framerate isn't affected.
I suppose there is a question as to whether you want to update the position before each physics update vs. before each graphics update (usually there are multiple physics updates per graphics update for better temporal resolution for physics simulation -> more accurate interactions). I do it just before each graphics update, and let it hold the position across each set of physics updates, but you can experiment.
For performance, it doesn't seem noticeable. There's probably some overhead in managing some associated lists and pointers, but it's negligible compared to the amount of work done elsewhere. Especially if you're only doing this for one object...
But like I said, you can also use a constraint and move that around instead of directly controlling the player's avatar. Just depends whether you want absolute control over the exact position/orientation, or if you want some influence from collisions and such.
I suppose there is a question as to whether you want to update the position before each physics update vs. before each graphics update (usually there are multiple physics updates per graphics update for better temporal resolution for physics simulation -> more accurate interactions). I do it just before each graphics update, and let it hold the position across each set of physics updates, but you can experiment.
For performance, it doesn't seem noticeable. There's probably some overhead in managing some associated lists and pointers, but it's negligible compared to the amount of work done elsewhere. Especially if you're only doing this for one object...
But like I said, you can also use a constraint and move that around instead of directly controlling the player's avatar. Just depends whether you want absolute control over the exact position/orientation, or if you want some influence from collisions and such.