| FBA The Creator http://www.fbacreator.com/fbaforum/ |
|
| Coding problem - can you help solve? http://www.fbacreator.com/fbaforum/viewtopic.php?f=5&t=70 |
Page 1 of 1 |
| Author: | Boiled Sweets [ Sun Jun 29, 2008 4:33 pm ] |
| Post subject: | Coding problem - can you help solve? |
OK clever people here goes, a nice puzzle for Sunday evening for those not watching Spain beat Germany Imagine drawing a line from the screen top left to the bottom right. Then draw a line from the screen top right to the bottom left. So you have drawn a big X, dividing the screen into 4 triangles. The user then taps the screen with their stylus, I need to know which 'triangle' the stylus is in. Any ideas on the maths behind this? Thanks in advance Of course you only really need to know if it's in the top or bottom triangle, cos if it's not then if the X position is less than the screen width divided by 2 then its in the left triangle and if not then it's in the right. So how can I detect whether it's in the top or bottom triangle? |
|
| Author: | umbiomar [ Mon Jun 30, 2008 8:32 am ] |
| Post subject: | Re: Coding problem - can you help solve? |
Dont need to reinvent the wheel, there'are lots of algorithm solving this problem (google for "point inside a polygon"). The one I used in one of my games use an array of point (x,y) defining a closed polyline. I dont remember the url, I'll give a look as soon as Ill go home. |
|
| Author: | umbiomar [ Mon Jun 30, 2008 1:53 pm ] |
| Post subject: | Re: Coding problem - can you help solve? |
OK, i recover that old code I used to detect point inside a polygon with 4 vertex (here named "quad"), the source is C++ because that code was written in c++ If i got some time i'll change it to lua syntax. hope this helps. Code: class Quad {
|
|
| Author: | phrebh [ Mon Jun 30, 2008 3:46 pm ] |
| Post subject: | Re: Coding problem - can you help solve? |
After searching for a while and testing a lot of different code, I came up with this function for FBA. Code: function pointInPolygon(x, y, polyX, polyY) It should work for any polygon, complex or simple. The arguments required are the x and y coordinates to test and two arrays: the first gives the x coordinates of your polygon's vertices, and the second gives the y coordinates of your polygon's vertices. Here is my entire test project code: Code: xcoords={0,239,119}
|
|
| Author: | Boiled Sweets [ Tue Jul 01, 2008 7:47 am ] |
| Post subject: | Re: Coding problem - can you help solve? |
Outstanding work!!! I really appreciate this, demo coming soon... |
|
| Author: | evilmaio [ Tue Jul 01, 2008 8:04 pm ] | ||||
| Post subject: | Re: Coding problem - can you help solve? | ||||
Let me suggest an alternative way to solve your problem using maps with in-memory pictures. Two notes before you jump out of your window by happines 1 - for your case, is better to use a mathematical approach, because you can easy track points inside triangles using previous solutions; you will save memory needed by an hidden mask ! (note: memory needed for an image is <width * height * 2> bytes, so for a full screen mask you'll need 240*320*2 = 153600 bytes, lot of memory, imho !) 2 - this method could be used if you have very complex maps where you'd like to track points (i can't figure any example now btw Actually i don't know why i posted that...just to show some different code
|
|||||
| Author: | phrebh [ Tue Jul 01, 2008 8:43 pm ] |
| Post subject: | Re: Coding problem - can you help solve? |
That certainly looks like a good way of check for really complex shapes. One thing, though: whenever I clicked in one of the triangles, it reported my position as the bottom until I moved my mouse again. It seems like I've read about that issue before, but I'm not sure. |
|
| Author: | evilmaio [ Tue Jul 01, 2008 8:59 pm ] |
| Post subject: | Re: Coding problem - can you help solve? |
This isn't really a bug...but how currently FBA works This is what happens inside FBA... When the application triggers mousedown/move/up.. FBA updates an internal variable (let's say CURRENTMOUSEX and CURRENTMOUSEY) with current mouse position.. FBA functions mousex() and mousey() return values CURRENTMOUSEX and CURRENTMOUSEY When FBA is launched CURRENTMOUSEX and CURRENTMOUSEY are set to -1 When FBA receives the mousedown/move events, it updates CURRENTMOUSEX and CURRENTMOUSEY with current position. When FBA receives the mouseup event, it sets CURRENTMOUSEX and CURRENTMOUSEY to -1.. I wonder if it's better to leave them as is when FBA get the mouseup event... (My original idea was: if we aren't clicking the stylus, keeping latest CURRENTMOUSEX/Y is not correct, that's why FBA reset them to -1) I updated the zip in my previous post with a way to avoid the "-1" weird behaviour |
|
| Author: | Boiled Sweets [ Tue Jul 01, 2008 9:10 pm ] |
| Post subject: | Re: Coding problem - can you help solve? |
Implemented using phrebh maths... Works great and seems pretty fast. I think it's a really useful exmaple that should be added to the FBA examples for the next release. I used the co-ords... Code: -- TABLES |
|
| Author: | evilmaio [ Tue Jul 01, 2008 9:13 pm ] |
| Post subject: | Re: Coding problem - can you help solve? |
Feel free to update the showcase area with your samples/tutorials/full projects |
|
| Page 1 of 1 | All times are UTC |
| Powered by phpBB® Forum Software © phpBB Limited https://www.phpbb.com/ |
|