Dragging Objects Differently (half physics?)

Official forum for the Chipmunk2D Physics Library.
Post Reply
tolmasky
Posts: 10
Joined: Wed Nov 16, 2011 4:52 am
Contact:

Dragging Objects Differently (half physics?)

Post by tolmasky »

So I've seen some examples online on how to drag and move objects interactively (either through the mouse or touch), but all of these do it through constraints and such, giving the objects they move velocities, and forces and so forth. This is great for a lot of applications, but in my particular case I want something slightly different. I am still new to Chipmunk so my apologies if this question has an obvious answer or takes too long to explain:

To make my requirements slightly easier to understand, allow me to give an example. Let's pretend I am writing a hockey game. In this game there is a puck as viewed from above (represented as a circle shape and associated body) and a stick (rectangle shape to make things easy). When the user click-drags, the stick appears under the pointer and it tracks his movement exactly (Algorithmically speaking, I am just updating the .pos of the stick's body on every mouse move -- in other words, no delay as the mouse moves around).

Now, if this stick shape/body comes into contact with the puck, the puck should feel the "force" of the stick. In other words, dragging quickly should hit the puck stronger than dragging slowly. HOWEVER, I don't want the stick itself to have velocity because if the user lets go it would seem really weird for the stick to go flying all over the place. Iwould want it to stand still. More importantly, as the user moves the stick around, I don't want it to be rotating all over the place due to its inertia and where the user decided to pick it up (center vs. edges).

To expand on this, two sticks may appear at once (say this is a two player game), so the interactions should otherwise continue as expected.

I have tried achieving the above by simply updating the .pos's of the sticks, but the results are not really what I'd expect -- namely I don't think chipmunk is "inferring" the force of the stick from how fast the user is moving it around (expected I suppose), but I was wondering if someone knew of a way to accomplish this -- it doesn't seem like its too strange a use case.

Thanks!
tolmasky
Posts: 10
Joined: Wed Nov 16, 2011 4:52 am
Contact:

Re: Dragging Objects Differently (half physics?)

Post by tolmasky »

I've come up for a partial solution to what I want to do, but could use help understanding the second porition. So, if on every mouse move I store the next position:

onMouseMove()
{
nextPos = mouseLoc;
}


In the step function I can then do:

body.vel = (nextPos - body.pos) / dtl <-- basically what cpBodySlew used to do I imagine.
step(dt)
body.vel = 0;

This actually is behaving very similarly to how I want, However, my stick still spins in response to movement and hitting things. Is there any way to make the stick "rigid"? By changing its moment perhaps?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Dragging Objects Differently (half physics?)

Post by slembcke »

Ah, yes all you need to do is make sure to update both the velocity and the position. If you just update the position, you are telling Chipmunk that the object is "teleporting" from place to place each frame and not moving smoothly between them.

If you set the stick's moment to INFINITY when you create it's body, it won't be able to spin.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
tolmasky
Posts: 10
Joined: Wed Nov 16, 2011 4:52 am
Contact:

Re: Dragging Objects Differently (half physics?)

Post by tolmasky »

So is this the way you would recommend accomplishing what I want, or should I in fact be using some sort of joint magic?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Dragging Objects Differently (half physics?)

Post by slembcke »

Hmm. Depends. I guess I prefer the joint magic myself as it can help avoid some stability issues that come up from setting the position/velocity of an object every frame. If you don't notice any issues, go with what you have I guess. The joint way isn't much more complicated, but it's might not be very obvious how it works.

The magic would basically work the same as the mouse control code. You'd have a rogue mouse body that you update the position and velocity of that instead. Then you have a joint with a max force set on it that connects the mouse body to the object you want to drag around. When the mouse body is moved, the joint correction will pull your object towards the mouse body while respecting the force limit set.

The reason why this works a little better is because the joint is limited by the force you tell it, and that force is calculated as part of the solver. When you change the velocity of a body manually, the "force" that it applies is basically proportional to the size of the timestep you are using and how much the velocity changed. This can end up creating really huge forces, and because you are setting the position explicitly you can end up smashing through other objects.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
tolmasky
Posts: 10
Joined: Wed Nov 16, 2011 4:52 am
Contact:

Re: Dragging Objects Differently (half physics?)

Post by tolmasky »

Is there any way to give an object a max force similarly to a joint to avoid that possible situation?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Dragging Objects Differently (half physics?)

Post by slembcke »

No, the issue is that the force that occurs from changing a body's velocity is implicit. You could calculate the maximum velocity change for a given timestep by hand, but you might as well just use a joint in the solver then. It would be easier.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests