Move my body to a point - cocos2d-iphone

I am using box 2d. I just want to move my body to the point. What is the best way to do this?

When you say you just want to move the body, do you mean you want to apply a force to get the body to a point?
There's a joint designed for mouse movement, and it might work well in your case if you want to drag bodies around on an iPhone. It's called the Mouse Joint, and it's under 8.10 on these box2d docs.
If you want a body that doesn't respond to things hitting it, but pushes things around based on where it is and where it is going, go for the b2_kinematicBody on the same docs
Hope it helps. Your question is very vague.
EDIT in response to comment:
Well, generically the way to do this would be cpBodyApplyForce or cpBodyApplyImpulse. There are many ways to use this to move the body to a position, and they can get more complex than I can summarize in a comment. Essentially, you're getting into stuff that can be better covered by game AI programming sources.
The most basic way would be to apply a force that is some multiple (on each axis) of the distance from the object to the target position. If you want the object to slowly stop, the search terms "AI arrive behavior" might be a good idea. I found this discussion on gamedev.net.

Related

Working with touches on a zoomed layer

I have a CCLayerPanZoom subclass. For testing purposes I put a sprite in the middle of the screen. I do the following test before and after zooming:
if(CGRectContainsPoint(mySprite.boundingBox,touchedPoint))
NSLog(#"Touch inside the sprite");
Before zooming I get "Touch inside the sprite" output, but when it's zoomed I don't. I have asked a similar question (Getting different x and y coordinates for touched location on a zoomed view) and figured it out later. But the solution I found works only for when panning, not for zooming. There're lots of games out there and this must be super simple (at least done prior to me). I need to be able to have the touched point the same as it was before zooming.
I regret all the wasted time to find a way out from this problem. It turns out that there's a neatly packed method called convertToNodeSpace in Cocos2D that lifts all the heavy weight. No need to make all those crazy calculations to take the panned distance and zoomed scale into account. I just did the following and everything worked like a magic
touchedPoint=[self convertToNodeSpace: touchedPoint];
where self is a subclass of CCLayerPanZoom which is a subclass of CCLayer. That's all. Hope this helps someone.

Box2d - how to grab and throw objects

Say there are 3 boxes on the screen, how can I go about touching one of them to pick it up and "throw" it at the others? I have the rest of the world implemented but can't find much information on how to grab/drag/toss physics objects. Any sample code or documentation out there that would help with this?
It depends what you are attempting to do. It is a physics simulation and as such a typical way of interacting with the system is by applying forces to objects opposed to direct manipulation of the x,y coordinates. But you can in fact do either. I believe the most common approach is to use a mouse joint. A google search on b2MouseJoint will show the documentation and several examples including this one.
http://muhammedalee.wordpress.com/tag/b2mousejoint/

How to make box2d mouse joint works similar to box2d setposition

I want to make b2mouse joint working similar to b2setposition, though i know in mouse joint force is applied, so it's not possible to reach the desired point without any delay like setPosition(), but i want to make it works as close as b2setPosition(). So on which mousejoint/body properties should i work on so that it acts as close as b2setposition.
Thanks for your answer.
According to the Box2D API Reference on b2MouseJoint:
NOTE: this joint is not documented in the manual because it was
developed to be used in the testbed. If you want to learn how to use
the mouse joint, look at the testbed.
There's no "b2setposition". There's b2Position which is an internal class, or you meant b2Body->SetTransform() which sets the position of a body.
If you could explain better what you're trying to do and why it has to be a b2Mouse joint, I might be able to help more.

Use Chipmunk body location to drive Cocos2D Parallax

I'm using Chipmunk with SpaceManager inside of Cocos2D. I have a body which is moving around with impulses, I'd like to be able to use that bodies location to drive a parallax node in Cocos2D.
So, when body 'a' moves to the left, the parallax follows but with a small time offset, or elastic effect. If you know AS3 and Flash, you can think of this as using a movieclip's x and y location to drive a constantly updating tween with an ease out effect. This is what I'd like to achieve, and after no sleep trying to work it out last night I've decided to ask you much more experienced people :)
Thanks.
You could associate an additional body with the parallax node, and attach it to body a with a suitable joint or set of joints. It is difficult to tell from the question what exact behaviour you are looking for, but at least one of slide, groove or damped-spring constraint should do the trick.
I worked this out, for future ref in case others need it you use:
[voidNode setPosition:(yourShape.position)];

How to implement moving platforms with Cocos2d, TMXTiledMaps and Chipmunk

I'm making slow but steady progress with a Cocos2d game, but I'm stuck creating moving platforms.
The main character needs physics and collision detection, and is therefore a chipmunk shape/body. I wrote a class to iterate over the TMXTiledMap in order to cut back on the amount of bodies in the chipmunk space. So with a map like this
----------
--------x-
-xxx----x-
----------
instead of having 5 individual bodies (rects), there are two bodies, One is three tiles wide, the other is two tiles tall.
I've managed to get the code working to identify which tiles are part of a moving platform and to move the tiles as needed.
However, the bodies need to move with the tiles in order for this to work properly. And this is where I'm stuck. The bodies are of a static mass so...
platformShape->body->p = cpv(x,y);
Doesn't do anything (I'm guessing that this is the expected behavior).
But if I set their mass to anything other than static, all the physics comes into play and the bodies do not behave as expected, or they behave perfectly depending on you how you look at it. They move erratically and the rotate when they hit another body (eg: the main character). What I'm after is the typical type of moving platform you would expect to find in a typical platform game that moves smoothly in any given direction.
My question is; Has anyone implemented something like this before and what was your technique? Or, if you were to implement something like this, how would you do it?
The relevant code is here. I put it in a pastebin since I figure it's more of a conceptual misunderstanding than anything else.
It turns out you need to call
cpRehashStaticShapes
Obvious really, but easy to miss in my opinion.