0
votes

So I am trying to test collision in cocos2d-x with box2d. In my game loop I am moving the sprite and not using box2d physics. I do have it setup with a body in box2d so that I can have the rectangles right, however when it collides it is over lapping with the other object. I want it to stop where it collided at. I know why it is doing it (which is because it is told to move 8 points and the collision happens at 4) but I am not sure how to get it to stop exactly where it collides. If I do this:

float x = a->getPosition().x + (a->getContentSize().width / 2) + (b->getContentSize().width / 2);
b->setPositionX(x);

It works but you can see it jump after it is done moving the 8 points and back 4 points. I am not sure if I am doing this completely wrong or what but I cant seem to get it to stop where it collided without jumping. Also just so you know what the update looks like:

Vec2 pos = _enemy->getPosition();
pos.x -= _scrollSpeed;
_enemy->setPosition(pos);

_scrollSpeed = 8 btw

Any help is greatly appreciated.

1
you did not ask a question. to be honest i don't understand the issue based on description and code fragments, specifically what to make of not using but still using physics - when, where, how and why?LearnCocos2D
The question was how to get the sprites to stop when they collide with another at the collision point. Since I am moving the sprites in the layer update function it is set to move at 8 points, however the collision happens after 4 points of movement. So the sprites are actually over top of each other when the game stops but I want them to stop at the actual point they collided at. In the first snippet I have that in the collision BeginContact function and it sets it at the collision point but it jumps back from where it originally overlapped. Am I going about this the wrong way?ngreenwood6
I can post the full code for viewing if that would make it easier. I am just learning how to do everything so its not a real game anyways.ngreenwood6
"you can see it jump" - how about not rendering the scene until things have been set back to the position where they collided.iforce2d
@iforce2d Yeah that is basically what I am trying to do but I am unsure how to do that. I am just learning game programming and cocos2d and am unsure how to do that. I have in the "tick" function to move the objects but the collision from box2d is on the BeginContact method so it has already updated from the tick function when it collides. Is there a way to tell it to stop rendering?ngreenwood6

1 Answers

1
votes

You could create a field in your class which tells whether object is moving or not and set to true/false in Begincontact. Then in update() method just check whether this field is set then you will know if you should move it or not.

Edit: Remember to check this before world->step() is called