Picking up a box2d body

Forums Programming physics: Box2d, Chipmunk et al Picking up a box2d body

This topic contains 13 replies, has 4 voices, and was last updated by  utku 3 years, 9 months ago.

Viewing 14 posts - 1 through 14 (of 14 total)
Author Posts
Author Posts
July 2, 2010 at 1:37 pm #222911

steg90
@steg90

Hi,

I want to allow the player sprite in my game that when they touch a box2d object, it will follow them, what is a good way to do this? Is it just a matter of detecting the body you have collided with and then set this body to just be the player sprites body position?

Regards,

Steve

July 5, 2010 at 2:18 am #289094

rickms
Participant
@rickms

I believe the Mouse Joint is what you want to look into, however as per the Box2d Manual

“Many users have tried to adapt the mouse joint for game play. Users often want to achieve precise positioning and instantaneous response. The mouse joint doesn’t work very well in that context. You may wish to consider using kinematic bodies instead.”

Rick

July 6, 2010 at 5:02 pm #289095

steg90
@steg90

Mmmm, interesting.

Could I not just make the body be the same position as the player with some offsets?

Regards,

Steve

July 6, 2010 at 5:43 pm #289096

rickms
Participant
@rickms

Manually moving a box2d body can potentially mess up the physics (ie moving a body into another body). Depending on what you are doing you can probably ->SetTransform() on the body to reposition it, but note the API states:

“This breaks any contacts and wakes the other bodies. Manipulating a body’s transform may cause non-physical behavior.”

July 6, 2010 at 6:20 pm #289097

steg90
@steg90

Yep, I’m aware of that, I was just thinking incorrectly, thought I could move it without using SetTransform() but can’t.

Guess I need to use a mouse joint that you mentioned, never used one before, guess there are some examples some place, I’ll take a look on here.

Thanks for you advice, I appreciate it.

Steve

July 6, 2010 at 6:28 pm #289098

steg90
@steg90

Looks like all the box2d stuff in the box2dtestbed use mouse joints so this should help.

July 6, 2010 at 8:34 pm #289099

utku
@utku

you should use either mouse joint or a kinematic body, mouse joint can give the feeling of not being precise or delayed, Erin recommends using kinetic bodies. in presolve phase you can calculate the velocity needed to move the body to the desired place since you will know the delta time, after the solution step set linear velocity to zero.

July 6, 2010 at 9:30 pm #289100

steg90
@steg90

Thanks utku,

Although I’m very unclear how to use kinemetic body method you talk about. I want the body to basically follow the player with a slight offset, kinda like walking a dog if you know what I mean?

I’m taking it for kinemetic bodies, you set the velocity and these then get moved in presolve?

Regards,

Steve

July 6, 2010 at 10:08 pm #289101

utku
@utku

after calculating and setting velocity, you need to call Step method of the world to actually move the kinematic body. Here is a simple Step method you call using a timer.

b2Vec2 mouseV = moveDistance;
mouseV *= (1/dt);
body->SetLinearVelocity(mouseV);
world.Step(dt);
body->SetLinearVelocity(b2Vec2_zero);

July 7, 2010 at 1:45 pm #289102

steg90
@steg90

Many thanks utKu for your help.

Regards,

Steve

July 7, 2010 at 7:15 pm #289103

indy2005
@indy2005

Hi,

Would this linear velocity just get added to the exiting velocity? I guess the linear velocity being reset to zero deals with this.

Regards

i

July 7, 2010 at 8:10 pm #289104

utku
@utku

you should not add the calculated mouseV to existing velocity if you want it to move as much as the mouse precisely, but you can assign existing linearVelocity to another temporary variable before setting mouseV, and assign it back as linearVelocity after Step(dt) call.

July 8, 2010 at 7:14 am #289105

indy2005
@indy2005

Hi,

Thx. Have I read somewhere that setting linear velocity manually can disrupt the simulation?

Regards

i

July 8, 2010 at 4:55 pm #289106

utku
@utku

yes since the kinematic body have infinite mass it can push anything with a great force, even violate constraints, but using a mouse joint is not entirely safe either, since it uses a really stiff spring, it too can apply massive forces to other actors in the world. Ideal solution is using forces only, however if you need kinematic or static body it means you want to use something that does not effect by forces in the world, so you have to move them using velocity, or you will have to use constraints on a dynamic body, but since box2d and chipmunk are iterative solver they probably will not look fixed enough in that case.

Viewing 14 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic.