Skip to main content
Fixed typos
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I have a few remarks:

  • You may have a problem with this line of code:

     float slope = (y1 - entity->y1) / (x1 - entity->x1); 

    If entity is already at the longitude x1, this will cause a division by 0. Such a division is undefined behaviour. Anything can happen. A plane may crash on your house. You should check first whether x1 == entity->x1 and handle this case properly. I see that the last else close of your program handles this case (//When the player shoots up or down ), but the division by 0 appears before you handle it.

  • I don't know whether entity.x and entity.y are int or float types. If they are int types, I guess that you can get rid of the float variables: the only thing that may need more prceisionprecision than an integer is slope, and if entity.x and entity.y are integer types, then slope will contain an integer castedcast to a float (since it was intializedinitialized with an integer division).

It lacks a bit of context to provide a complete review. You should post the code of at least Map and Entity so that we don't have to assume things.

I have a few remarks:

  • You may have a problem with this line of code:

     float slope = (y1 - entity->y1) / (x1 - entity->x1); 

    If entity is already at the longitude x1, this will cause a division by 0. Such a division is undefined behaviour. Anything can happen. A plane may crash on your house. You should check first whether x1 == entity->x1 and handle this case properly. I see that the last else close of your program handles this case (//When the player shoots up or down ), but the division by 0 appears before you handle it.

  • I don't know whether entity.x and entity.y are int or float types. If they are int types, I guess that you can get rid of the float variables: the only thing that may need more prceision than an integer is slope, and if entity.x and entity.y are integer types, then slope will contain an integer casted to a float (since it was intialized with an integer division).

It lacks a bit of context to provide a complete review. You should post the code of at least Map and Entity so that we don't have to assume things.

I have a few remarks:

  • You may have a problem with this line of code:

     float slope = (y1 - entity->y1) / (x1 - entity->x1); 

    If entity is already at the longitude x1, this will cause a division by 0. Such a division is undefined behaviour. Anything can happen. A plane may crash on your house. You should check first whether x1 == entity->x1 and handle this case properly. I see that the last else close of your program handles this case (//When the player shoots up or down ), but the division by 0 appears before you handle it.

  • I don't know whether entity.x and entity.y are int or float types. If they are int types, I guess that you can get rid of the float variables: the only thing that may need more precision than an integer is slope, and if entity.x and entity.y are integer types, then slope will contain an integer cast to a float (since it was initialized with an integer division).

It lacks a bit of context to provide a complete review. You should post the code of at least Map and Entity so that we don't have to assume things.

Source Link
Morwenn
  • 20.2k
  • 3
  • 69
  • 132

I have a few remarks:

  • You may have a problem with this line of code:

     float slope = (y1 - entity->y1) / (x1 - entity->x1); 

    If entity is already at the longitude x1, this will cause a division by 0. Such a division is undefined behaviour. Anything can happen. A plane may crash on your house. You should check first whether x1 == entity->x1 and handle this case properly. I see that the last else close of your program handles this case (//When the player shoots up or down ), but the division by 0 appears before you handle it.

  • I don't know whether entity.x and entity.y are int or float types. If they are int types, I guess that you can get rid of the float variables: the only thing that may need more prceision than an integer is slope, and if entity.x and entity.y are integer types, then slope will contain an integer casted to a float (since it was intialized with an integer division).

It lacks a bit of context to provide a complete review. You should post the code of at least Map and Entity so that we don't have to assume things.