user5373865
user5373865

Reputation:

Creating a missile that fires in a ever changing direction

Ok, I'm a programming student at university and I've came across this issue while creating my 2D Star Wars X-Wing Game. The Ship is constantly changing direction to avoid incoming TieFighters and I have been able to create a missile in front of the X-Wing.

The issue is that I don't know how to fire the missile in the direction the ship is facing.

Code for placing missile:

if (pInputs->KeyPressed(DIK_SPACE))
{
    Vector2D missileDirection;  //Section needs fixed as missile spawns inside ship and destroys it.
    missileDirection.setBearing(m_angle, 100);
    Vector2D missilePosition = m_position + missileDirection;
    FlakShell* pFlakShell = new FlakShell;
    pFlakShell->Initialise(missilePosition, m_angle, m_pParticles);
    Game::instance.m_objects.AddItem(pFlakShell, true);
}

This is the velocity for the FlakShell(missile):

m_velocity.set(0, 0);

and this is the code I have to make it move:

m_velocity += m_velocity * 1.5f * frametime;
m_position += m_velocity * frametime; //Position is equal to velocity * frametime

Can anyone help me?

Upvotes: 2

Views: 313

Answers (1)

David O.
David O.

Reputation: 90

I think you already have the right code, but you are missing a scaling factor to move the missle away from the ship:

float ScalingFactor = 100.f; //Just a guess...
Vector2D missilePosition = m_position + missileDirection*ScalingFactor;

Upvotes: 1

Related Questions