Collision point

BlitzMax Forums/BlitzMax Programming/Collision point

GfK(Posted 2009) [#1]
I figure a diagram is in order for this:



I know the position of the ship, and the position of the enemy. I also know the vector, shown in red.

I can detect a laser-to-ship collision. What I need is exact x/y coordinates of the point of impact.

Ideas?


Wiebo(Posted 2009) [#2]
You could do a simple line intersection calculation. check all four rectangle list against the vector.


GfK(Posted 2009) [#3]
Well, the rectangle was just for illustration. The enemy won't necessarily be rectangular.


Amon(Posted 2009) [#4]
You could store the pixel locations of the target in a lookup table and check collision on that???


Nate the Great(Posted 2009) [#5]
there is a lines intersect function somewhere in the code arcs... Make a rough outline for each enemy and use those as collision "meshes"


Tommo(Posted 2009) [#6]
there is a lines intersect function somewhere in the code arcs...

It seems to be this one:
http://www.blitzbasic.com/codearcs/codearcs.php?code=1676


Jesse(Posted 2009) [#7]
The way I am doing it is with ImagesCollide But it doesn't return a exact point of collision. I was thinking of creating a single pixel image and testing it against the image. If and when the collision occurred then I know that is a close point of collision(speed error), move back one pixel at a time in the direction of the laser until it no longer collide. I seen it done before in some demo code but I really don't know how bad it will be as far as processing speed goes.


BlitzSupport(Posted 2009) [#8]
This might be affected by how you're detecting the collision at the moment, but assuming you can construct a line of some sort from where the laser starts, here's what I would at least try (completely untested, of course, and probably trickier than it sounds)...

· Calculate the slope of the (laser's) line in terms of x-offset and y-offset;

· Use a line/rect intersect function to find where the laser enters the spaceship's bounding image rectangle;

· Start at the intersection point (translated from the image on the screen to the image's pixmap), reading the pixel value from the pixmap, moving along if it matches the mask colour:

While pixmap_pixel_value = mask_color 

    x = x + x_offset
    y = y + y_offset

    ' Check still inside pixmap boundaries, Exit if not!

Wend

' If a non mask_color pixel was found, then collision_x = x; collision_y = y


· When pixel_value <> mask_color, collision has occurred. In theory.

Although it sounds involved, I bet it'd be pretty quick in reality. Of course it might be utter twaddle.


GfK(Posted 2009) [#9]
That sounds feasible... thanks. I'd probably need to check for non-alpha pixels though to get the right result (I'm using PNGs).

I'll give that a crack tomorrow.


BlitzSupport(Posted 2009) [#10]
I was curious enough to have a quick hack at this. I've only dealt with the 'inside pixmap' part of my theory, and assumed the laser starts at 0, 0 with the image also at 0, 0, no scaling, etc, but it gives an idea of how this could work. It's quick enough here, anyway.

IMPORTANT: Save this code into the BlitzMax\samples\hitoro folder so that the image meets the needs of the hacky demo code:



To truly see the awesome power of this demo, change MouseHit to MouseDown!


Czar Flavius(Posted 2009) [#11]
Whenever you code without Strict, a little baby rabbit dies. Even hacky demo code :'(


beanage(Posted 2009) [#12]
I'd suggest (sry for ugly pseudocode):

'for each model outline
   'get intersection
      'if ship.distance( intersection )< ship.distance( previous intersection )
         'previous intersection:= intersection
'return previous intersection



GaryV(Posted 2009) [#13]
Well, the rectangle was just for illustration. The enemy won't necessarily be rectangular.


No Borg War game? :(