Page 1 of 1

Falling through?

Posted: Fri Jan 25, 2008 11:18 pm
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.

Re: Falling through?

Posted: Sat Jan 26, 2008 9:23 pm
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.

Re: Falling through?

Posted: Sun Jan 27, 2008 6:38 pm
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!

Re: Falling through?

Posted: Sun Jan 27, 2008 8:14 pm
by slembcke
Unfortunately, Infinity is a very useful value, but there is no constant for it. :(

Re: Falling through?

Posted: Mon Jan 28, 2008 2:07 am
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?

Re: Falling through?

Posted: Mon Jan 28, 2008 12:04 pm
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.

Re: Falling through?

Posted: Fri Feb 01, 2008 5:21 am
by mitchellvriley
Sorry! I should have checked.

How can I stop the object from oscillating back and forward when I pick it up?

Re: Falling through?

Posted: Fri Feb 01, 2008 11:29 am
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...