Bullet rotation

Topics: User Forum
Mar 8, 2013 at 7:13 PM
Hello there,
I'm still writing a 2D worms-like game and i got troubles orienting at an angle the bullet.
The player is fixed and can aim 360°, when it fires in a direction, I want bullet to describe something like an elliptic trajecctory and the bullet would rotate around the angle it makes between the tangent to the elliptic arc and the X axis. Would that be possible ?
Here's my code for creating a bullet at the end of the canon :
        const float BulletForce = 8000f;
        public Body Shoot(float angle)
        {
            if (CanShoot)
            {
                CanShoot = false;
                Vector2 direction = new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));
                Vector2 force = new Vector2(BulletForce * direction.X, BulletForce * direction.Y);

                bullet = new Body(_world);
                // The bullet is a thin rectangle to fit a rocket
                Vertices vertices = PolygonTools.CreateRectangle(1.5f, 0.5f);
                PolygonShape polygon = new PolygonShape(vertices, 1f);
                Fixture fixture = bullet.CreateFixture(polygon);

                // Calculate the initial position of the bullet, at the end of the canon
                bullet.Position = _weapoon.Body.Position + new Vector2((float)Math.Cos((double)_weapoon.Body.Rotation) * 5f,
                                                                       (float)Math.Sin((double)_weapoon.Body.Rotation) * 2f);
                bullet.Mass = 15f;
                bullet.BodyType = BodyType.Dynamic;
                // give the bullet the same rotation the weapoon, 
                // FIXME: never updated
                bullet.Rotation = _weapoon.Body.Rotation;

                bullet.ApplyForce(force);
                bullet.OnCollision += new OnCollisionEventHandler(bullet_OnCollision);

                return bullet;
            }
            else
                return null;
        }
I wonder if moving the local center of the bullet would help, but if so, where?

Thanks in advance !
Mar 12, 2013 at 7:17 PM
If I understand correctly, you want your bullet to fly like a rocket, or a thrown spear (that would stick in the ground when it lands), correct? If so, I would try moving the LocalCenter to the front of the bullet/rocket and then using an Impulse to fire the rocket instead of a Force. If that doesn't work you might also try applying the Impulse to the back of the rocket (where the engine would be) instead of it's LocalCenter. To do this I think you'd need to attach another body to the back of the rocket (call it the engine) and then apply the impulse to that body.

Hope that helps,
Mike.
Mar 18, 2013 at 1:54 PM
Edited Mar 18, 2013 at 1:54 PM
Thanks for the answer, yes, you understood correctly, I just got 2 problems with this solution :
First creating a new body at the back of the rocket may cause an additional weight and may weigh in the wrong place, am I right ? Also, how do you compute the vector of the backward impulse applyed to this body ? Because, in this solution the impulse could be applyed on each frame so its direction have to be updated every time it's applyed and the BulletForce const will change alot.
Mar 18, 2013 at 3:54 PM
Well I suggested moving the LocalCenter to the front of the bullet first and trying that. You'll only need to add another body if that doesn't give you the behavior you're after, and adding a small body with very low density won't throw off the weight distribution -- you can offset it by moving the LocalCenter on the bullet.

I'm not suggesting you apply the impulse every frame. If you're simulating a bullet or spear, you want to apply just one large impulse when the bullet is "fired".

At any rate, that's what I would try.

Good luck,
Mike.
Developer
Mar 18, 2013 at 4:39 PM
Have a look at this tutorial:
http://www.iforce2d.net/b2dtut/sticky-projectiles

It explains how you can manually add some force to the projectiles, to make them behave like you want them to. I'd go another route though: Since we are talking about a Worms Clone here and pixel perfect collisions might not be needed, I'd just fire small circles as bullets and would just rotate the sprite at the circles position to always face in the direction of the circles linear velocity. No need to simulate the exact shape of the projectile.
Mar 18, 2013 at 7:23 PM
Thanks for the answer, yes, you understood correctly, I just got 2 problems with this solution :
First creating a new body at the back of the rocket may cause an additional weight and may weigh in the wrong place, am I right ? Alose, how do you compute the vector of the backward impulse applyed to this body ? Because, in this solution the impulse could be applyed on each frame so its direction have to be updated every time it's applyed and the BulletForce const will change alot.