Any type of MoveTo function?

Official forum for the Chipmunk2D Physics Library.
korki696
Posts: 24
Joined: Wed Jul 15, 2009 2:15 am
Contact:

Any type of MoveTo function?

Post by korki696 »

Hey all
Im making an iphone game using cocos2d and possibly chipmunk but my problem is, is there any type of MoveTo function because i want to be able to click and select a sprite and then be able to drag it but i dont want it to go with the touch but follow it.

My other question was that i already have all my touch controls setup how i need them so that they work. I dont want to have to redo everything. I have a total of three controlable sprites. The way i have it setup now is that when you click a sprite it becomes selected and you can move it around. If you double tap anywhere they all become selected and follow. Problem is that they walk throught eachother and i dont want that. I want them to have to walk around eachother. I do have collision detection setup but all it will do is tell me if two touched. Any idea on how i could easily change my code to get this to work?
Thanks
-Lars
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Any type of MoveTo function?

Post by slembcke »

Not directly no, but there are a few methods you could choose that would give you the same effect.
1) Use forces to move the object
2) Blend the velocity of the body to get it to move in desired direction.
3) Use a soft joint to get the body to move to the desired position.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
korki696
Posts: 24
Joined: Wed Jul 15, 2009 2:15 am
Contact:

Re: Any type of MoveTo function?

Post by korki696 »

Hey Slembcke
Thanks for the reply. I was wondering is there a way to code it to do that? since there is a position property and all moveto is really doing is taking the ccp position of the sprite and the CGPoint position of the finger and then moving it over time. Would it be possible to do something like that? Im fairly new to chipmunk but since you have a position property i thought maybe it could be done?
Thanks
-Lars
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Any type of MoveTo function?

Post by slembcke »

You don't really want to just set the position property because that will change it's position without updating it's velocity. Blending the velocity is probably the most straightforward. Lerp partway from the current position to the final position, figure out the velocity that would make it move there in the next timestep, then lerp some of that velocity with the body's current velocity.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
korki696
Posts: 24
Joined: Wed Jul 15, 2009 2:15 am
Contact:

Re: Any type of MoveTo function?

Post by korki696 »

Hey slembcke
I thought about it a little and was wondering what advantages velocity will actually give me? It seems like just changin the position would be a lot easier.
Thanks
-Lars
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Any type of MoveTo function?

Post by slembcke »

Because Chipmunk is a physics engine, velocity means everything. Velocities are what Chipmunk solves for. While it also tries to fix positional overlaps, it doesn't do it as well and you'll run into some serious problems with object going right through each other or just looking squishy.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
korki696
Posts: 24
Joined: Wed Jul 15, 2009 2:15 am
Contact:

Re: Any type of MoveTo function?

Post by korki696 »

Ok so i have a question about this lerp method. currently whats happening is that when you tap and drag the sprite follows smoothly behind it continuously updating its direction and rotation. will this lerp method give me that?
Thanks
-Lars
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Any type of MoveTo function?

Post by slembcke »

You could also make it update the direction as well, but that would be extra code.

To be honest it doesn't really sound like you want a physics engine. Whenever people try to use Chipmunk just because it implements collision detection, they have a lot of difficulty getting the physics to also do what they want.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
korki696
Posts: 24
Joined: Wed Jul 15, 2009 2:15 am
Contact:

Re: Any type of MoveTo function?

Post by korki696 »

Ok well i guess what im having problems with is collision response because i have it setup to detect a collision. i just need it so it wonot go through the objects and thats what im having trouble with
-Lars
clint
Posts: 11
Joined: Mon May 11, 2009 11:55 am
Contact:

Re: Any type of MoveTo function?

Post by clint »

For what it's worth, I'm also using cocos2d and Chipmunk and had to solve a similar problem. In my case, there are several Chipmunk bodies displayed on the screen as cocos2d sprites. The user can tap and drag the sprites, and therefore the Chipmunk bodies, too. At each game step iteration I use the cpBodySlew() function to move the sprite/body and keep it in sync with the user's finger.

Perhaps there's a better way to do it, but this works for me.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Heise IT-Markt [Crawler] and 18 guests