Sign in to follow this  
lucky6969b

Bullet How to avoid physics and AI to dominate over each other?

Recommended Posts

If I set the physics after AI, physics will dominate the final world transformations, which totally erased the world transformations calculated from the AI, or vice versa.
I want to calculate the relative transformation when physics is in effect, how do I do that with btMotionState or derived classes with getWorldTransform overrided?
Thanks
Jack

Share this post


Link to post
Share on other sites

If you override transforms after physics you run into the problem that you might break the physics constraints or move into penetration. You can take control over a physics object using ghost objects. A ghost object takes part in the broadphase, but not in the physics simulation. You are responsible for not moving the ghost objects into penetration (e.g. using raycast or volume sweeps).

 

Edited by Dirk Gregorius

Share this post


Link to post
Share on other sites

I assume you are directly modifying position of objects?

If that is the case, you should make the AI set applied force or velocity, for example. Not the position itself. It's a common mistake with many physics engine with broad-phase (engines that will solve the position and angle of objects, the reactions).

Share this post


Link to post
Share on other sites

I don't know how to figure out the forces applied to the object affected....


void AgentMotionState::getWorldTransform(btTransform& worldTrans) const
{

btTransform t;
btDefaultMotionState::getWorldTransform(t);

D3DXMATRIX l_mat;
D3DXMatrixIdentity(&l_mat);
if (m_object)
{
// DX TRANSFORM
l_mat = BT2DX_MATRIX(t);

D3DXVECTOR3 scale, pos;
D3DXQUATERNION quat;

D3DXMatrixDecompose(&scale, &quat, &pos, &l_mat);

Transform* l_transform = m_object->FindComponentByType();

// if setting positions here, the physics will dominate over AI
l_transform->setPosition2(pos);
l_transform->setRotation(quat);

}

// BULLET TRANSFORM
worldTrans = t;

}
Edited by lucky6969b

Share this post


Link to post
Share on other sites

You need to figure out which part of the object is responsible for what actions.  Right now you have physics and AI both trying to move an object around, and that just wont work... clearly.    So, let the physics be responsible for the object's transform and let the AI determine what it wants to do, and then tell the physics system what it wants through setting forces or impulses.

 

9 minutes ago, lucky6969b said:

I don't know how to figure out the forces applied to the object affected....
 

Start with F = ma and work from there.  You should know what mass your objects have, and what speed you want, so just accelerate them until they reach that speed and then set accel back to 0.  Or, if your objects dont need to accelerate, just give them impulses.

Share this post


Link to post
Share on other sites

But if I let the physics run in its own thread, this has some advantages when I don't update the physics too much by leveraging it some time to sleep,the AI actually has time to counteract with what the physics is currently doing. Because physics is continuous, if I divide the 1.0 with the frame rate, I can get some continuous feel without the physics hogging the world transformations all the time, when the physics thread is sleeping, the AI can do its work now...
Thanks
Jack

Share this post


Link to post
Share on other sites
3 hours ago, lucky6969b said:

But if I let the physics run in its own thread, this has some advantages when I don't update the physics too much by leveraging it some time to sleep,the AI actually has time to counteract with what the physics is currently doing. Because physics is continuous, if I divide the 1.0 with the frame rate, I can get some continuous feel without the physics hogging the world transformations all the time, when the physics thread is sleeping, the AI can do its work now...
Thanks
Jack

I'm not sure I understand, do you still have both the AI and physics setting your object's transforms?

Share this post


Link to post
Share on other sites
1 hour ago, lucky6969b said:

I meant when the physics thread goes to sleep, the AI can take control of the world transformation.

The AI should not be controlling the world transforms of your objects.   Not sure what you mean by "when the physics thread goes to sleep".  Regardless of whether you're multithreading your physics, or how you have Bullet set up to update, it should always be in control of the transform and the AI should be interfacing with it through setting forces, impulses, or maybe velocities.  But the AI should not directly change the positions of objects.  You're asking for trouble by doing this.

Share this post


Link to post
Share on other sites
9 hours ago, lucky6969b said:

How can I calculate the forces from the resultant world transformation calculated by bullet?
Thanks
Jack

I'm confused by your question.  It sounds like you're trying to work backwards.

You first decide what you want your object to do, then you apply forces/impulses/velocities to get it to start moving in that direction, and finally Bullet will update the transform every frame according to those forces/impulses/velocities.

But, if you're trying to set the transform yourself through the AI code, you're actively fighting against the physics engine and that will not work.

Share this post


Link to post
Share on other sites

I think people are misunderstanding the question here. AI absolutely does control an object's position in many games, at least indirectly. If I'm walking from place to place then the AI may find a path and attempt to follow it, moving me along that path. And obviously at the same time, other things in the world might apply physical forces in other ways.

