This project has moved and is read-only. For the latest updates, please go here.

Spear and Arrow Projectiles

Apr 13, 2009 at 11:20 AM
1. How do I launch a Spear Body/Geom at a user specified angle? I want the user to change the angle that the spear launches so they can arch it to make it go further or shorter.
2. How do I do this relative to the rotation of a Ship Body/Geom so that if the Ship is at a steep angle, the spear will launch at a steeper angle?
3. My Spears are created using Texture to Vertices, this makes the spear heavier at the pointy arrow end and seems to make my Spear spin out of control, is there anyway to limit this? Is it a problem because I am not applying the force in the direction that the spear is rotated and therefore causes it to spin? It might clear up after the first two questions if that is the case.

Thanks for your help. I would research more on my own first but I am really tired now and our Beta for our project is due Tuesday. Let me know if anything doesn't make sense.
Apr 13, 2009 at 12:25 PM
Lemme take a shot:
1. Create your body at the required position and rotation, and use Body.ApplyImpulse with a calculated value based on the rotation
2. You'll need to elaborate just a bit on what you mean because it doesnt make sense to me (but i'm tired too :D)
3. Hmm. I assume you're using a separate body/geom combo for the spear tip? In that case, either add a counterweight, increase the spear body weight, or decrease the spear tip mass.
I hope these help, if they dont im sure others will be able to help you.
Apr 13, 2009 at 7:03 PM
I added Body.ApplyImpulse instead of Body.ApplyForce and that works much better but I not quite sure how to set the impulse based on the rotation.

My spear is a png graphic aligned as shown below with transparency set around it. I am using Texture to Vertices on it so that it maps more accurately for collision detection.


I want to aim it at a diagonally upward angle,         /               and fire it in that direction. This angle should be aligned to the ship.
                                                              \      ship          /
If the ship tilts up, the angle that the spear should fire should still be at the same angle relative to the ship even though the ship is no longer horizontally oriented. I do not think that my third question matters as long as I get one and two working.

Yeah, basicly I want to make it just like this:
The only difference is that my spear needs to shoot tip first since it is not a ball. The angle that it aims visually matters, but otherwise, no difference. Also, the velocity would be hard coded depending on the projectile type or "powerup" so I don't care about increasing or decreasing power.
Thanks again, hope you like my ascii pictures. I think ascii graphics would be awesome as an unlockable mode btw!
Apr 13, 2009 at 8:28 PM
are you using a fixed angle joint on the spear, Id reccomend it, but I will assume your not.

Vector2 Velocity;

public void Shoot()
Velocity = new Vector2((float)Math.Cos(Spear.Geom.Rotation),
(float)Math.Sin(Spear.Geom.Rotation)) * 5;
5 is the speed,
Since we are in farseer i suppose you could apply this vector two to the apply impulse instead of the position.
I have only done this with position updating and not impulse updates but it shuld work the same.
Apr 15, 2009 at 6:42 AM
Thanks guys. The spear now shoots out great and I can control the angle at which it fires. The only problem is that now it does not rotate after being shot. It does and nice spear-like curve path, but the spear just stays pointing upwards at the same angle to whole time. Obviously, I would like the spear to shoot upwards and start pointing down as it reaches the top of the curve. Thanks.
Apr 15, 2009 at 8:15 AM
what body/geom configuration are you using for your spear? i would reccomend attaching both the spear's shaft and head geoms to a single body (with offsets), that way they always stay at proper angles to each other.
Apr 15, 2009 at 10:45 AM
The Spear is actually just one Body/Geom. It is a Texture to Vertices style Geom so the head and shaft are calculated together from a PNG spear picture with transparency. The code I have that shoots the spear follows.

            // Fire Weapon
            if (keys.IsKeyDown(Keys.Space) &&
                PhysicsObject spearTemp = spear.Fetch();

                // Spear does not collide with playerShip
                spearTemp.geom.CollisionCategories = CollisionCategory.Cat2;
                spearTemp.geom.CollidesWith = CollisionCategory.All & ~CollisionCategory.Cat1;

                //We need to reset the dynamics of the body.

                spearTemp.geom.OnCollision += OnCollision;
                spearTemp.body.Position = new Vector2(playerShip.body.Position.X, playerShip.body.Position.Y - 100);
                spearTemp.body.Rotation = projectileAngle;
                Velocity = new Vector2((float)Math.Cos(spearTemp.geom.Rotation),
                        (float)Math.Sin(spearTemp.geom.Rotation)) * 10000;

                //Reactivate the body
                spearTemp.geom.Body.Enabled = true;

                // Play Sound


The spear shoots out fine, but its rotation becomes permanently set at projectileAngle. Gravity does not change its rotation. When I tried to shoot the spears with ApplyForce, the spear rotated as expected due to gravity. I don't know if it is a problem with my code, or if I just need to add something else entirely.