Nice looking pseudo destruction

ca$per
Posts: 35
Joined: Fri May 21, 2010 2:48 pm

Nice looking pseudo destruction

Post by ca$per »

Hi all!
I'm trying to implement a nice looking pseudo destruction of a ship. By pseudo i mean that all parts that should be "destructed" are separate meshes.
Perhaps the screen will explain better.
Green wireframes show a created btConvexHullShape. Red is it's AABB. Everything is drawn with a help of bullet's debug draw.
The main problem is that if i just simply create a rigid body with this shape, set it's mass to 1.0f and add it to the world, it just falls down... actually it looks like it's sliding down. It doesn't look like it is braked from an explosion for example. Tried to use applyImpulse to push it away. It works. It falls a side, but it doesn't rotate in air. So it again just slides sideways. Tried to use applyImpulse with a relative position, no result. Tried to set angular speed... results were.... strange. It kept rotating without stoping. It's pivot point is located at the bottom of the mast.
So the question is, how to make this "destruction" look more natural?
You do not have the required permissions to view the files attached to this post.
sparkprime
Posts: 508
Joined: Fri May 30, 2008 2:51 am
Location: Ossining, New York

Re: Nice looking pseudo destruction

Post by sparkprime »

Maybe attach a constraint to hold it to the original piece for a few frame.

Also the reason it's not falling naturally could be due to inertia or damping, so you should test it falls naturally by itself before worrying about the destruction method.
robagar
Posts: 48
Joined: Fri May 21, 2010 1:49 am

Re: Nice looking pseudo destruction

Post by robagar »

Yes, and you might want the topmast to stay attached, to simulate it being caught up in the rigging. Nice boat btw :)
ca$per
Posts: 35
Joined: Fri May 21, 2010 2:48 pm

Re: Nice looking pseudo destruction

Post by ca$per »

sparkprime wrote:Maybe attach a constraint to hold it to the original piece for a few frame.
Hmm... haven't tried that yet. But this way looks not right to me. It's kind of weird. Are there any other methods? All is needed, is this mast to at least start to rotate a little.
sparkprime wrote:Also the reason it's not falling naturally could be due to inertia or damping, so you should test it falls naturally by itself before worrying about the destruction method.
That's exactly what i'm trying to do. The destruction method is just a creation of a rigid body. Nothing more.
robagar wrote:Yes, and you might want the topmast to stay attached, to simulate it being caught up in the rigging. Nice boat btw :)
I'm not sure i fully understood you. Could you elaborate a little more?

Thank you...
robagar
Posts: 48
Joined: Fri May 21, 2010 1:49 am

Re: Nice looking pseudo destruction

Post by robagar »

I was just thinking that if you temporarily attached the topmast to the main with a ball & socket joint like sparky suggested to start it off falling nicely, you could actually leave it connected. That would look like a mast that had been knocked down, but was still tangled up in rigging so it couldn't fall freely.
ca$per
Posts: 35
Joined: Fri May 21, 2010 2:48 pm

Re: Nice looking pseudo destruction

Post by ca$per »

robagar wrote:I was just thinking that if you temporarily attached the topmast to the main with a ball & socket joint like sparky suggested to start it off falling nicely, you could actually leave it connected. That would look like a mast that had been knocked down, but was still tangled up in rigging so it couldn't fall freely.
Ok. Got it now! Thanks!
I actually thought of something more easy, like make this mast just rotate a little when it falls. Like if it was hit by something in the top point. If that would look sufficient, it will be enought for me. But the problem is that i don't know how to do it. Playing with the relPos parameter in applyImpulse function didn't have any results. I thought that if i just apply an impulse to a top point of this mast it would fall with rotation.
robagar
Posts: 48
Joined: Fri May 21, 2010 1:49 am

Re: Nice looking pseudo destruction

Post by robagar »

Yes, an impulse to the top of the mast should certainly give it some rotation. How are you calculating the relpos value? I got caught out by assuming it would be in object local coordinates, but it's actually in world space.

(btw I'm working on boats too, though so far I've only got as far as making them float. Post a demo when you get it working!)
ca$per
Posts: 35
Joined: Fri May 21, 2010 2:48 pm

Re: Nice looking pseudo destruction

Post by ca$per »

