Friday, July 13, 2018

cocos2d x - Box2d - Attaching a fired arrow to a moving enemy


I am firing an arrow from the player to moving enemies. When the arrow hits the enemy, I want it to attach exactly where it hit and cause the enemy (a square) to tumble to the ground. Excluding the logistics of the movement and the spin (it already works), I am stuck on the attaching of the two bodies. I tried to weld them together initially but when they fell, they rotated in opposite directions.


I have figured that a revolute joint is probably what I am after. The problem is that I can't figure out a way to attach them right where they collide.


Using code from iforce2d:


b2RevoluteJointDef revoluteJointDef;
revoluteJointDef.bodyA = m_body;
revoluteJointDef.bodyB = m_e->m_body;

revoluteJointDef.collideConnected = true;
revoluteJointDef.localAnchorA.Set(0,0);//the top right corner of the box
revoluteJointDef.localAnchorB.Set(0,0);//center of the circle
b2RevoluteJoint m_joint = *(b2RevoluteJoint*)m_game->m_world->CreateJoint( &revoluteJointDef );
m_body->SetLinearVelocity(m_e->m_body->GetLinearVelocity());

This attaches them but in the center of both of their points.


Does anyone know how I would go about getting the exact point of collision so I can link these? Is this even the right method of doing this?


Update:


I have the exact point of collision. But I still am not sure this is even the method I want to go about this. Really, I just want to attach body A to B and have body B unaffected in any way.





No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...