Use Chipmunk body location to drive Cocos2D Parallax - cocos2d-iphone

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)];

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 transfer remaining impulse from one body to another

after applying to a Box2D body:
b2Vec2 force = b2Vec2(velocity.x/PTM_RATIO, velocity.y/PTM_RATIO);
_body->ApplyLinearImpulse(force,_body->GetPosition());
I'm trying as in many game (like doodle jump) to stop moving the hero body once it reaches a certain distance from the top of the screen and start scrolling the stage so that we feel the hero is still climbing higher. For this I need to move the hero in the first place and then move the stage.
How can this be achieved correctly ? Any idea?
Thats quite an odd thinking to solve the problem. You never stop the character or any of the environment objects. Let them behave as they are intended by box2d. You have everything added to one root node of some sort, your environment and the character. What you do is create some sort of a "camera controller" and you give the characters CCSprite, or the wrapping objects if you have one, as a target. In the update function that you call each frame you change the root nodes position in a way that it centres the screen on the character. You can implement follow delays, smooth scrolling and other nice features as you need them.
Unfortunately I don't use Cocos2d at all, so I can't give you a sample code. The given solution will work for Cocos2d as it is not far of the engine I use at my work place.

Can box2d track my Cocos2d actions of sprites?

I just started using Cocos2D this week. While playing around with Box2d i was wondering if it was possible to move CCSprites with the help of CCActions and use box2ds collisiion detection feature to detect collision between those sprites..
I'm pretty sure this must be possible?
If you don't need real physics behavior, I'd highly recommend to "manually" deal with your collision logic. That said, for your scenario I would start with this approach.-
Create one body per sprite, and assign each sprite to the user data.
Your 'static' scenario would map to static bodies (i.e floor, platforms, etc...)
Your 'dynamic' sprites would map to dynamic bodies, which only fixture would be marked as sensor
You'd register a b2ContactListener to listen for the collisions.
As for the tricky part, you'd need to set in each iteration of the main loop, the position of each body to the position of each sprite (of course, translating pixels to meters), in order to avoid that they just behave as physics bodies. You could try just to not calling world->step, but not sure if contactListener would work then.
Hope it helps!

how to prevent tunneling on sensor objects in Box2D

I'm making an ipad game using cocos2d and box2d.
Among other elements, there's a fast-moving player object and a bunch of static line objects. I want the lines to detect when the player crosses them but not to act like a wall to the player object or any other moving objects in the game. So I've got the lines set to be sensors.
However, the nifty anti-tunneling code that Box2D has for fast-moving object collision detection doesn't seem to apply to bodies that are set as sensors. So now my player object passes right through the lines and only gets detected maybe one time in five.
How can I get box2d to detect the sprite crossing the line every time, no matter how fast it's going?
Edit: I found this post on the box2D forums where someone had a similar issue and found a possible solution. However I don't follow how to implement the solution. Maybe it'll help someone else, or maybe someone can explain what this person did more clearly. Here's what they said:
OK I got it working. Someone responded in the Box2D forums with a solution, which is to use a ray cast instead of relying on the built-in collision detection. I was able to find instructions on how to do this in this excellent tutorial on RayWenderlich.com
For my purposes, I simply calculated the sprite's velocity from the last frame, then performed a ray cast to see if it crossed any lines. The callback gives the x,y coordinate of where it crossed.

Move my body to a point

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.