Skip to content Skip to sidebar Skip to footer

I Am Wondering Why It Says The Distance Has To Be Less Than 27? Why Less Than 27? Where Is 27 Coming From?

My teacher gave us this code to analyze and I am not sure why he put 27 there for collision.. I asked him he said if the distance between the player and the enemy is less than 27

Solution 1:

The value is 27, because you compute the Euclidean distance between (enemyX, enemyY) and (bulletX, bulletY) in the function isCollision:

defisCollision(enemyX, enemyY, bulletX, bulletY):
   distance = ((enemyX - bulletX) ** 2 + (enemyY - bulletY) ** 2)
   distance = math.sqrt(distance)
   print(distance)

The Euclidean distance between 2 points (Ax, Ay) and (Bx, By) is

d = sqrt((Bx-Ax)**2 + (By-Ay)**2) = hypot(Bx-Ax, By-Ay)

In 2 dimensional space this is the same as the Pythagorean theorem. The length of the diagonal in a square with a side length of 19 is approximately 27.

Solution 2:

Try changing the '27' to a small no. like '5' and you will see that the collision is happening too late on screen. OR if you replace the enemy/bullet image you are loading with some other small/large image you will notice that you will have to change you '27' to some other number to make things work as intended.

Simple Words The reason for the hard-coding '27' could be because the width of enemy image and the width of bullet image you load in your code seem to touch (collide) eachother at the distance '27' from the point where they are drawn.

Explanation When we load an image pygame draws it from top-left corner. For Example your background image is so large in code but when you ask your code where is this 'background' located it well tell you it is on (0,0) although it's on your full screen not a single point. Infered from screen.blit(background, (0, 0)) line in your code

Same for the images/sprites of enemy and bullets. The position you use in collision enemyX, enemyY, bulletX, bulletY are the top-left coordinates. Now to show them actually touching/colliding you may have to tell the code that 'My enemy is more than one pixel on the position. So I want to take care of it by using the constant '27' (depends on width of enemy/bullet). As soon as the both positions have a Euclidean distance of '27' this means they are colliding on my screen hence proceed as true.

If you are still confused about the positioning issue and how center of image or top-left of image matters read these, it will help.

https://stackoverflow.com/a/51182238/11672352

Why is the pygame rect not in the right place?

Solution 3:

From reading your code there are functions called bulletX and bulletY. Is the 27 saying the range of the bullets is 27 and the play must be with that distance to be affected by those bullets?

Solution 4:

It seems to me like the game triggers the collision once the bullet enters a circle of radius 27 pixels around the enemy. Although your enemy may not be a perfect circle, it is a lot simpler to treat them as if they were when calculating collisions as it is relatively easy to check whether a point has collided with a circle, in this case, the bullet with the enemy. the lines:

distance = ((enemyX - bulletX) ** 2 + (enemyY - bulletY) ** 2)
distance = math.sqrt(distance)

are used to calculate the distance between the middle of the enemy and the bullet by using Pythagoras' theorem (a^2 + b^2 = c^2 where a and b are two different sides of a right angled triangle and c is your hypotenuse, aka the longest side) The value 27 is likely used because the enemy is likely around 52 pixels (27 * 2) wide and tall

Post a Comment for "I Am Wondering Why It Says The Distance Has To Be Less Than 27? Why Less Than 27? Where Is 27 Coming From?"