March 06, 2021, 04:07:51 AM
Welcome,
Guest
. Please
login
or
register
.
Did you miss your
activation email
?
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
Like stats
Home
Forum
Help
Search
Gallery
Login
Register
SyntaxBomb - Indie Coders
»
Languages & Coding
»
Blitz Code Archives
»
Graphics
»
[bb] Collision2.0 - Motion based, very accurate. by AntonyWells [ 1+ years ago ]
« previous
next »
Print
Pages: [
1
]
Go Down
Author
Topic: [bb] Collision2.0 - Motion based, very accurate. by AntonyWells [ 1+ years ago ] (Read 668 times)
BlitzBot
Jr. Member
Posts: 1
Total likes: 0
[bb] Collision2.0 - Motion based, very accurate. by AntonyWells [ 1+ years ago ]
«
on:
June 29, 2017, 12:28:43 AM »
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
;-quick example
initcol
(
64
)
;init system with 64pixel motion map
col1=createCol
(
myImage
)
create a col image.call this once
for
each
unique image.
moveCol
(
col1,
20
,
20
)
;move it. call this once per frame.
and
finally,
checkCol
(
col1,col2,rate
)
to
check
for
a collision.
rate= skip rate, see above.
;-[=-]
Type
cSys
Field
mRes,iRes
Field
xs#,ys#
Field
gw#,gh#
End Type
Type
col
Field
id,mMap
;low res version and motion map.
Field
lx#,ly#,x#,y#
End Type
Global
cSys.cSys
Function
initCols
(
mRes=
256
)
cSys=
New
cSys
csysmRes=mRes
cSysiRes=iRes
cSysgw=
GraphicsWidth
(
)
cSysgh=
GraphicsHeight
(
)
TFormFilter
False
End Function
Function
createCol
(
image
)
col.col=
New
col
colid=
CopyImage
(
image
)
colmMap=
CreateImage
(
csysmRes,csysmres
)
wd#=
Float
(
ImageWidth
(
image
)
)
/
Float
(
GraphicsWidth
(
)
)
hd#=
Float
(
ImageHeight
(
image
)
)
/
Float
(
GraphicsHeight
(
)
)
nw#=csysmres*wd
nh#=csysmres*hd
ResizeImage
colid,nw,nh
Return
Handle
(
col
)
End Function
Function
moveCol
(
col,x#,y#
)
;move it to the same x,y as the image
in.col=Object.col
(
col
)
inlx=inx
inly=iny
inx=x
iny=y
End Function
Function
checkCol
(
col1,col2,skipRate=
5
)
;higher skip rate = faster but less accurate.Play with it, diff shapes will fair differantly
c1.col=Object.col
(
col1
)
If
c1=
Null
Return
c2.col=Object.col
(
col2
)
If
c2=
Null
Return
;-
;-[ Generate motion maps]
genMotionMap
(
c1,skipRate
)
genMotionMap
(
c2,skipRate
)
Return
ImagesCollide
(
c1mmap,
0
,
0
,
0
,c2mmap,
0
,
0
,
0
)
End Function
Function
genMotionMap
(
in.col,skip=
1
)
;INTERNAL FUNCTION -
ax#=inx
ay#=iny
ex#=inlx
ey#=inly
ax=csysmres*
(
ax/csysgw
)
ay=csysmres*
(
ay/csysgh
)
ex=csysmres*
(
ex/csysgw
)
ey=csysmres*
(
ey/csysgh
)
;scale coords into the res of the motion map
xd#=ex-ax
yd#=ey-ay
If
Abs
(
xd
)
>
Abs
(
yd
)
steps#=
Abs
(
xd
)
Else
steps#=
Abs
(
yd
)
xi#=xd/Steps
yi#=yd/Steps
SetBuffer
ImageBuffer
(
inmMap
)
sImg=inid
xi=xi*
Float
(
skip
)
yi=yi*
Float
(
skip
)
Cls
If
steps<
1
steps=
1
Repeat
;-
DrawImage
sImg,ax,ay
ax=ax+xi
ay=ay+yi
;-
steps=steps-skip
Until
steps<
1
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.
Logged
Print
Pages: [
1
]
Go Up
« previous
next »
SyntaxBomb - Indie Coders
»
Languages & Coding
»
Blitz Code Archives
»
Graphics
»
[bb] Collision2.0 - Motion based, very accurate. by AntonyWells [ 1+ years ago ]
SimplePortal 2.3.6 © 2008-2014, SimplePortal