Creating shapes without a body. [pymunk]

Official forum for the Chipmunk2D Physics Library.
Post Reply
CylonOven
Posts: 2
Joined: Sat Apr 27, 2013 9:02 am
Contact:

Creating shapes without a body. [pymunk]

Post by CylonOven »

I'm trying to have a game where you would put different shapes together to form a single 'ship'. This would not need any physics, just basic collision detection.
Then when the ship is built, place all the parts(shapes) onto a single pymunk.body to be used in a simulation.

However shapes want to have a body specified in the init, so how is that done?
The question is: how do create a shape without a body? This should be possible because in the API docs for all Shapes there is this method:
update(position, rotation_vector)

Update, cache and return the bounding box of a shape with an explicit transformation.

Useful if you have a shape without a body and want to use it for querying.
offonoff
Posts: 28
Joined: Sun Nov 06, 2011 1:38 am
Contact:

Re: Creating shapes without a body. [pymunk]

Post by offonoff »

Why can't you have a ship body that the shapes are all attached to? Just make if before you create your shapes and attach each to it when you're ready. You don't add it to the space, and you update the position and angle of all the shapes simultaneously by updating the body. Updating each shape individually would be difficult.

Is there something I'm not getting?
viblo
Posts: 206
Joined: Tue Aug 21, 2007 3:12 pm
Contact:

Re: Creating shapes without a body. [pymunk]

Post by viblo »

Its not very clear from the docs, but you can send in None as the body. Like this:

Code: Select all

>>> import pymunk
>>> pymunk.Circle(None, 3)
EDIT: Latest trunk of pymunk has support for setting the body later. If you dont use trunk this is the relevant code to set a body (where self is the shape):

Code: Select all

def _set_body(self, body):
        if self._body != None:
            self._body._shapes.remove(self)
        if body != None:
            body._shapes.add(self)
            self._shapecontents.body = body._body
        else:
            self._shapecontents.body = None
        self._body = body
http://www.pymunk.org - A python library built on top of Chipmunk to let you easily get cool 2d physics in your python game/app
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Creating shapes without a body. [pymunk]

Post by slembcke »

I think you are hinting at something bigger that isn't possible. In order to do shape queries, the shapes you are querying against need to be added to the space. In order for a shape to be added to the space, it must be connected to a body.

Chipmunk doesn't explicitly support collisions without physics, but it's not hard to set up. Basically what you'll be doing is creating rogue bodies, and attaching the shapes to those. A rogue body is just a body that you don't add to the space so that you can control the way it's position and velocity are updated manually. Collisions still work normally. To avoid Chipmunk trying to apply collision forces between your rogue bodies, you should override the space's default begin callback to return false. That tells Chipmunk to ignore processing the collision further. Then make collision handlers that implement the pre-solve callback for any pairs of collision types you want events for. These callbacks must also return false to tell Chipmunk to ignore processing the collision forces.

So:
1) Create a body for each ship. Don't add the body to the space since you'll update it yourself.
2) Override the default "begin" collision handler to simply return false to avoid processing collision forces.
3) Add collision handler with a pre-solve for the colliding types you care about. It should also return false.
4) Each frame, update the position and velocity* of the rogue bodies. Then step the space to call the collision callbacks.

*Updating the velocity is optional, but it will give you slightly better performance if you do. The collision detection uses the velocity to estimate how long to cache collisions for.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
CylonOven
Posts: 2
Joined: Sat Apr 27, 2013 9:02 am
Contact:

Re: Creating shapes without a body. [pymunk]

Post by CylonOven »

Thanks for the answers. I think the changing the body method that viblo quoted might be what I'm looking for.

Not 100% sure of that though.

The model I had in mind was something like this:
#goal
To have a shipbuilder simulator where ship components of various shapes can be moved to connect (and disconnect) by flat edges to a large group of shapes. Then be able to 'export' the ship body with the shapes attached. So that the ship body has realistic mass and point of rotation/center of mass, depending on what's attached to it.
#Initial ideas + problems.
I initially wanted to have each component to be tracked by a vector. Then use vector math to figure out the offsets to attach to the ShipBody. But I hit the big snag where shapes not being able to be made without a starting body, nor being able to assign new bodys.
#current idea
If there is away to change the body of the component to the ShipBody without compromising its location then perhaps a better way would be:
The ShipBody has infinite Mass during construction. Component parts would each have their own relative masses. A legal connection is when a component is moved so a edge is flush with a shape on the Ship (I like this much more then having static 90deg connections. It allows for poly-shape components). If the player unclicks (or whatever) during a legal connection collision, the component's shape is attached to the Ship Body, and the components' mass would be added to the ship's. As well as the point of rotation being updated (Is there an easy way of doing that or is the only way to calculate the new point, compare to current, then move all shapes in the body to adjust?)

So the main problems now is: body switching and getting query for a 'flush' flat edge to edge collision.
offonoff
Posts: 28
Joined: Sun Nov 06, 2011 1:38 am
Contact:

Re: Creating shapes without a body. [pymunk]

Post by offonoff »

I'm not sold that the shape needs its own body to start with. If you insist you can use a shape with its own body while setting its position, then when it becomes part of the ship you remove the body and shape and recreate the shape on the ship body, using the relative position and angle of the old body to translate the shape into the coordinates of the ship's body.

Otherwise, I'd start the shape on the ship's body and use a shape query on the space to see if the selected shape has started to overlap other shapes on the ship. I'm a noob though, so take it with a grain of salt.
Post Reply

Who is online

Users browsing this forum: No registered users and 28 guests