C# Collision test of a ship and asteriod, angle confusion

Posted by Cherry on Game Development See other posts from Game Development or by Cherry
Published on 2012-03-19T16:16:53Z Indexed on 2012/03/19 18:15 UTC
Read the original article Hit count: 270

Filed under:

We are trying to to do a collision detection for the ship and asteroid. If success than it should detect the collision before N turns. However it is confused between angle 350 and 15 and it is not really working. Sometimes it is moving but sometime it is not moving at all. On the other hand, it is not shooting at the right time as well.

I just want to ask how to make the collision detection working??? And how to solve the angle confusion problem?

// Get velocities of asteroid


Console.WriteLine("lol");

// IF equation is between -2 and -3
if (equation1a <= -2)
{
    // Calculate no. turns till asteroid hits
    float turns_till_hit = dx / vx;

    // Calculate angle of asteroid
    float asteroid_angle_rad = (float)Math.Atan(Math.Abs(dy / dx));
    float asteroid_angle_deg = (float)(asteroid_angle_rad * 180 / Math.PI);

    float asteroid_angle = 0;

    // Calculate angle if asteroid is in certain positions
    if (asteroid.Y > ship.Y && asteroid.X > ship.X)
    {
        asteroid_angle = asteroid_angle_deg; 
    }
    else if (asteroid.Y < ship.Y && asteroid.X > ship.X)
    {
        asteroid_angle = (360 - asteroid_angle_deg);
    }
    else if (asteroid.Y < ship.Y && asteroid.X < ship.X)
    {
        asteroid_angle = (180 + asteroid_angle_deg);
    }
    else if (asteroid.Y > ship.Y && asteroid.X < ship.X)
    {
        asteroid_angle = (180 - asteroid_angle_deg);
    }

    // IF turns till asteroid hits are less than 35
    if (turns_till_hit < 50)
    {
        float angle_between = 0;

        // Calculate angle between if asteroid is in certain positions
        if (asteroid.Y > ship.Y && asteroid.X > ship.X)
        {
            angle_between = ship_angle - asteroid_angle;
        }
        else if (asteroid.Y < ship.Y && asteroid.X > ship.X)
        {
            angle_between = (360 - Math.Abs(ship_angle - asteroid_angle));
        }
        else if (asteroid.Y < ship.Y && asteroid.X < ship.X)
        {
            angle_between = ship_angle - asteroid_angle;
        }
        else if (asteroid.Y > ship.Y && asteroid.X < ship.X)
        {
            angle_between = ship_angle - asteroid_angle;
        }

        // If angle less than 0, add 360
        if (angle_between < 0)
        {
            //angle_between %= 360;
            angle_between = Math.Abs(angle_between);
        }

        // Calculate no. of turns to face asteroid
        float turns_to_face = angle_between / 25;

        if (turns_to_face < turns_till_hit)
        {
            float ship_angle_left = ShipAngle(ship_angle, "leftKey", 1);

            float ship_angle_right = ShipAngle(ship_angle, "rightKey", 1);

            float angle_between_left = Math.Abs(ship_angle_left - asteroid_angle);

            float angle_between_right = Math.Abs(ship_angle_right - asteroid_angle);

            if (angle_between_left < angle_between_right)
            {
                leftKey = true;
            }
            else if (angle_between_right < angle_between_left)
            {
                rightKey = true;
            }

        }

        if (angle_between > 0 && angle_between < 25)
        {
            spaceKey = true;
        }
    }
}

© Game Development or respective owner

Related posts about c#