Move Object (Body) to Location

Official forum for the Chipmunk2D Physics Library.
mypi
Posts: 1
Joined: Thu Aug 18, 2011 7:09 am
Contact:

Move Object (Body) to Location

Post by mypi »

Hi there!

I'm currently developing my first game and i'm new to both cocos2d and Chipmunk. I've followed this tutorial: http://www.raywenderlich.com/3128/how-t ... s-tutorial, which worked great. For my game, I want to have an object (cpBody) that will move to the X-coordinate of where I touch.

Code: Select all

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    CGPoint touchLocation = [self convertTouchToNodeSpace:touch];    
    [smack movements:(touchLocation)];
    return YES;
}

Code: Select all

- (void)movements:(CGPoint)location {
}
I don't know which functions to use in order to get my object (smack) to the position touched on the screen. I've tried cpBodySlew, cpBodyApplyForce, cpBodyApplyImpulse and cpBodyUpdateVelocity but I can't get it to work. Sometimes the object just moves a little bit, and sometimes the object moves way to far! Can someone help me?

Thanks in advance,
Bobramyl
Posts: 16
Joined: Sun Nov 07, 2010 3:02 pm
Contact:

Re: Move Object (Body) to Location

Post by Bobramyl »

I think that there are 2 possible ways, but you forgot to mention one crutial thing - does the object have to move, or it will simply teleport to location?

Assuming it will teleport, its very easy:

Code: Select all

 CGPoint whereToMove = CGPointMake(myTouch.x, myTouch.y);
 body.pos = cpv(whereToMove.x,whereToMove.y);
Assuming it will have to move all the way until it reaches end, that's somehow trickier. I actually don't know formula for that (sorry, maybe some smart person will), but from point of view of the programmer, i'd do following:

1. calculate distance between center of body and myTouch - using pythagoras

Code: Select all

length = sqrt((myTouch.x - body.x)^2 + (myTouch.y - body.y)^2)
2. apply impulse of force F into body, using vector body->myTouch

Code: Select all

cpv vector = cpvnormalise(myTouch.x - body.x, myTouch.y - body.y));
vector = cpv(vector.x * F, vector.y * F);
-> use method to apply impulse to body (cpBodyApplyImpulse(cpBody *body, cpVect j, cpVect r) or applyImpulse in Obj-C, profit
Now force F is what I'm missing, I don't know how to calculate that.. Assuming you have weight of body, length, you can calculate that for sure :) Happy googling, hope it will help.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Move Object (Body) to Location

Post by slembcke »

Basically yes, if you want it to "teleport" just set it's postion.

It sounds like you want to smoothly move the object over time though. To do that, you can set the velocity of the object each frame to make it move to the new place. There a number of ways you can do this depending on the specific behavior you are after.

You can also use a fancy pivot joint to move the object to the new position and stop it there. You would have check the position of the object to remove the joint when it arrives at the destination though.

Code: Select all

cpConstraint *joint = cpPivotJointNew2(body, staticBody, cpvzero, targetPoint);
cpConstraintSetMaxBias(joint, movementSpeed);
cpConstraintSetMaxForce(joint, maxForce);

cpSpaceAddConstraint(space, joint);
@Bobramyl
Just FYI, on iOS and OSX cpVect is equivalent to CGPoint. So no need to convert them. Also, there are a bunch of vector helper functions like cpvdist(pos1, pos2) that you can take advantage of.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
markhula
Posts: 188
Joined: Wed Feb 02, 2011 4:23 am
Contact:

Re: Move Object (Body) to Location

Post by markhula »

Hey Scott,

I thought "teleporting" was bad; and certainly shouldn't be done on many objects on a per frame basis???

Cheers

Mark
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Move Object (Body) to Location

Post by slembcke »

Teleporting is generally fine as long as you only do it once in a while. You probably want to be careful that you don't teleport it into a wall or another object.

Changing the position every frame is bad because it doesn't allow the physics to do what it's supposed to. Setting the velocity explicitly each frame like velocity = constantValue is also sort of bad.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
usandhar
Posts: 18
Joined: Thu Jun 13, 2013 4:56 pm
Contact:

Re: Move Object (Body) to Location

Post by usandhar »

Hi Guys - Are there any updates to "how to smoothly move the object over time to a touched point", especially, if I were doing it using Obj-Chipmunk? Thanks for your help.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Move Object (Body) to Location

Post by slembcke »

Yes, there are many ways to do it. Use force to accelerate the object over time, make small velocity changes over time, use a constraint to do it. You don't need to change the position of an object explicitly or constantly overwrite the velocity of an object to do that.

I usually pick the last way, and that is described in the the following tutorial:
http://chipmunk-physics.net/tutorials/ChipmunkTileDemo/
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
usandhar
Posts: 18
Joined: Thu Jun 13, 2013 4:56 pm
Contact:

Re: Move Object (Body) to Location

Post by usandhar »

Thanks Scott. I tried it and it works great. Two follow-up Qs:
1) How can I remove ChipmunkPivotJoint to several ChipmunkBody objects? I tried [targetPointBody removeFromSpace: space] but that doesn't work. Think of this as balloons getting attracted to touch but as soon as the touch is removed they start falling to the ground.

2) How can I have different ChipmunkBody objects move smoothly towards touch point and stop at different lengths from the touch point as opposed to all ChipmunkBody objects trying to get as close as possible to the touch point?

Thanks in advance for your help.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Move Object (Body) to Location

Post by slembcke »

First of all, you should use the [ChipmunkSpace add:] and [ChipmunkSpace remove:] methods for adding and removing everything. You can use the more specific methods, but those really only exist to implement the ChipmunkBaseObject protocol (which you don't really need to care about).

If you want to remove multiple objects at once, you need to create an object that implements the ChipmunkObject protocol. All it needs is a chipmunkObjects method or property that holds an array of all the things to add or remove.

If you don't want all of the objects to converge on the same point, then just make a separate joint for each and give them different coordinates.

Also, the forces produced by constraints might look weird for a balloon. A simple force that pushes the balloon towards the point and a drag force to keep it from moving too fast might work better.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
usandhar
Posts: 18
Joined: Thu Jun 13, 2013 4:56 pm
Contact:

Re: Move Object (Body) to Location

Post by usandhar »

Thanks Scott. Do you have an example of ChipmunkBaseObject protocol? I added separate joint for each bodies and converge bodies to their respective point on touch. Right now, I converge all joints to the same point (although I will change this soon to go to different co-ordinates). On touchesbegan, I add the target points and joints and reposition. On touchmoved, I move all target points and on touchended I remove all touch points and joints so the bodies can free fall or float around. When I touch the screen for the first time it works well - the bodies converge well and when I let go the touch the bodies fall down due to gravity setting. However, when I retouch the converge doesn't work as well. Could you please point me to what might be going wrong?

Thanks.
Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests