Flipping X of an complex object.

Official forum for the Chipmunk2D Physics Library.
Post Reply
Birkemose
Posts: 30
Joined: Thu Feb 10, 2011 8:45 am
Contact:

Flipping X of an complex object.

Post by Birkemose »

Hi
I have a small tank running on some terrain, and now I want the tank to be able to drive both ways.
Is it in any way possible to flip the entire object ( bodies, shapes, joints, contraints ) around the X-axis, or should I create a new object?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Flipping X of an complex object.

Post by slembcke »

There isn't really an easy way to do that no. Recreating the object isn't all that easy either as you need to make sure to flip all the shapes (each one is a little different) and all of the constraints (each one is a little different...). You also need to make sure that you aren't flipping the wheel or something through a wall in the process. It's definitely not simple like it is in graphics to just flip the transformation matrix around.

What I've done in the past to get around this is to make the physics models symmetrical. Then it doesn't matter if it's going to the left or right, the physics is the same. All you need to do is flip the sprite for it and nobody is the wiser. The tank you were showing off earlier was already pretty symmetrical, you'd really just need to tell the phase on the gun barrel joint to flip back and forth. It even gives it a nice animation that way as well.

I made a little game prototype once with a fully flippable vehicle for an even crazier example. It's really simple, I just change some of the parameters for the shocks pop the wheels to the other side of the car when it flips over.
http://howlingmoonsoftware.com/extras.php
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Birkemose
Posts: 30
Joined: Thu Feb 10, 2011 8:45 am
Contact:

Re: Flipping X of an complex object.

Post by Birkemose »

Hmm, the ting is, that the asymmetrical design is part of the "tumbler" stragety which prevents the tank from coming to rest upside down.
But you are right so far, that it is only a few parts which actually needs to be changed, and I was wondering if the easiest way would be to create shapes for both ways, and then simply add and remove them to the group, as needed. They are practically without mass anyways.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Flipping X of an complex object.

Post by slembcke »

Yeah, that will work. As long as you remove the object from both the space and the chipmunkObject array it will be fine. If you do one or the other, it will either leave garbage objects in the space or trigger assertions about removing things from a space more than once.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Birkemose
Posts: 30
Joined: Thu Feb 10, 2011 8:45 am
Contact:

Re: Flipping X of an complex object.

Post by Birkemose »

Thx, that will be the way to do it then, and I will make sure to keep group and space in sync.
Birkemose
Posts: 30
Joined: Thu Feb 10, 2011 8:45 am
Contact:

Re: Flipping X of an complex object.

Post by Birkemose »

Well, it turned out, that removing and adding joints and constraints at runtime, wasn't such a stellar idea. I guess even if I place a constrained body in right position, the constraint isn't "up to speed", so I would probably have to remove the old constraint completely, and add a new one.
In stead I ended up adding both objects which face left, and objects which face right, and then simply ignore collisions on objects facing the wrong way. Downside is simulating 3 bodies, 3 shapes, 2 joints, 2 rotation limiters and 1 gear more than I actually need, but I guess that is a small price to pay.
Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests