November 28, 2020, 10:10:26 AM

Author Topic: [bb] Collision2.0 - Motion based, very accurate. by AntonyWells [ 1+ years ago ]  (Read 624 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Collision2.0 - Motion based, very accurate.
Author : AntonyWells
Posted : 1+ years ago

Description : This lib lets you check for collisions with much greater accuracy than blitz's current method.
As it will register collisions even if images pass over each wholey. (I.e if you're ship as moving at 25 pixels and in one frames goes over a 12 pixel image completely, this lib will still register the collision)

It does this by generating(In hardware) motion maps for each collision object, and then performing just the single col check as per normal for a standard test.

InitCol(res) lets you set the resolution of the motion maps. Smaller =less accurate but faster.
id=CreateCol(image) creates a collision object. Create one for every image.

Then to update it, just call moveCol(col,x,y) once per frame.

And that's it, just checkCol(col1,col2) and it'll return true if they have collided, even if they moved 600 pixels in one frame.

CheckCol has a skip rate...The higher the skip rate the faster the motion map generation, but the less accurate it'll be.

The example code will work with any image, but just delete it when you need the lib for your project.

there's a little demo in there..just hit left mouse button..it draws the two motion maps on the top left hand corner of the screen. And it tells when a collision has occured.


Code :
Code: BlitzBasic
  1. ;-quick example
  2.  
  3. initcol(64) ;init system with 64pixel motion map
  4.  
  5. col1=createCol(myImage) create a col image.call this once for each unique image.
  6.  
  7. moveCol(col1,20,20) ;move it. call this once per frame.
  8.  
  9. and finally,
  10.  
  11. checkCol(col1,col2,rate) to check for a collision.
  12. rate= skip rate, see above.
  13.  
  14.  
  15. ;-[=-]
  16. Type cSys
  17.         Field mRes,iRes
  18.         Field xs#,ys#
  19.         Field gw#,gh#
  20. End Type
  21. Type col
  22.         Field id,mMap ;low res version and motion map.
  23.         Field lx#,ly#,x#,y#
  24. End Type
  25.  
  26.  
  27. Global cSys.cSys
  28. Function initCols(mRes=256)
  29.         cSys=New cSys
  30.         csysmRes=mRes
  31.         cSysiRes=iRes
  32.         cSysgw=GraphicsWidth()
  33.         cSysgh=GraphicsHeight()
  34.         TFormFilter False
  35. End Function
  36. Function createCol(image)
  37.         col.col=New col
  38.         colid=CopyImage(image)
  39.         colmMap=CreateImage(csysmRes,csysmres)
  40.         wd#=Float(ImageWidth(image))/Float(GraphicsWidth())
  41.         hd#=Float(ImageHeight(image))/Float(GraphicsHeight())
  42.         nw#=csysmres*wd
  43.         nh#=csysmres*hd
  44.         ResizeImage colid,nw,nh
  45.         Return Handle(col)
  46. End Function
  47.  
  48. Function moveCol(col,x#,y#) ;move it to the same x,y as the image
  49.         in.col=Object.col(col)
  50.         inlx=inx
  51.         inly=iny
  52.         inx=x
  53.         iny=y
  54. End Function
  55.  
  56. Function checkCol(col1,col2,skipRate=5) ;higher skip rate = faster but less accurate.Play with it, diff shapes will fair differantly
  57.         c1.col=Object.col(col1)
  58.         If c1=Null Return
  59.         c2.col=Object.col(col2)
  60.         If c2=Null Return
  61.         ;-
  62.         ;-[ Generate motion maps]
  63.         genMotionMap(c1,skipRate)
  64.         genMotionMap(c2,skipRate)
  65.         Return ImagesCollide(c1mmap,0,0,0,c2mmap,0,0,0)
  66. End Function
  67.  
  68. Function genMotionMap(in.col,skip=1) ;INTERNAL FUNCTION -
  69.         ax#=inx
  70.         ay#=iny
  71.         ex#=inlx
  72.         ey#=inly
  73.         ax=csysmres*(ax/csysgw)
  74.         ay=csysmres*(ay/csysgh)
  75.         ex=csysmres*(ex/csysgw)
  76.         ey=csysmres*(ey/csysgh) ;scale coords into the res of the motion map
  77.        
  78.         xd#=ex-ax
  79.         yd#=ey-ay
  80.         If Abs(xd)>Abs(yd) steps#=Abs(xd) Else steps#=Abs(yd)
  81.         xi#=xd/Steps
  82.         yi#=yd/Steps
  83.         SetBuffer ImageBuffer(inmMap)
  84.         sImg=inid
  85.         xi=xi*Float(skip)
  86.         yi=yi*Float(skip)
  87.         Cls
  88.         If steps<1 steps=1
  89.         Repeat
  90.                         ;-
  91.                         DrawImage sImg,ax,ay
  92.                         ax=ax+xi
  93.                         ay=ay+yi
  94.                 ;-
  95.                 steps=steps-skip
  96.         Until steps<1
  97. End Function


Comments :


Rck(Posted 1+ years ago)

 missing comment statements dude, run your code first then do a clean copy-paste next time


Q(Posted 1+ years ago)

 ^What he said.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal