I have an isometric game where enemies use mp_grid. To save some time I'd like to check if the randomly selected target point is actually in the playable area
Can you tell us how you are drawing your game? In other words, if we knew how you get from the game's world space to what you see drawn on the view, we could tell you have to reverse the process. EDIT: But in general, if (x,y) is a point in the world, and (x1,y1) is an isometric projection of that point, and the projection can be described like this: x1 = x*a + y*c y1 = x*b + y*d That is, moving 1 unit along the world's x axis, will move you the length and direction of the vector (a,b) in the isometric projection. and moving 1 unit along the world's y axis will move you along a vector (c,d) on the isometric proejction. then the inverse is x = ( x1*d - y1*c)/(a*d - b*c) y = (-y1*b + x1*a)/(a*d - b*c)
You can pass a line from your point in any direction and if it intersects with your quadrilateral an odd number of times, it's inside.
Howdy, Marasovec, One sure-fire shortcut way would be to run collision-in-triangle twice. (Or once if it's true on the first because then you don't need to check the second half of the parallelogram.) I hope this helps. $:^ J
I misunderstood your problem. I presumed you were using an isometric transformation to go from world to view space. I also presumed the random point you wanted to check was a point in view space. However, my original answer can actually be used to provide a speedy solution to your actual problem. Even though it probably will be slower than using the built-in point-in-triangle functions, I'll write this out just because I'm embarrassed about misunderstanding the nature of your problem. Let's say p0 is one of the points of the quadrilateral. And p1, p2 are two other points at the end of the sides coming off of p0. And that p3 is the point you want to check. If (a,b) is p1-p0, and (c,d) is p2-p0. and (x0,y0) is p3-p0. Then you can can check if the point is in that quadrilateral like this: var _id = 1 /(a*d-b*c); var x1 = ( x0*d - y0*c) * _id; var y1 = (-x0*b + y0*a) * _id; var in_quad = (x1 > 0) and (y1 > 0) and (x1 < 1) and (y1 < 1);
Don't worry. I misunderstood a lot of problems here and felt like an idiot many times I'll check your solution out too