A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: Check between points

  1. #1
    Game Master ConnELITE's Avatar
    Join Date
    Apr 2005
    Location
    United States, DC
    Posts
    474

    Check between points

    Hey, I need to do a check between two points to see if there are any obstructions and I have no idea what to do.

    For example, does anyone have any good ideas on how to say find every point between (1,1) and (10,6) and also check if any of those points fall insde of element box?

    Thanks.
    BC

  2. #2
    Senior Member
    Join Date
    Dec 2006
    Posts
    274
    Well... in theory something like this
    if(x>=1 && x<=10 && y>=1 && y=<6){
    // we are inside..
    }

    better yet to make functions that check these things..
    function isInsideBox(x,y,box)...
    function isInside(sx,sy, x1,y1,x2,y2)...

  3. #3
    Game Master ConnELITE's Avatar
    Join Date
    Apr 2005
    Location
    United States, DC
    Posts
    474
    Thanks for the idea, but could you elaborate a little more? I get where your going with it but I'm not sure I quite understand.

    What I have so far is basicaly I calculate the distance from point a to point b. I convert that to an integer, find the slope, then get 100 points between those two points and dump them into an array. I run the array against the boxes bounds to see if any points collide with the box.

    I was wondering if anyone had any better more efficient methods because this leaves room for a lot of error and will most likely slows down the program quite a bit if it was under stress.

    Thanks.
    Last edited by ConnELITE; 04-03-2007 at 02:22 PM.
    BC

  4. #4
    Senior Member
    Join Date
    Dec 2006
    Posts
    274
    Quote Originally Posted by ConnELITE
    Hey, I need to do a check between two points to see if there are any obstructions and I have no idea what to do.
    I totally missed this line or it wasn't there last time !?
    Anyway now I understand what you are after but I have no real solution for this, other that that slow method you wrote. One little speedup (if there are lot of elements) could be to isolate which elements fall inside the "box" of "points between (1,1) and (10,6)". Then perform crude collision check between line vs. every isolated "element boxes" . After that you perform slow per pixel line check for elements that were catched by crude check.
    Sorry, just idea, no code

  5. #5
    Game Master ConnELITE's Avatar
    Join Date
    Apr 2005
    Location
    United States, DC
    Posts
    474
    Hi, what do you think of this?

    for(x=first_points_x;x<int_distance_between_points ;x++)
    {
    y=x*slope;
    if(y>box.ymin && y<box.ymax)
    {
    if(x>box.xmin && x<box.xmax)
    {
    collision=true
    break;
    }
    }

    In this, if the two points were (0,0) and (6,6) it would run 6 times (distance formula). If there was a box that contained point (3,3) it would return collision=true.

    I relize the syntax is a little off and this is assuming I only have one box, but does anyone have any better ideas or see anything wrong with this?

    Thanks.
    BC

  6. #6
    Senior Member
    Join Date
    Dec 2006
    Posts
    274
    Not much to add, just loop whatever is larger, x or y and use "slope" to smaller one, otherwise you may miss some of the points in situations where y_distance > x_distance

  7. #7
    Senior Member
    Join Date
    May 2005
    Posts
    163
    I'm not sure what your tring to do but to me it sounds like your looking to use a MAP.

    To do this you setup an array that represents the x by y grid of your screen. So for ex. if your screen was 60 x 60 and each grid was 6 x 6 you would need a 10 x 10 array to keep track of each box on the grid. To find out where an object is you would divide the x and y coord by 6 to give you the location in array.

    Just another idea.

  8. #8
    Game Master ConnELITE's Avatar
    Join Date
    Apr 2005
    Location
    United States, DC
    Posts
    474
    Well, I actually have a tank game and damage is determined by where you click. I want to add walls but I need a way to find out if there's a wall in way of your shot. I'm having a pretty difficult time with this. I have to limit the amount of points it checks to keep it fast while keeping it accurate. I'm still open to ideas since I'm not very comfortable with the one I made since it has a lot of room for error and will possible lag the game under stress.

    I don't think the grid system will work ppedz because the walls are moving and appear in random places every time but thanks anyway.
    BC

  9. #9
    Senior Member
    Join Date
    May 2005
    Posts
    163
    I think a tank game would be an ideal candidate for the MAP concept. To give you some idea of how it works take a look at my ISOMETRIC demo I have at my site http://www.myjavaserver.com/~ppedz/3dfa. As far as the walls moving all you do is update the map to its new location. If something is occupying the space then you have a collision. You can make different walls by adding blocks together. Take a look at the demo, I think it will help.

  10. #10
    Senior Member kusco's Avatar
    Join Date
    Nov 2001
    Posts
    681
    There is actually a neat mathematical solution to this problem. If you have a line between two points ax,ay and bx,by ... then you can determine the distance of any point px,py from the line using the following algorithm.

    var t1 = bx - ax;
    var t2 = ay - by;
    var t3 = ax - px;
    var t4 = ay - py;
    var ln = sqrt (t1*t1 + t2*t2);
    var dp = (t1 * t4 + t3 * t2) / ln;
    var dq = (t4 * t2 - t3 * t1) / ln;

    dp will be the perpendicular distance from point px,py to a line of infinite length that passes through ax,ay and bx,by. If 1 > dp > -1 then you are right on the line.

    dq will give you the perpendicular intersection. If 0 < dq < ln ... then the intersection is between points a and b.

    For these kinds of problems, there is almost always a neat mathematical solution which you will find explained on any one of a thousand good vector math websites. Vector mathematics often allows you to do amazing things with only a few calculations
    Last edited by kusco; 04-06-2007 at 12:22 AM.
    Cheers,
    kusco
    (3DFA Support Team)

  11. #11
    Game Master ConnELITE's Avatar
    Join Date
    Apr 2005
    Location
    United States, DC
    Posts
    474
    ThanK you very much Kusco! When I posted this I was actually hoping for someone to give me a neat formula. I'll see if I can turn this into a function and apply it to my game. Thanks for helping everyone.
    BC

  12. #12
    Senior Member sadako232's Avatar
    Join Date
    Mar 2006
    Posts
    153
    is there an example of use?

  13. #13
    Game Master ConnELITE's Avatar
    Join Date
    Apr 2005
    Location
    United States, DC
    Posts
    474
    Well this was my first test...

    ax=mouseX()
    ay=mouseY()
    bx=element ("testele").x
    by=element ("testele").y
    px=element ("testcheck").x
    py=element ("testcheck").y

    var t1 = bx - ax;
    var t2 = ay - by;
    var t3 = ax - px;
    var t4 = ay - py;
    var ln = sqrt (t1*t1 + t2*t2);
    var dp = (t1 * t4 + t3 * t2) / ln;
    var dq = (t4 * t2 - t3 * t1) / ln;

    if((dp<1) && (dp>-1) && (dq>0) && (dq<ln))
    {
    test=true
    }
    else
    {
    test=false
    }

    Works fine!
    BC

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center