Falling through?

Official forum for the Chipmunk2D Physics Library.
Post Reply
mitchellvriley
Posts: 4
Joined: Fri Jan 25, 2008 11:07 pm
Contact:

Falling through?

Post by mitchellvriley »

Hey!

I've just started trying to write a game in Ruby using Chipmunk. I've been trying to make a border around the screen, but at the moment my objects are falling through. Here's what I've got.

Code: Select all

    @space = CP::Space.new
    @space.gravity = CP::Vec2.new(0.0, 10.0)

    boundary = CP::Body.new(100.0, 100.0)
    boundary_shapes = [CP::Shape::Segment.new(boundary, 
                                              CP::Vec2.new(0.0, 0.0), 
                                              CP::Vec2.new(0.0, 480.0),
                                              3),
                       CP::Shape::Segment.new(boundary, 
                                              CP::Vec2.new(0.0, 480.0), 
                                              CP::Vec2.new(640.0, 480.0),
                                              3),
                       CP::Shape::Segment.new(boundary, 
                                              CP::Vec2.new(640.0, 480.0), 
                                              CP::Vec2.new(640.0, 0.0),
                                              3)]
    boundary_shapes.each do |shape|
      @space.add_shape(shape)
    end
    boundary.p = CP::Vec2.new(0.0, 0.0)
I haven't added the body, because isn't that how to make a static shape? What values do I use for the body's constructor? What do I use for the segments' widths?

I'm still pretty new to programming, so I could use all the help I can get.
Ostsol
Posts: 12
Joined: Sat Jan 26, 2008 9:16 pm
Contact:

Re: Falling through?

Post by Ostsol »

Static shapes still require a body. Look at the demo source code and/or the documentation and you'll see that static shapes use a body whose mass and moment of inertia are both "infinite". I am not familiar with the Ruby bindings, but under C static shapes should be added to the space via cpSpaceAddStaticShape, rather than cpSpaceAddShape. The body for static shapes should not be added to the space.
mitchellvriley
Posts: 4
Joined: Fri Jan 25, 2008 11:07 pm
Contact:

Re: Falling through?

Post by mitchellvriley »

Ok. I changed it to add_static_shape, replaced the 100's in the body's constructor with 1.0/0.0 to get infinity (Yuck!), and it works.

Thanks for your help!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Falling through?

Post by slembcke »

Unfortunately, Infinity is a very useful value, but there is no constant for it. :(
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
mitchellvriley
Posts: 4
Joined: Fri Jan 25, 2008 11:07 pm
Contact:

Re: Falling through?

Post by mitchellvriley »

I would like add the ability to throw objects around to my program. What's the best way to go about that? Create a small bounding box at the mouse click, check for intersections, then create a joint between that object and a temporary one which is moved to the mouse position at each step? (Phew!) Is there an easier way?

Also, my bodies are turning very quickly when they hit each other. I know that that can be fixed by increasing the "moment of inertia" on the bodies, but to stop that from happening I have to set it *really* high: 15000 or so. What would cause that?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Falling through?

Post by slembcke »

Use a "rubber band" force. Basically just take the difference from the object to the mouse, multiply it by some number and use that as the force on the object. Make sense? Creating an ad-hoc joint and body for the mouse would be a lot more trouble.

Don't Guess the moment of inertia!!! This has been discussed in other posts. The moment of inertia is a property of the distribution of the mass in an object. As such, it differs with the size, shape and mass of an object. Chipmunk has a few functions for calculating them (cpMomentForPoly etc.). Wikipedia has a good page with formulas as well.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
mitchellvriley
Posts: 4
Joined: Fri Jan 25, 2008 11:07 pm
Contact:

Re: Falling through?

Post by mitchellvriley »

Sorry! I should have checked.

How can I stop the object from oscillating back and forward when I pick it up?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Falling through?

Post by slembcke »

I suppose you could also just use the damped spring function. That gives you damping forces, and off center anchors for free. It works slightly differently, but the effect should be more or less the same. Not really sure why I didn't just suggest that in the first place...
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests