Home Game Development c# – calculate projectile velocity from moving shooter

c# – calculate projectile velocity from moving shooter

0
c# – calculate projectile velocity from moving shooter

[ad_1]

I have a task which i have a moving ship that can shoot in any direction i want. i have to shoot to standing or moving nearby objects.

the problem is, I can’t correctly calculate true shoot direction.I must find a direction after ship velocity apply on it then bullet go to target position.

Ship shoot method :

public void shoot(vector3 direction)
{
   var projectile=  new Projectile();
   projectile.Velocity = Ship.LinearVelocity + Vector3.Normalize(direction) * Ship.GunInfo.ProjectileSpeed;
   projectile.Position = Ship.Position;
   World.Add(projectile);
}

Projectile update method :

public void Update(TimeSpan deltaTime)
{
    Position+= Velocity * deltaTime;
}

So what is true direction which must to be passed into shoot method?

I tried bellow calculations :

1- first solution i tried to shoot targets : (really bad accuracy)

var baseDirection = target.Position - Ship.Position;
var direction = baseDirection - Ship.LinearVelocity/Ship.GunInfo.ProjectileSpeed;
shoot(direction);

2- second solution i tried to shoot targets : (better accuracy)

var baseDirection = target.Position - Ship.Position;     
var shootDirection = Ship.LinearVelocity + Vector3.Normalize(baseDirection) * Ship.GunInfo.ProjectileSpeed;
var direction = Vector3.Reflect(- shootDirection, Vector3.Normalize(baseDirection))
shoot(direction);
  • All velocities (ship, projectile) are vector3.

  • I don’t use any physic engine but i have vector3 math functions like (normalize, project, dot, …).

  • Normalize mean vectorA/length(vectorA) that means vector with length one

  • ship speed at shoot time is constant but periodically change velocity and direction.

  • there is no gravity,wind etc all velocity remain constant except ship that change its way.

my solutions

[ad_2]