Like Tree2Likes
  • 1 Post By grumpy
  • 1 Post By VirtualAce

help with collision

This is a discussion on help with collision within the C++ Programming forums, part of the General Programming Boards category; I am making a game and checking collision with the following method: Code: //player variables int player_x; int player_y; int ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    64

    help with collision

    I am making a game and checking collision with the following method:

    Code:
    //player variables
    int player_x;
    int player_y;
    int player_x2;
    int player_y2;
    int player_x3;
    int player_y3;
    int player_x4;
    int player_y4;
    int player_width=(player_x2-player_x);
    int player_height=(player_y3-player_y2);
    //enemy named "imran" variables
    float imran_x=100.0+enemy_a;
    float imran_y=100.0+enemy_b;
    float imran_x2=200.0+enemy_a;
    float imran_y2=100.0+enemy_b;
    float imran_x3=200.0+enemy_a;
    float imran_y3=200.0+enemy_b;
    float imran_x4=100.0+enemy_a;
    float imran_y4=200.0+enemy_b;
    int imran_width=(imran_x2-imran_x);
    int imran_height=(imran_y3-imran_y2);
    bool collision_enemy(){
        if(player_x + player_width > imran_x && player_x < imran_x + imran_width && player_y + player_height  > imran_y && player_y < imran_y + imran_width )
        {return true;
        cout<<"collision theindi payee\n";}
        else 
            return false;
        }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You are not going to see the output "collision theindi payee" even if you have as condition the number 1 (or any different number from zero).
    return true is located before the cout, thus the function will return before cout.

    Also, I do not really remember the priority of the + and > operators, but I won't even bother to check. Use parentheses in order to be sure, like this
    Code:
    if( ( (player_x + player_width) > imran_x) && ( player_x < (imran_x + imran_width) ) && ...
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    Quote Originally Posted by std10093 View Post
    Also, I do not really remember the priority of the + and > operators, but I won't even bother to check. Use parentheses in order to be sure, like this
    Classic case of laziness leading to wasted effort and code that is more fragile: too many unnecessary brackets in expressions tend to make the code harder to understand, and therefore easier to get wrong.

    Numeric operators are higher precedence than comparison operators. So expressions mean what anyone would expect if they paid even minimal attention during primary school or secondary school mathematics classes.

    So "a + b > c" is equivalent to "(a + b) > c", not to "a + (b > c)".

    I can understand your caution for mixing (say) pointer operations with incrementing (since people don't learn rules in real life outside programming that are applicable), but not for basic math expressions.
    kmdv likes this.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    How much can some words make you feel bad?

    Anyway
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Collision is simpler to test for if you eliminate what is impossible. What remains must be true.
    iMalc likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. collision
    By ichijoji in forum Game Programming
    Replies: 8
    Last Post: 07-13-2004, 08:04 PM
  2. How do I detect collision?
    By Marcos in forum Game Programming
    Replies: 11
    Last Post: 03-24-2004, 03:49 PM
  3. collision
    By lambs4 in forum Game Programming
    Replies: 3
    Last Post: 10-07-2003, 07:42 PM
  4. Box Collision
    By Shiftytitan in forum Game Programming
    Replies: 2
    Last Post: 12-31-2002, 06:19 PM
  5. collision???
    By kas2002 in forum Game Programming
    Replies: 3
    Last Post: 07-08-2002, 06:39 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21