The simplest thing that could possibly work here is to implement the AI path-following behaviour as a force applied to the character, so that feeds in to the physics, and the physics applies the actual motion and therefore sets the position. A very simple path follower can just apply a constant force (or assign a desired velocity, whatever your physics supports) in the direction of the next node on the path, and assuming that is sufficient force to move the character, and no other forces deflect or block the character, they can follow the path that way. You will need logic in the path-following system to recognise when it is failing to make progress (e.g. imagine walking onto a treadmill that pushes you back faster than you can move forwards) and signal further up the AI hierarchy to request a change in plan.

Another method is to have and explicit hand-off, where either one system or the other is in charge. e.g. In Unity there is a 'isKinematic' flag on a RigidBody, and if it's set, you can move the character around via navigation but collisions will not (generally) affect the character. Or you can clear the flag and collisions and other forces will work - but navigation may not. Other engines might have a 'ragdoll' mode or similar. Knowing when to switch the flag is tricky, however.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
      628353
    • Total Posts
      2982228
  • Similar Content

    • By MeeMaster
      I am a beginner in the Game Dev business, however I plan to build a futuristic MMO with some interesting mechanics.
      However, I have some doubts about shooting mechanics that I chose for this game and would like to know your opinion on this. The mechanic goes as follows:
      - Each gun would have it's damage-per-shot value
      - Each gun would have it's shots-per-second value
      - Each gun would have it's accuracy rating
      Now the question is: how to calculate the output damage? I have three available options:
      1) Calculate the chance of each shot hitting the target (per-shot accuracy)
      2) Multiply the damage output of a weapon by it's accuracy rating (weapon with 50% accuracy deals 50% of it's base damage)
      3) Don't use accuracy at all and just adjust the weapon damage output
      Which of these three mechanics would you like to see in a game? Mind, this will be an MMO game, so it will have lock-on targets, AoE effects and all that jazz.
    • By lucky6969b
      My AI subsystem is completed dragged by the physics with objects with Gimpact proxies.. When you need to calculate stuff like bumps, it is very horrible...It is even worse than using compound vehicle methods...
      Thanks
      Jack
    • By lucky6969b
      I looked one of the the bullet physics samples which talks about the topic in height field. But however, when the height fields get rendered, the "DemoApplication" class calls the opengl shape drawer object which finally retrieves the display list of the collision shape, which is strongly coupled to opengl, I want to do the same thing with DirectX (D3DX at the moment, damn old, but hey).. How can I draw the height fields out? Is there a way to turn the display list into something recognizable by Direct3D 9?
      Thanks
      Jack
    • By lucky6969b
      //synchronize the wheels with the (interpolated) chassis worldtransform l_vehicle->updateWheelTransform(j,true); const btTransform& t = l_vehicle->getWheelInfo(j).m_worldTransform; D3DXMATRIX l_mat = BT2DX_MATRIX(t); D3DXMATRIX l_rot; D3DXMatrixRotationY(&l_rot, 1.57f); l_mat = l_rot * l_mat; // assume front wheels AgentMotionState* motion = dynamic_cast(l_vehicle->getRigidBody()->getMotionState()); if (motion) { boost::shared_ptr pObj = motion->m_object; if (j==0) { D3DXVECTOR3 s, p; D3DXQUATERNION r; D3DXMatrixDecompose(&s, &r, &p, &l_mat); double yaw, pitch, roll; yaw = pitch = roll = 0.0f; QuatToEuler(r, yaw, pitch, roll); TRACE("Veh: " << i << "Pos of wheel 0 is " << p.x << " " << p.y << " " << p.z); yaw = CapRadian(yaw); TRACE("Veh: " << i << "Rot of wheel 0 is " << yaw); FRAME* frontWheels = (FRAME*)D3DXFrameFind(pObj->m_mesh->GetFrameRoot(), "Front_Left_Wheel"); frontWheels->matCombined = l_mat; //frontWheels->TransformationMatrix = l_mat; }


      Looks like the left front wheel is on the far left of the chassis with a large gap?
      Why is that?
      Thanks
      Jack
    • By 123iamking
      I used to use ScreenPointToRay function to help me do this in Unity3d. But now I use Ogre and Bullet, I have to do this on my own.
      I try to dig into Bullet's example and find out this function getRayTo which convert the point on that click on screen (camera) to the 3d world point with 'farPlane' distance from the camera (that what I think). Then they create the ray (vector AB) by set the A point is the camera position, B point is the point get from the function getRayTo above. I try to re-implement this function in my project (use Ogre for graphic, so I have to use Ogre's camera and world). But I can't get it right because I don't understand the function getRayTo from Bullet.
      Please explain the algorithm to convert screen point to ray. If there is a better algorithm please teach me.
      Note: the scenario to use this algorithm is to find out the position to go for the character - like in the game TouchLight - a point and click game.
      https://github.com/bulletphysics/bullet3/blob/d52fb7510bbe3801431cc0a431e4c1847ee3e0f1/examples/CommonInterfaces/CommonRigidBodyBase.h#L17
  • Popular Now