Hi,
How can i detect if say a little ball is inside something like a pacman's mouth?
See diagram:
Printable View
Hi,
How can i detect if say a little ball is inside something like a pacman's mouth?
See diagram:
Couldn't you just use some sort of coordinate system and see if the ball x and y is equal to the mouth x,y (or perhaps slightly off)
The pacman-like thing should be able to move and turn 360 degrees, so it's a bit confusing for me as in how to compare the coordinates etc. Any ideas?
Hm...well I think the way you want to do it would be a bit detailed. It seems like coordinates could still work, but sometimes the top or bottom of the mouth would go through a ball, rather than having the ball just go right inside the mouth...Unless you can just get the right spacing so that the opening and closing of the mouth is in sync with hitting the balls...
Wat do you mean ?:rolleyes:
The solution you require is finding out if a point is inside a circle. I think probably the best way to do this is:
Assumptions:
Pacman is its own graphic - ie: a 30x30 .bmp (pixel and file format are arbitrary - the math is the concentration)
Pacman's radius is =~ 15 (ie the circle takes up the full bitmap
Pacman's center is the drawing location of the bitmap plus radius
The byte sized ball he eats is 10/10
The byte sized ball has a radius of 5
The byte sized ball's center is its drawn location + radius
To find out if pacman is eating any givin ball on the screen compare the distance of pacmans center to the desired ball's center. If the distance is equal to or less than the radius of pacman, you are eating the ball.
Code:// Psuedo code'ish
pmanCenter.x = pman.X // where these are the coordinates on screen (pixel) you will start
pmanCenter.y = pman.y // drawing the bit map
pmanCenter.x += pmanRadius
pmanCenter.y += pmanRadius
byteCenter.x = byte.x // screen coordinates you start drawing byte ball
byteCenter.y = byte.y
byteCenter.x += byteRadius
byteCenter.y += byteRadius
dx = byteCenter.x - pmanCenter.x
dy = byteCenter.y - pmanCenter.y
dist = sqrt( dx*dx + dy*dy )
if( dist <= pmanRadius )
// the ball was eaten!
THnx, but no. I want the ball only to be eaten if it is inside the pacman's mouth, not just inside pacman's outline. I didn't say im making pacman, im just using pacman as an example, since my sprite should be able to turn and move in 360 degrees.
thnx
well guess what :
thats part of the answer still - but now its just something to eliminate potential's that arnt close enough to be eaten.
After you've found something that is CLOSE ENOUGH to be eaten, then you find out if it is eaten. Get the enclosing points of the moth, for pacman example its 3 poitns (a triangle). (this would be much easier for a 4 point mount (rectanguler) - perhaps you could simplify the mouth and make it (progmaticly) a 4 point area - which sould be close enough really.
Continuing on the 4 point path - all you will have to do is check to see if the byte ball center is between all four points of the mouth. (Make sure you take the 2d coordinates rotate them to find out what they are after the points are rotated to match the mouth orientation).
But now you have an awkwardly rotated rectangle - which is still difficult to handle - what to do? Grab all the points of the mouth + the point of the bite ball center, and rotate them so teh mouth rectangle is nicely parallel witht he x -axis. (ie rotate by -mouth orientation) then check to see if the opint is in the bounding box.
If you went the direction of a 3 point mouth, you wont have to worry about rotating the ball point and the mouth points to the - orientation for nice ness, but it has its own negative side. For all 3 sides of the 3 point triangle you have to find the slope between each vertex, and test to see if the byte ball center point is on a certan side of the line. Do this for all 3 sides, and if the ball is on the correct side of the line for each, then its in the mouth.
A bit more math heavy there.
Hope that helped.
Hey,
I found out the answer :D . For future readers, my mouth (the pacman's) is a triangle, so we need to know if a point is inside a triangle. To do this follow these steps:
byeCode:1) Get the formula for the area of a triangle given the three vertices.
2) Calculate the area of the triangle.
3) Use the point as a new vertix and calculate the areas of the three
triangles formed by the point and the three sides of the original triangle.
4) If the sum of the areas of those three triangles is the same as the area
of the original triangle, then the point is within the triangle. If the sum
is greater, the point is outside the triangle.
what happens if the point is on the oppisite side of the mouth ? (back of the head, it may still have the same area or smaller, but be in the wrong direction.
A point is either inside or outside. I can do the radius distance check first.
The problem im having now is how to allow the point to pass through the outline of the pacman thing to enter the mouth...<thinkin>
1) Check if it's inside the circle (easy, using pythagoras)
2) Get the angle between the food and the center of the circle. If it's between the angles of Pac-man's 'jaws', then it's in the mouth. Angles can be calculated with arctan or some other trig function.
to add to what magos said the dotproduct between two normalized vectors is the cosine of the angle between the vectors.