Hello, I'm newcomer in cocos2d and box2d and iPhone development in all. I want to get moving direction in next format: +1 - forward moving; -1 - backward moving; 0 - no moving. May be exist the easy and direct way to do this? I think I can define CGPoint variable to store position of object (I write wrapper for box2d fixture with body, in userData I store self pointer) and set value of this field in tick function ( sheduler )? And then I can get difference between current object position and previously. Is this right way, because I have an array of objects? And the next question is: how can I destroy body in contact listener? I read some post in this forum, where this problem was discussed, but without right solution. In box2d source code I found this comment: /// @warning You cannot create/destroy Box2D entities inside these callbacks.
But may be exist a hack or other way to do this, I need to destroy object immediately after collision?
Moving direction in box2 and destroying bodies inside contact listener problems
(7 posts) (4 voices)-
Posted 1 year ago #
-
The best way to go about doing this is to make an array to hold the bodies you want to get rid of and then purge that array at the end of your tick method calling world->DestroyBody();
Posted 1 year ago # -
Uhm, I'm not entirely sure I understand your first question.
Are you trying to understand if a box2d body is moving left (-1), right (+1) or not at all (0).
Or are you trying to apply those kind of forces?
If it's the first one, simply store the position of the body during a step and also check it's previous position and calculate the +1,-1 thing.Since you can't destroy bodies in callbacks, the normal approach would be to fill an array of bodies that need to be destroyed during the callback, once the callback is finished, in your step/tick method for example, you iterate over that array and destroy the bodies.
Posted 1 year ago # -
Thanks a lot, I did that and it works! As you both said, I implemented destroying bodies in step/tick method and calculate direction as difference between current position and previous position, in this code:
int directionOnX = (currentPoint.x >= ball.previousPoint.x) ? MOVING_FORWARD : MOVING_BACKWARD ;and set value of previous point in step/tick method. All as you said. Thanks again!
Posted 1 year ago # -
Another way to check the direction a box2d body is moving is to look at its velocity.
body->GetLinearVelocity()will return both the x and y velocity and you can check the x velocity to see if it is negative or positive. This will simplify your code some, but
it sounds like you already got everything working.Posted 1 year ago # -
toollbird, I thought that x velocity sign not changing (once defined) like force or impulse. Thanks, I will check this solution.
Posted 1 year ago # -
The velocity is a signed value that indicates direction and speed the body is currently moving. It will be updated every time the box2d world is stepped. In general, if a value is called "speed" it does not have directional information. If a value is called "velocity" it has direction and speed. Hope this helps!
Posted 1 year ago #
Reply
You must log in to post.