Box2D Forums

It is currently Mon May 20, 2013 1:02 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Wed Jun 06, 2012 3:43 pm 
Offline

Joined: Thu Oct 27, 2011 11:36 am
Posts: 13
Hi,
i am working on breakable bodies. For example, i have a single big square box, represented with a box-polygonal shape, as in this video:

http://tinypic.com/r/65tp2t/6

In a off-line phase, i create N texture pieces to simulate cracks. Every texture part generated is approximated
with a convex polygon (or more if the convex polygon has too many vertices), that will be used as a fixture
for the part body. The pieces form a perfect subdivision of the original texture.
When it is time, i de-activate (actually, remove from box2d scene) the big box and place all the parts in the
correct position (the big box position + offset of the parte) and orientation (i use the big box orientation).

In the video above, the box is fixed at a position in the screen with a revolute joint. In the test, after few second, i remove the box body
(so the joint is destroyed) and all the small parts are placed in the scene. There is a floor down there even if it is
not represented with a sprite.

In this simple case, it seems to work, but what if the big box body is moving (maybe accelerating, spinning etc...)?
How can i put the small bodies in the box2d world and set their properties in order to keep the simulation 'smooth' (avoiding
unphysical behaviour of the small objects)?

Another way i will try is to create directly the broken body, with distance joints in order to keep it togheter.
When there is a collision, one just destroy the joints (or a part of them, to make things more funny) and let
box2d do the rest.

I wonder if not-perfect texture alignments will show up. Another thing that concerns me is the fact that all that bodies
must be created and at the start of the simulation, while in the other way, they are created only when the body have
to be destroyed. This is clearly faster, even if it could generate some frame rate drops...

Ps.
Some little pieces are going through the floor, but it should be a bug of mine...


Top
 Profile  
 
PostPosted: Wed Jun 06, 2012 8:44 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1515
Location: Tokyo
To make the new smaller pieces continue at the same speed and rotation as the original large one, I think you could use this function of b2Body to get the appropriate linear velocity:
Code:
        /// Get the world linear velocity of a world point attached to this body.
        /// @param a point in world coordinates.
        /// @return the world velocity of a point.
        b2Vec2 GetLinearVelocityFromWorldPoint(const b2Vec2& worldPoint) const;

You would pass this the new center-of-mass position for each of the small bodies to find the correct linear velocity to use for that piece. The angular velocity I think would just be the same angular velocity as the original box, for each small piece. This is all a guess though :)


Top
 Profile  
 
PostPosted: Thu Jun 07, 2012 4:50 am 
Offline

Joined: Thu Oct 27, 2011 11:36 am
Posts: 13
@irresistible_force

Thank you, now it seem to work correctly:

http://tinypic.com/r/2qicjmt/6


To compute the position of a object part, given its offset in object coordinates, just call:

b2Vec2 part_position = GetWorldPoint( offset );

and it will give you the world coordinats of the part. To get its linear velocity:

b2Vec2 lin_vel = GetLinearVelocityFromWorldPoint( part_position );

The angle and angular velocity are obtained using GetAngle() and GetAngularVelocity().

I will do some further tests...

Ps.
Should i make the sum of the part masses equal to the mass of the original object??
And if so, how to do that?


Top
 Profile  
 
PostPosted: Thu Jun 07, 2012 5:35 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1515
Location: Tokyo
If the total area of the smaller pieces is the same as the original large piece and the density is the same, then the mass should be the same...

If not, you could loop through all the pieces and find the total mass. Then you would know how much to scale down the mass to get the desired total. You can then loop through all the pieces again, and change their mass by:
- use GetMassData to get the current mass data
- scale the 'mass' and 'I' members of the mass data
- use SetMassData to set the changed mass data back into the body


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Bing [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group