5

I am brand new to game development and I thought it would be fun to try out Gosu, Ruby and Chipmunk together. So far I have a player and a ball on a 2d Top-Down field and they collide. My problem right now is that I want my player to be a square.
I sucked at Math in high school and that was way too many years ago to think about. The documentation for Chipmunk says this about drawing a Polygon:

# Create a poly collision shape attached to the given body at
# the given offset with the given vertexes. _verts_ must be an
# Array of CP::Vect with a counterclockwise winding.
 def initialize(body, verts, offset); end

I have this code to get my polygon right now:

verts = Array.new
verts.push(CP::Vec2.new(0,32))
verts.push(CP::Vec2.new(32,32))
verts.push(CP::Vec2.new(32,0))
verts.push(CP::Vec2.new(0,0))

@shape = CP::Shape::Poly.new(@body, verts, CP::Vec2.new(0,0))

That ends up giving me a shape attached to my square png, but it appears as if 0,0 is the top, left corner of the image and the actual Polygon starts even farther to the top, left of the image.

So I assumed I should set the "Offset" to half of my width of my Polygon but that doesn't give me the right outcome.

I am sure that for most people on here this is elementary. But I am still in the elementary when it comes to game dev.
Can someone explain this to me please?

EDIT
Here is my full code as of 5/28/2011

James P. Wright
  • 8,991
  • 23
  • 79
  • 142
  • So....no one on the Chipmunk forums or on StackOverflow has any idea how to draw a square of 32x32 pixels with Chipmunk? That's...awkward... – James P. Wright Apr 17 '11 at 18:11
  • I was playing with your code a little bit. The Poly verts seems ok. I'm kind of worried about the body center. Did you try to make the Ball a square too ? It will be much easier to test with two squares since the Circle collisions will behave weird. I'll keep trying a couple of things and see if we can make it work properly. – Roberto Decurnex May 30 '11 at 20:48

2 Answers2

5

Looks like your problems stem from inconsistencies between drawing and collision origins.

In effect, your ball is drawn centered on x,y, while the player is drawn with top left corner on x,y. That coupled with your Player shape not being centered on x,y is causing you difficulties.

To center the drawing of your player, just use the draw_rot method with an angle of 0 to avoid rotating the image.

class Player
  def draw
    @image.draw_rot(@shape.body.pos.x, @shape.body.pos.y, 1, 0, 0.5, 0.5, 1, 1)
  end
end

To make the Chipmunk shape fit the centered player image, you need to offset your vertices by half the size of the shape. You can hardcode this doing

class Player
  def initialize(space, image) 
    ...
    verts = Array.new
    verts.push(CP::Vec2.new(-16,16))
    verts.push(CP::Vec2.new(16,16))
    verts.push(CP::Vec2.new(16,-16))
    verts.push(CP::Vec2.new(-16,16))
    ...
  end
end

or simply do it at runtime with

CP::recenter_poly(verts)

before you add verts to your shape.

With those changes in place I suspect your physics will behave more like you expect.

Jakob S
  • 19,575
  • 3
  • 40
  • 38
  • That isn't quite what I'm looking for, but it does work better. I can't use the CP.recenter_poly(verts) method call though, says the method doesn't exist. But still, this at least leads me in a better direction. Thank you! – James P. Wright Jun 04 '11 at 16:46
  • That should've been CP::recenter_poly(verts), sorry. Updating answer. – Jakob S Jun 04 '11 at 18:55
  • I actually tried that as well (on the assumption that you typed it wrong) and it gave the same error. – James P. Wright Jun 06 '11 at 01:33
  • It exists in Chipmunk gem 5.3.4.4, see the documentation at http://beoran.github.com/chipmunk/#Shape - and thanks for accepting, good luck with your game – Jakob S Jun 06 '11 at 07:44
2

If you want a 32x32 square which has its top left corner at (0,0), your coordinates should read: (0,32); (32,32); (32,0); (0,0). Or is it 31 instead of 32? For Chipmunk polygon definitions, pen & paper come in handy.

It's worth pointing out that Chipmunk has absolutely no idea about rendering- all it does are abstract physics calculations. So what you want here is to define a square. The drawing is done in Gosu and detached from the Chipmunk shapes, as you have painfully experienced :) Makes me think a pre-made library to visualize chipmunk shapes in Gosu would be nice, but I am not a Chipmunk user myself.

  • I just got back around to playing with this and tried your solution...and it doesn't work. Thanks for trying to help. I'm uploading my code right now to see if I can get more detailed help. – James P. Wright May 29 '11 at 00:35