Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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. It moves sometimes and it is not shooting at the right time.

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

// Get velocities of asteroid
// 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;
        }
    }
}
share|improve this question
    
Can you trim out the code that's not important? –  Byte56 Mar 19 '12 at 19:26
1  
A picture would help other people understand your source code. Can you take a screen capture of your program and label what the various variables represent? (asteroid_angle, ship_angle, angle_between, ship_angle_left, ship_angle_right, angle_between_left, angle_between_right) Also please clarify which directions the X and Y axes go in your game and whether 'left' is supposed to be clock-wise or counter-clockwise. –  Leftium Mar 21 '12 at 9:12
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.