1. Collision Detection
- Posted by Andy Aug 08, 2009
- 1095 views
- Last edited Aug 09, 2009
Hey Guys,
I was playing around with Euphoria, and I was wondering how I would add some collision detection to this program? Its pretty simple, I just wanna add some collision detection, so I know when the images intersect with each other. Here's the code.
without warning without type_check include win32lib.ew include file.e include get.e include misc.e include wildcard.e if platform() != WIN32 then puts(1,"Must run under Windows!\n") abort(1) end if atom SizeX, SizeY SizeX = 640 SizeY = 480 constant Win = createEx(Window,"Simple Game",0,Default,Default,SizeX,SizeY,0,0), Stat = createEx(StatusBar,"",Win,0,0,0,0,0,0) atom Guy, Guy2 Guy = loadBitmapFromFile("Stick.bmp") Guy2 = loadBitmapFromFile("Stick2.bmp") atom X, Y, X2, Y2 X = 180 Y = 35 X2 = 280 Y2 = 35 procedure GameLoop(integer self, integer event, sequence parm) integer key key = parm[1] drawBitmap(Win,Guy,X,Y) drawBitmap(Win,Guy2,X2,Y2) if key = VK_ESCAPE then closeWindow(Win) end if wPrintf({Win,1,1},"X:%d",{X}) wPrintf({Win,1,12},"Y:%d",{Y}) wPrintf({Win,1,25},"X2:%d",{X2}) wPrintf({Win,1,42},"Y2:%d",{Y2}) if key = VK_LEFT then X = X - 5 X2 = X2 + 5 drawBitmap(Win,Guy,X,Y) repaintWindow(Win) elsif key = VK_RIGHT then X = X + 5 X2 = X2 - 5 drawBitmap(Win,Guy,X,Y) repaintWindow(Win) elsif key = VK_UP then Y = Y - 5 Y2 = Y2 + 5 drawBitmap(Win,Guy,X,Y) repaintWindow(Win) elsif key = VK_DOWN then Y = Y + 5 Y2 = Y2 - 5 drawBitmap(Win,Guy,X,Y) repaintWindow(Win) elsif X and Y = X2 and Y2 then setText(Stat,"Images Hit") end if end procedure setHandler(Win,w32HPaint,routine_id("GameLoop")) setHandler(Win,w32HKeyDown,routine_id("GameLoop")) WinMain(Win,Normal)
I plan on adding some other features to it later on, but I wanna get the collision detection down first.
2. Re: Collision Detection
- Posted by useless Aug 09, 2009
- 1081 views
I'd have thought you'd see if the two sticks shared the same bits in the bitmap. If they do, then they've collided. To see if they are near colliding, search for shared bits nearby.
useless
3. Re: Collision Detection
- Posted by prickle Aug 09, 2009
- 1069 views
A simple method might be bounding boxes. Depending on the number of guys you have on the screen, it may be acceptably fast.
If we have the following functions:
--sign of r function sgn(atom r) if r < 0 then return -1 else return 1 end if end function --absolute value of r global function abs(atom r) return sgn(r) * r end function
(BTW, have these been included in more recent versions of Euphoria? If not, may I suggest them?)
We also need the size of your guys:
atom sx, sy, sx2, sy2 sx = length(Guy[1][1]) sy = length(Guy[1]) sx2 = length(Guy2[1][1]) sy2 = length(Guy2[1])
(it still works this way, right?)
We then precalculate the difference between the midpoints of the guys when they are just touching.
atom dx, dy dx = floor((sx + sx2) / 2) dy = floor((sy + sy2) / 2)
Then, where you have:
elsif X and Y = X2 and Y2 then setText(Stat,"Images Hit") end if
We can say:
elsif abs(X - X2) < dx and abs(Y - Y2) < dy then setText(Stat,"Images Hit") end if
Is it obvious what we are doing here? We are finding the difference between the positions of the guys and seeing if it is small enough for them to be touching. Of course this gives one a rectangular bounding box around a guy which may not be ideal for irregular shapes, but it is probably the cheapest, simplest method of collision detection I know.
Hope this helps.
Cheers,
Nick
4. Re: Collision Detection
- Posted by gaz Aug 09, 2009
- 1084 views
This is a boundbox collision detection function
The first two arguments are the first image x and y positions and
the next two arguments are the width and height of the first image. and
the next four arguments are the x and y position and width and height of the second image.
function check_bb_collision_general(integer x1,integer y1,integer w1,integer h1,integer x2,integer y2,integer w2,integer h2) if not (((x1)>=(x2)+(w2)) or ((x2)>=(x1)+(w1)) or ((y1)>=(y2)+(h2)) or ((y2)>=(y1)+(h1))) then return 1 end if return 0 end function
The function returns true or 1 if the boundboxs are overlaping
5. Re: Collision Detection
- Posted by Andy Aug 09, 2009
- 1124 views
Yes, I am slowly getting it. CD isn't super easy for me to understand, but I am getting the grasp of it.