|
Post by Wayne Rayner on May 26, 2010 1:53:22 GMT -5
Ok so this a test for my tutorial and in fact the first part of the platform game tutorial.
The problem is that my collision detection doesn't work. So can someone please teach me how to do collision detection.
here is the code I have got.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Set-Up ResizeSpriteArea(800,600) '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Variables and constants dim velocity# = 1 dim platform, block dim posX# = 300 dim posY# = 100 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Load and place sprite/images platform = NewSprite(LoadTex("platform.png")) SprSetSize (400,20) SprSetPos (200,480) BindSprite (platform)
block = NewSprite(LoadTex("block.png")) SprSetSize (32,32) SprSetPos (posX#,PosY#) SprSetVel(0,velocity#) BindSprite (block) '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' while true
' Collision Test if SprTop (platform) and SprBottom(block) then velocity# = 0 endif AnimateSprites() wend
Your help would be wonderfull.
Yours
Wayne Rayner
|
|
|
Post by Supermonkey on May 26, 2010 4:53:43 GMT -5
' Collision Test if SprTop (platform) = SprBottom(block) then velocity# = 0 SprSetVel(0,velocity#) endif
I didn't know this either, never used the sprite library but you have to "reset" the sprite velocity too. When you use SprSetVel it copies the value of velocity# at that moment in time and stores it internally. Therefore when you change the velocity value, you're not actually changing the value stored for the sprite.
Hope that helps.
|
|
|
Post by Wayne Rayner on May 26, 2010 5:21:31 GMT -5
Ok now the block doesn't move at all. Owell I will try and figure out why.
I still don't like this sprite library that basic4GL uses
|
|
|
Post by shadow008 on May 26, 2010 7:05:54 GMT -5
Regardless to what Supermonkey said, your using the SprSetVel correct. Setting the velocity to (0,0) WILL stop the sprite - but only if you bind the CORRECT SPRITE with BindSprite().
In terms of your problem, i figured that box collision was the easiest. Get the x and y size of the box and test them for collision. Look back to my Ninjas demo i posted way back then. What your doing is testing for collision only if two PRECISE LINES coincide with each other.... Slim chance of that happening... Google "axis aligned bounding box" (usually shortened to AABB) Usually for 3D but the concepts can be applied to 2D easily.
Something like this where X is the x size of one sprite and A is the x size of another
if Xmax > Amin and Xmin < Amax then....
|
|
|
Post by Supermonkey on May 26, 2010 7:38:16 GMT -5
Did you copy and paste the code Wayne? cause you were doing the collision check totally wrong. Also shadow his usage of SetSprVel was not correct. Wayne was attempting to stop the object from falling by setting velocity# = 0, which does not affect the sprite. To set the sprite velocity to 0 you need to call SetSprVel(0,0) as I stated above. In terms of how he was doing collision detection, the method Wayne was using wasn't great but as a gravity test it worked fine.
A good workman never blames his tools, however I do think there are better libraries out there.
|
|
|
Post by Wayne Rayner on May 26, 2010 9:17:23 GMT -5
Thanks supermonkey it does work I must of missed a bit of code when I copied and pasted it (Gee I'm useless).
Well I know my collision detection isn't good at all, but the box collision I just can't seem to get my head around it at all.
I just wish I could learn a better way for collision detection.
Thanks anyways
Wayne Rayner
|
|
|
Post by Supermonkey on May 26, 2010 10:01:57 GMT -5
Wayne you have the basic idea already, which is why I didn't overload you by modifying the code too much. You are, in effect testing one edge of the box. Here's some untested code to show how box collision could work:
if SprBottom(block) > SprTop(platform) then ' Tests bottom of block collide with top of platform if SprTop(block) < SprBottom(platform) then ' Test top of block is above platform. if SprRight(block) > SprLeft(block) then ' Test right of block is collide with left of platform If SprLeft(block) < SprRight(platform) then ' Test left of block is to right of platform 'COLLIDE! Endif Endif Endif Endif
The code is a bit crude, but it's all about checking if one rectangle is within the bounds of another rectangle.
[edit] I thought I'd test it out here's some code to show you how to do bounds checking.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Set-Up ResizeSpriteArea(800,600) '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Variables and constants dim velocity# = 0 dim platform, block dim posX# = 300 dim posY# = 100 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Load and place sprite/images platform = NewSprite(LoadTex("platform.png")) SprSetSize (400,20) SprSetPos (200,480) BindSprite (platform)
block = NewSprite(LoadTex("block.png")) SprSetSize (32,32) SprSetPos (posX#,PosY#) SprSetVel(0,velocity#) BindSprite (block) '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' while true if scankeydown(VK_LEFT) then posX# = posX# - 3 endif if scankeydown(VK_RIGHT) then posX# = posX# + 3 endif if scankeydown(VK_DOWN) then posY# = posY# + 3 endif if scankeydown(VK_UP) then posY# = posY# - 3 endif
SprSetPos(posX#, posY#)
' Collision Test if SprBottom(block) > SprTop(platform) then ' Tests bottom of block collide with top of platform if SprTop(block) < SprBottom(platform) then ' Test top of block is above platform. if SprRight(block) > SprLeft(block) then ' Test right of block is collide with left of platform If SprLeft(block) < SprRight(platform) then ' Test left of block is to right of platform printr "Collide!" Endif Endif Endif Endif
AnimateSprites() wend
|
|
|
Post by shadow008 on May 26, 2010 11:27:04 GMT -5
' Collision Test if SprTop (platform) = SprBottom(block) then velocity# = 0 SprSetVel(0,velocity#) endif
I didn't know this either, never used the sprite library but you have to "reset" the sprite velocity too. When you use SprSetVel it copies the value of velocity# at that moment in time and stores it internally. Therefore when you change the velocity value, you're not actually changing the value stored for the sprite. Hope that helps. Ouch >.<.... i guess i missed that when i looked at his code But Wayne, box collision is really easy, its just testing if the side is within a box (which is just a bunch of greater than's (>) and less than's(<)). Im sure you'll get it in time .
|
|
|
Post by Nicky Peter Hollyoake on May 27, 2010 13:22:33 GMT -5
It's really good to learn by yourself. But if you want an easy way out, here's my sprite collision function. function SpriteCollide(S1, S2) Dim X1#, Y1# Dim X2#, Y2# 'Sprite 1 X1# = SprX(S1)-(SprXSize(S1)*SprXCentre(S1)) 'X Y1# = SprY(S1)-(SprYSize(S1)*SprYCentre(S1)) 'Y 'Sprite 2 X2# = SprX(S2)-(SprXSize(S2)*SprXCentre(S2)) 'X Y2# = SprY(S2)-(SprYSize(S2)*SprYCentre(S2)) 'Y
return X1#+SprXSize(S1)>=X2# AND X1#<=X2#+SprXSize(S2) AND Y1#+SprYSize(S1)>=Y2# AND Y1#<=Y2#+SprYSize(S2) endfunction Enjoy! By the way, distance collision is the easiest collision (in my opinion), you might wanna check it out. - Nicky
|
|
|
Post by Wayne Rayner on May 28, 2010 23:37:00 GMT -5
ok thanks Nicky, the collision works well. I like it a lot and yes I am also learning how to do collision detection my self and I will look into distance collision as well
thanks again nicky
|
|