3

I've searched extensively on how to make a Box2d body move along a Bezier curve or arc-like path with a start point, end point and possibly another control point. I know cocos2d objects can be moved around using ccBezier but how can it be done for Box2d bodies? Please help. Thanks in advance.

allenalex
  • 41
  • 2
  • You can use SetTransform to set the position of a b2Body. Update it as often as necessary. You could also use SetLinearVelocity to make the body move towards the desired point. – iforce2d Sep 05 '12 at 00:32
  • @iforce2d: I am not sure how to set linear velocity to make the body go up and come down (in an arc-like/ Bezier-like movement). I hope I'll not be asking too much of you to show me an example. Thanks. – allenalex Sep 05 '12 at 07:49
  • The source code here might be useful: http://www.iforce2d.net/b2dtut/sticky-projectiles (The kinematic body moving up and down on the right hand side is moving along a sine curve in 1d, you could change the target position to be on your bezier curve) – iforce2d Sep 05 '12 at 10:14
  • @iforce2d: thanks. The Box2d code is a bit advanced, I'm trying to wrap my head around how it works. I also saw your tutorial on [Projected Trajectories](http://www.iforce2d.net/b2dtut/projected-trajectory). I like the projectile and believe something like that would work better for me, that is once I understand how to re-implement it in my code. What do you say? – allenalex Sep 05 '12 at 17:05
  • I say you start with a simpler curve like sine. The point will be (x, sin(x)) and you just keep incrementing x a little bit every frame to get a new point. Then set the linear velocity of the body so that it moves from its current position, towards the point. – iforce2d Sep 05 '12 at 23:19
  • 2
    this one is helpful http://gamedev.stackexchange.com/questions/35553/making-a-box2d-body-move-along-a-bezier-curve-arc-path – shaqir saiyed Jul 12 '13 at 12:42

1 Answers1

1

I have tried to move a box2d body using a curve (making a spider walk around an asteroid). I found that using SetTransform every frame worked, but it made the collision response in the engine fail.

I was able to find a much better solution by using prismatic joints. You create a joint starting at the position (p0) you want to start from and pointed towards the next waypoint (p1). Then you let the joint push the body from p0 to p1 using the motor. When the body gets very close to the next point, destroy the joint and form a new one for the next two points. The body will still have its velocity so the motion looks smooth.

I put a more complete post on this on Stack Overflow here.

Was this helpful?

Community
  • 1
  • 1
FuzzyBunnySlippers
  • 3,387
  • 2
  • 18
  • 28