robagar wrote:Yes, an impulse to the top of the mast should certainly give it some rotation. How are you calculating the relpos value? I got caught out by assuming it would be in object local coordinates, but it's actually in world space.
That's strange. I sure read from the forum that they are in local space and the impulse itself is in world space. However, i tried it different ways:
1. rigidBody.applyImpulse(btVector3(10.0f, 0.0f, 0.0f), btVector3(50.0f, 50.0f, 50.0f);
2. rigidBody.applyImpulse(btVector3(10.0f, 0.0f, 0.0f), btVector3(50.0f, 50.0f, 50.0f) + rigidBody.getCenterOfMassPosition());

Both gave me the same "sliding". Nothing changed.
robagar wrote:(btw I'm working on boats too, though so far I've only got as far as making them float. Post a demo when you get it working!)
Nice looking! But unfortunatelly we are still far away from making a demo. But when it's finished we sure will post it here.
dangerdaveCS
Posts: 14
Joined: Mon Jun 07, 2010 5:37 am

Re: Nice looking pseudo destruction

Post by dangerdaveCS »

Perhaps you need to calculateLocalInertia for the btCollisionShape and pass that to the btRigidBody constructor so it can create the inertia tensor.
ca$per
Posts: 35
Joined: Fri May 21, 2010 2:48 pm

Re: Nice looking pseudo destruction

Post by ca$per »

Perhaps this is where i made mistakes.
Here is my class:

Code: Select all

class BreakableMast:
  public btMotionState
{
  private:
    Node *mastNode;
    btConvexHullShape convexShape;
    btRigidBody rigidBody;

    void setWorldTransform(const btTransform &worldTrans)
    {
      Transform4D transform;
      worldTrans.getOpenGLMatrix(static_cast<btScalar*>(transform.Get()));
      mastNode->SetNodeTransform(transform);
      mastNode->Invalidate();
    };

    void getWorldTransform(btTransform &worldTrans) const
    {
      Transform4D transform(mastNode->GetNodeTransform());
      worldTrans.setFromOpenGLMatrix(static_cast<btScalar*>(transform.Get()));
    };

  public:
    BreakableMast(Node *node, const float *vertices, const unsigned long vertexCount);
    ~BreakableMast();
};
Shape and body are direct members. They are not constructed explicitly using new operator. Thus i can't compute intertia tensor BEFORE passing shape to rigid body. I compute it after that. Can this affect things?

EDIT:
Checked with creating them throught "new". No difference.
The only thing that made it look different is changing inertia tensor of a rigid body directly in it's constructor. But i have no clue what should i pass there, because with different number my objects behaive really strangely.
robagar
Posts: 48
Joined: Fri May 21, 2010 1:49 am

Re: Nice looking pseudo destruction

Post by robagar »

What does the BreakableMast constructor look like?
ca$per
Posts: 35
Joined: Fri May 21, 2010 2:48 pm

Re: Nice looking pseudo destruction

Post by ca$per »

Sorry... forgot about it.

Code: Select all

BreakableMast::BreakableMast(Node *node, const float *vertices, const unsigned long vertexCount):
  mastNode(node),
  convexShape(vertices, vertexCount, 12),
  rigidBody(1.0f, this, &convexShape)
{
  convexShape.calculateLocalInertia(1.0f, btVector3(0.0f, 0.0f, 0.0f));
  TheBulletMgr->AddRigidBody(&rigidBody);
  Transform4D transform(node->GetNodeTransform());
  rigidBody.getWorldTransform().setFromOpenGLMatrix(transform.Get());
}
Tried with different combinations of inertia calculations - after or before adding to world. No difference. I guess i don't know how to deal with it correctly.
robagar
Posts: 48
Joined: Fri May 21, 2010 1:49 am

Re: Nice looking pseudo destruction

Post by robagar »

Yes the problem is with this line:

Code: Select all

  convexShape.calculateLocalInertia(1.0f, btVector3(0.0f, 0.0f, 0.0f));
The second inertia parameter is an out parameter - you pass in a vector and calculateLocalInertia fills in the values for you. The way you're calling it the vector only exists on the stack for that function call, and is destroyed as soon as the function returns.

Try something like this

Code: Select all

btScalar mass = 1.0;
btVector inertia(0,0,0);
convexShape.calculateLocalInertia(mass, inertia);

// now the inertia vector should have correct values, update the rigid body
// (I think this is the correct way, but I've not tried it myself)
rigidBody.setMassProps(mass, inertia); 
btw have you tried using a simpler collision shape, like a cylinder?
ca$per
Posts: 35
Joined: Fri May 21, 2010 2:48 pm

Re: Nice looking pseudo destruction

Post by ca$per »

Thank you! I guess i missed that part in tutorials. I will test it and report back.

P.S. Yes i have tried with cylinders. Same thing.
robagar
Posts: 48
Joined: Fri May 21, 2010 1:49 am

Re: Nice looking pseudo destruction

Post by robagar »

no problem :)