tModLoader Need a little help with a math calculation

POCKETS

Terrarian
Alright, so I am attempting to spawn two projectiles from what's known as a "held projectile" (basically a weapon, but it's a projectile that rotates just like a normal weapon item, and is stuck to the player).

Anyhow, normally, I would spawn these two projectiles using the item's Shoot() hook. However, I can't do that anymore, since this particular issue has to do with the 1.4 alpha, and the Shoot() hook doesn't give me the functionality required to make this work (there's more to it than that, but I'll just leave it at that).

Here is the original calculation from the Shoot() hook I was using in 1.3 tMod -

C#:
Vector2 velocity = new Vector2(10, 10); // I added this in the held projectile because I needed a velocity. This wasn't used in the Shoot() hook
float startRadius = 5f;
Vector2 direction = Vector2.Normalize(new Vector2(velocity.X, velocity.Y));
direction *= startRadius;
Vector2 firePosition1 = direction.RotatedBy(0.9f);
Vector2 firePosition2 = direction.RotatedBy(- 0.9f);
Projectile.NewProjectile(Projectile.GetProjectileSource_FromThis(), Projectile.Center.X + firePosition1.X, Projectile.Center.Y + firePosition1.Y, velocity.X, velocity.Y, ModContent.ProjectileType<ModProjectile>(), Projectile.damage, Projectile.knockBack, player.whoAmI);
Projectile.NewProjectile(Projectile.GetProjectileSource_FromThis(), Projectile.Center.X + firePosition2.X, Projectile.Center.Y + firePosition2.Y, velocity.X, velocity.Y, ModContent.ProjectileType<ModProjectile>(), Projectile.damage, Projectile.knockBack, player.whoAmI);

As you can see, I am shooting two projectiles, offset from each other by a small margin.

The problem is, now that I am shooting these projectiles from a held projectile, and not inside the Shoot() hook, this no longer works. Well, it does shoot the projectiles, but at a 45 degree angle down and to the right. The projectiles are still offset from each other, as they should be, but I believe I need to factor in the rotation of the held projectile here, and I can't seem to figure out how to do that. My math skills are... lacking, so I was hoping someone could help me out.

Just for reference, here's how I am handling the position and rotation of the held projectile -

C#:
Projectile.velocity = Vector2.Zero;
Projectile.position.X = player.Center.X - Projectile.width / 2;
Projectile.position.Y = player.Center.Y + player.gfxOffY - Projectile.height / 2;

if (Main.myPlayer == Projectile.owner && Projectile.timeLeft > 2)
{
    Projectile.ai[0] = (Main.MouseWorld - player.Center).ToRotation();
}
Projectile.rotation = Projectile.ai[0];
 
Are you able to post screenshots to make the behavior visible? Do the projectiles always deviate by the same 45 degree angle? Wouldn't you just need to offset it by 45 degrees to have it fire correctly, or am I misunderstanding?
 
Are you able to post screenshots to make the behavior visible? Do the projectiles always deviate by the same 45 degree angle? Wouldn't you just need to offset it by 45 degrees to have it fire correctly, or am I misunderstanding?

It's probably my fault for not explaining it very well.

The held projectile rotates with your mouse when firing. Think of it just like a normal weapon (item). The projectiles are supposed to come out of the muzzle of the weapon (held projectile), even if you rotate said weapon. They are not. They always fire at a 45 degree angle down and to the right, no matter how I rotate the held projectile(weapon).

For some reason, it's not taking into account the rotation of the held projectile(weapon), from what I can gather.
 
Last edited:
I managed to get this fixed by taking a look at some other code I had written awhile back.

It's not entirely fixed, but it's working as intended (for the most part). I have the rotation down, and it's following the mouse, and held projectile, but I still need to offset the two projectiles, so I'm working on that now.

EDIT: Fixed.


For those interested in how I fixed this -

C#:
Vector2 shotDirection = new Vector2(8, 8).RotatedBy(Projectile.rotation).RotatedBy(MathHelper.ToRadians(-45)); // this line was added
float startRadius = 5f;
Vector2 direction = Vector2.Normalize(new Vector2(shotDirection.X, shotDirection.Y));// these were changed to the new Vector2 line
direction *= startRadius;
Vector2 firePosition1 = direction.RotatedBy(0.9f);
Vector2 firePosition2 = direction.RotatedBy(- 0.9f);
Projectile.NewProjectile(Projectile.GetProjectileSource_FromThis(), Projectile.Center.X + firePosition1.X, Projectile.Center.Y + firePosition1.Y, velocity.X, velocity.Y, ModContent.ProjectileType<ModProjectile>(), Projectile.damage, Projectile.knockBack, player.whoAmI);
Projectile.NewProjectile(Projectile.GetProjectileSource_FromThis(), Projectile.Center.X + firePosition2.X, Projectile.Center.Y + firePosition2.Y, velocity.X, velocity.Y, ModContent.ProjectileType<ModProjectile>(), Projectile.damage, Projectile.knockBack, player.whoAmI);

In my specific case, I had already established the Projectile.rotation (in the original post), so I didn't have to specify it beforehand. However, I did have to add it to the equation.
 
Last edited:
Personally I prefer to use MathHelper.Pi and times that by what you need, MathHelper.Pi is half a circle's worth of rotation, so to get right angles times it by half or use MathHelper.PiOver2
 
Back
Top Bottom