I'm trying to build a complex object, something like slembcke talks about here:
http://www.slembcke.net/forums/viewtopi ... 319&p=1476
The problem I'm facing right now is more about the purpose of the joints. So... I made a simpler version of the buggie, which is just a big box over a "chassis" and the wheels, something like this:
I've connected the bix box to the "chassis" with a pin joint and the wheels to the chassis using a pivot joint. The problem is that apparently wheels are not rotating at all and the black box is not "glued" to the brown box... So, what would be the best way to join everything? Does the pin joint allows to rotate an object?
Here's the code I'm using: (ruby bindings, using cocos2d-iphone)
Code: Select all
cpvzero = CP::Vec2.new(0.0,0.0)
verts = [cpvzero, CP::Vec2.new(0.0,36.0), CP::Vec2.new(32.0,36.0), CP::Vec2.new(32.0,0.0)]
body1 = CP::Body.new(10, CP.moment_for_poly(10, verts, cpvzero))
shape1 = CP::Shape::Poly.new(body1, verts, cpvzero)
# the brown box
verts = [cpvzero, CP::Vec2.new(0.0,7.0), CP::Vec2.new(52.0,7.0), CP::Vec2.new(52.0,0.0)]
body2 = CP::Body.new(10, CP.moment_for_poly(10, verts, cpvzero))
shape2 = CP::Shape::Poly.new(body2, verts, cpvzero)
# the wheels
body_wheel1 = CP::Body.new(3, CP.moment_for_circle(3, 7, 0, cpvzero))
shape_wheel1 = CP::Shape::Circle.new(body_wheel1, 7, cpvzero)
body_wheel2 = CP::Body.new(3, CP.moment_for_circle(3, 7, 0, cpvzero))
shape_wheel2 = CP::Shape::Circle.new(body_wheel2, 7, cpvzero)
# position each body (@st_x and @st_y are 240, 160 respectively)
body1.p = CP::Vec2.new(@st_x - 0.0, @st_y - 0.0)
body2.p = CP::Vec2.new(@st_x - 0.0, @st_y - 21.5)
body_wheel1.p = CP::Vec2.new(@st_x - 25.0, @st_y - 21.5)
body_wheel2.p = CP::Vec2.new(@st_x + 25.0, @st_y - 21.5)
# create the joints
j1 = CP::Joint::Pin.new(body1, body2, cpvzero, cpvzero)
j2 = CP::Joint::Pivot.new(body2, body_wheel1, cpvzero)
j3 = CP::Joint::Pivot.new(body2, body_wheel2, cpvzero)
# add all bodies/shapes/joints to the space
[body1, body2, body_wheel1, body_wheel2].each { |b| $space.add_body(b) }
[shape1, shape2, shape_wheel1, shape_wheel2].each { |s| $space.add_shape(s) }
[j1, j2, j3].each { |j| $space.add_joint(j) }
# attach each body to the corresponding sprite
child_with_tag(1).attach_chipmunk_shape(shape1)
child_with_tag(2).attach_chipmunk_shape(shape2)
child_with_tag(3).attach_chipmunk_shape(shape_wheel1)
child_with_tag(4).attach_chipmunk_shape(shape_wheel2)
http://www.youtube.com/watch?v=Bi_KGaGRBPA
... What could be the problem here? Should I use another type of joints?
thanks!
rolando./