Arrow shooter game need physics help

Hi

I am working on arrow shooter game, similar to http://www.silvergames.com/apple-shooter.
using cocos2d-x inbuilt physics engine, for shooting the arrow i used applyImpulse method of PhysicsBody, the arrow is going in correct direction with angle.
Like when i fired the arrow with a angle of 45 degree, it is going upward but when going downward after reaching maximum height the angle of arrow is still 45 degree and head of arrow is still in upward direction.
i want the arrow while moving downward it should be correctly rotated and head of arrow should be in downward direction.
Before firing the arrow i am rotating the arrow with setRotation method.

version: cocos2d-x 3.6, C++
let me know you guys if you need any more information.

Thanks

Try making a arrow from two fixtures, one for head and one for body with different weights ( head should have higher weight). This will allow arrow to fly more naturally. Since center of mass wont be in the middle of a body.

thanks for your reply,

i am trying this code

    auto size = getContentSize();
    auto body = Node::create();
    body->setPosition( Vec2(size.width / 2, size.height / 2) );
    addChild( body, 1, 0 );
    
    auto arrowBody = PhysicsBody::createBox( Size(size.width - 20, size.height) );
    arrowBody->setDynamic( true );
    arrowBody->setMass( 1 );
    body->setPhysicsBody( arrowBody );
    

    auto head = Node::create();
    head->setPosition( Vec2(size.width / 2 + 80, size.height / 2) );
    addChild( head, 1, 1 );
    
    auto arrowHead = PhysicsBody::createBox( Size(size.height, size.height) );
    arrowHead->setDynamic( true );
    arrowHead->setMass( 100 );
    head->setPhysicsBody( arrowHead );
    
    PhysicsJointDistance* joint = PhysicsJointDistance::construct(arrowBody, arrowHead, Vec2(0, 0), Vec2(0, 0) );
    Director::getInstance()->getRunningScene()->getPhysicsWorld()->addJoint( joint );

but the problem is still there, can you tell me what i am doing wrong.

Aha, you are using built in physics. Sorry i’m not familiar with it. I was talking about Box2D.

I’m newbie, but i have an idea, can you get the arrow direction (or line near vector of that arrow), then you will rotate your arrow depend on that vector.

For example: when you shoot at 45* -> vector = (0.5,0.5), because of gravity, vector will decrease the y component -> (0.5,0.4)…(0.5,0.3)… then when it go below 0 -> (0.5, -0.1)… the arrow will come downward.

Hope this can help!

What you need to do is make the rotation of your arrow follow the path of the arrow - so if hte arrow is going left t right, it is rotated through 0 degrees. If it is going directly up, it is 90 degrees rotated etc.

There’s a load of stuff about this (in Box2d but the idea is the same ) here


and
http://www.iforce2d.net/b2dtut/sticky-projectiles

I implemented a (box2d) solution that tests the velocity, normalises it, calculates the angle the arrow should be going at, then rotates it by a maximum value toward that angle (this helps it look smoother).

you should be able to do something similar in built-in physics which uses cocos2d.

thanks

that link is very helpful.

PhysicsJointDistance* joint = PhysicsJointDistance::construct(arrowBody, arrowHead, Vec2(0, 0), Vec2(0, 0) );

and then

joint->createConstraints();

Now addJoint.