Box2D How to transfer remaining impulse from one body to another - cocos2d-iphone

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.

Related

Coordinating Multiple Sprite Animations in Cocos2dx

I have a 5 frame run animation for a character on a sprite sheet. I would like to give this character multiple "upgrades", such as sunglasses, hats, etc. I don't know the "correct way" to do this, so here's what I did:
I constructed a layer to represent the sprite itself, and added the character first, then the hat on top. I have a 5 frame animation for where the hat would be on that characters head ALSO on the sprite sheet, and have it animating with the exact same parameters as the main character.
The problem is that when I try to animate the two sprites simultaneously (the character sprite and the hat sprite), they get out of sync. Sometimes it works great, but sometimes the timing is just a wee bit off, which is enough that his head will go through the top of the hat, or the hat will pop off his head every step.
My current solution is to have an update tick, and manually cycle through the frames every time some number of milliseconds has passed, which works, but I assume since I'm manually setting the frames it's using up more processor time than needed.
So what would be the "correct" way to add a hat to a sprite without having to have a "hatted sprite" series on your sprite sheet, and keep them animating together.
edit: sorry this is tagged badly, I apparently don't have a high enough reputation to tag this "cocos2dx", despite the fact that it's a cocos2dx question.
Well if i have the same situation i will go with taking different spritesheets for different animation according to upgrade of character, that way there will be no need for two different animation pluse u can adjust sprites for best effect and it won't interfare with other animation, only problem is u have to make different sprites according to upgrade.
What kind of animation are you using?
I recommend you to animate using an skeleton. This way is very easy to replace any body-part with different sprites. In TestCpp you have some examples of skeleton animation using CocoStudio (free) or Spine (paid). You even have an example of an animation where weapons are replaced.
If you need to stick to standard frame animation, you need to make sure that both animations start from the same frame when you replace anything. Otherwise, if you replace your hat when you are on a keyframe different from 0, the animation will be out of sync. One solution is to restart both animations. The other is to check the current frame of the body animation and force the hat animation to start on that frame.
OK so what I ended up doing, all the sprites that are assembling a larger character object are put together in a class, together with a float "_lastFrameUpdateTick" and int "_lastFrameNumber". in my update(dt) I have a switch case on _lastFrameNumber for each sprite so it knows if the current time tick is past the _lastFrameUpdateTick + that frame's display time, it should update all the sprites in that class to the next frame number (also allowing me to shift around sprites relative position manually, for cases where, say, a character is bouncing up and down on the back of a horse). This does require me to put copies of any hats repeatedly on the sprite sheet for every animation (though that could probably be faked with plist were one so inclined). This also has the added advantage of allowing certain frames to display slightly longer or shorter than others, allow explicit skipping around in animation sequences depending on events (if there's an earthquake and the character is on a leaned-back part of an animation it falls backwards; if it's on the leaned forward frame of an animation it falls forward), and its easy to halt animations while allowing other stuff to keep running. Most importantly is that doing it manually allows me to always know exactly what frame a sprite is on, so it can smoothly transition between animations without hiccups (I know that if i'm leaned back and want to run forward, i need to go through a leaning-forward animation first).
I'm sure there's a better way to do this, but I'm no C++ developer, and it works, so I'm moving on.

after effects remove still background

I have a movie in the after effects that dosent have a KEY color of background, but a still background.
I want to detect the motion of 2 persons walking in front of this still background and bring them to the front, so I can create effects on the background.
Is that possible to do? Witch Effect do I use?
This works with a technique called difference matting. It never works well even if you have a solid clean plate. your best bet is to rotobrush or just mask them out manually. if you want to try difference matting you can set the footage layer with the people in it to difference blending mode and put a clean plate below it.
you can use the rotobtush that allows to separate elements from a static background. Works better if:
you have a clean background
good quality videos
front object needs to be cutted as big as possible
This method works well with a locked down camera. Depending upon how active people are and how still the background - you can remove the people by cutting and assemble pieces of the background to create a background layer without the people... Once you have a clean plate - then use a difference matte to subtract the background from your clip which will leave anything that moves (read people). This will work well if the hue/values between the background and the people are different.
Doing this in conjuncture with animated masks gives good results and can give you a jump on your rotobrushing to improve your edges.
If all else fails the Mocha plug works well as long as you break your masks up into lots of parts to follow the planes of your actors.
I have never found doing this is a single tool solution.
It is not impossible, but I don't think it could give you the best result.
Anyway, you should mask them out manually. but not keyframe by keyframe. there is a technic for it. for setting the mask keyframes, first, you should set 3 keyframes in first, middle and end of your footage.
after masking three one, you do it the same for first keyframe and the middle one.
it means between the first keyframe and the middle keyframe, you should set a key.
Do the same for middle and end key.
this technic could save your time and make masking with less fault.
by the way, you can use Mocha pro as well for tracking the persons.

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!

2D collision detection and stuff with OpenGL

I am working on a simple 2D openGL project. It contains a main actor you can control with the keyboard arrows. I got that to work okay. What I am wanting is something that can help explain how to make another actor object follow the main actor. Maybe a tutorial on openGL. The three main things I need to learn are the actor following, collision detection, and some kind of way to create gravity. Any good books or tutorials to help get me in the right direction would be great.
You could use a physics library like Chipmunk Physics, which lets you attach springs and things between the two objects and detect when they hit each other and other things.
A pre-rolled library would be good, but the concepts you describe are ones you need to know if you are going to do any sort of game programming anyways:
A simple way to make one actor follow behind another is to have the lead actor store its position every time it moves. Feed these positions to a trailing actor with a delay of a few values - the longer the delay, the further behind they travel. Simple, but doesn't handle dynamic collision (other actors moving the block collision.)
Collision detection in 2D can simply be axis aligned (AA) bounding boxes. Search for this and you'll see the 4 ifs or so that are needed.
Gravity is just adding a fixed velocity (usually down) to every object every game loop. This is constant acceleration which is exactly how gravity works.

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.