How to detect multiple collision point in sprite body. I know the detection of sprite bodies but don't know in custom collision.
In my game i have player and enemy sprite both are in dynamic body and I want to detect collision in two way like mario type. Collision detection in top of the enemy and another is collision detection in front of enemy..
How can i do this??
Thanks in advance.
you can add different fixtures to the head and to the bottom of your enemy. then just check which enemy fixture collided with your hero
Related
I am trying to use bullet physics library in my opengl game engine for collision detection.
I am not trying to do anything complicated, I only want to know when things collide in the 3d world. I am only using box shapes and I have no interest in moving the objects using physics (I have even disabled collision response for my rigid bodies).
I have managed to get the collision to work doing the following:
First, each frame I update the rigid bodies world transform based on the entity's transform it is attached to. For example I move my player right, the rigid body (and the bounding box) moves with me.
I am checking if 2 objects are colliding using this code:
void PhysicsManager::myTickCallback(btDynamicsWorld *world, btScalar timeStep)
{
int numManifolds = world->getDispatcher()->getNumManifolds();
if (numManifolds > 0)
cout << "COLLISION" << endl;
}
myTickCallback is the dynamics world internal tick call back: dynamicsWorld->setInternalTickCallback(myTickCallback);
What is weird now is that when I start up the game, I move my player on the other object I want it to collide with and it starts spamming COLLISION. If I leave the player on the object it will keep spamming it. However, if go away from the object and go back into it several times, after a while it just stops writing COLLISION.
I have checked if the rigid bodies are moving (if there is collision response) and there isn't.
This could be a problem in the game I am trying to create because I want the player to be hit by projectiles and it wouldn't be right if he stops getting hit after a while.
Im using Level Helper and SpriteHelper to create my sprites, images, levels and more importantly animations and physics.
Note, by physics i mean the debugdraw you can see on the simulator that is used for collision detection.
I created a swimmer and added physics to this. Done the code so the physics follows the swimmer around the pool as they move. I have now come to animate the swimmer, make the legs kick etc. Now when i load my game and only the first sprite of the animation is the outline for the physics. So i can see the legs kicking on the swimmer but the debugdraw mesh of the physics doesn't animate as well. Now this is not really a problem until for example my swimmer loses their legs (weird game i know). I change the animation to now a swimmer with no legs but again the physics mesh still shows the legs. So any collisions with stuff still happens where the legs were but they shouldnt. This make sense?
Is there a way to update the physics on the new animation or do i need to remove my whole swimmer and draw a new one?
Any help at all would be great. Thanks
This makes sense, since your sprite uses the same box2d mesh in both states. If you want to have a different collision behavior after altering your Sprite, you should assign another (smaller) mesh body.
Note that even in cocos2d side, your sprite still has the same container box with the new animation.
In order to keep using the SpriteHelper functionality you might want to create 2 different sprite-body sets: one with the full body, then after the "accident" replace it with the legless sprite.
Now, gameplay-wise, my opinion is that there shouldn't be any collision for the legs anyway. since they are moving, players will not find it weird to have them not colliding. You could use a mesh body with no legs and use it for both sprites. Except if you want to have different collisions as a gameplay feature (Like giving the player the choice to cut his legs in order to fit in a smaller cave etc)
How can I make a b2Body move in a arc-like or curved path? I have heard about cocos2d Bezier curve function (ccBezier) but this is for moving cocos2d sprites. Or can it be modified to move box2d bodies? Any help is appreciated. Thank you.
Don't think that you can modify it to move physical bodies. In case of CCNode subclasses this function uses setPosition: method. If you change position of your body every tick with SetTransform method of b2Body object, it will ignore all possible collisions.
You can try to set linear velocity to physical body. In this case you need to change(rotate) velosity vector as you want.
I am trying to make an object get attached on a collision point to a circle that is rotating, but the player needs to get attached with a constant point on the player. For example the player is moving back and forth and when the user touches the screen and the player jumps up but what I need is that when the player collides with the circle it attaches it's legs to it and continues rotating with the circle. So I wanted to know how to make this kind of collision joint in cocos2d box2d?
When the collision between the user and the circle is detected, you could run an action that calls a callback function (perhaps a CCCallFuncND). The callback function could be passed pointers to both bodies (wrapped in NSValue valueWithPointer) and create an arbitrary joint between them. This would be done by first instantiating a b2JointDef of the desired type, defining bodyA and bodyB for that joint as the two bodies you'd like "stuck together", and then calling b2World->CreateJoint().
In terms of attaching the player body to a specific point on the circle body, you'd have to do so via some parameter of the joint def like anchorPoint. For example b2PrismaticJoints have anchorPoint which defines the center of the joint's range of motion.
I'm developing an iOS game with cocos2D.
My game is simple, there are levels, and a rotating sprite.
The sprite need to go from the beginning to the end of the level without losing his lives.
So there is two possibilities for me :
1°) Already working good
Tilemap based levels with 2D pixels styles tilesets
Custom collision detection on the edge of the hero's sprite bounding box, and the tilemap collision.
2°) Would be better graphics, and better users experience (without physics, only collision):
map base on vector graphics / SVG
collision detection using the edge of the hero's sprite shape and the map
But, i read the cocos2D/Box2D documentation, and i doesn't found a collision detection on the edge of the sprite's shape ONLY. It's like a pixel perfect collision (already found algo).
I only want to know if one of the 4 edge of my hero's shape is colliding a border of the level, and if yes which shape is colliding (because my sprite is rotating).
Someone have an idea ?
Thank you a lot for your time.
One polygon shape should be attached to your Hero's body via fixture.
To detect a collision point use contacts between dynamic(hero) and static(walls) bodies.
Just take your hero's shape and divide in half to find the pixel width of the hero shape (the radius) and detect collision if the distance between your hero and another sprite is equal to or less than this radius.