Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pixel Collision
#1
I figured that I might try some 'old school' collision detection...

The concept is... A falling box. An area to land on. Using the 'pixel()' command, instruct the box to stop, as it "hits" the ground.

In this example, I am testing the pixel below the box's left bottom edge. (I have placed a reference pixel under the box at the same "y" co-ordinate.)

Press SPACE to drop...

Code:
' Open a window and enable double buffering.
set window "Pixel Collision", 640, 480
set redraw off

visible Box = [255, 255, 0, 64]
visible BoxW = 32
visible BoxH = 48
visible BoxX = (width(primary) - BoxW) / 2
visible BoxY = 0
visible vSpeed = 0
visible drop = false

visible ground = [0, 255, 0]

do

    set color 0, 0, 0
    cls
   
    Update()
    Draw()
   
    redraw
    fwait 30
   
until keydown(KEY_ESCAPE, true)

function Update()
    '   vSpeed
    if keydown(KEY_SPACE, true)     drop = true
    if drop = true  vSpeed = vSpeed + 0.03
   
    '   Box
    BoxY = BoxY + vSpeed
   
    '   Pixel Collision
    if pixel(BoxX, BoxY + BoxH) = ground
        vSpeed = 0
    endif
   
    '   Just in case 'pixel' fails... lol
    if BoxY > height(primary)
        vSpeed = 0
        wait 1000
        end
    endif
endfunc

function Draw()
    '   Ground
    set color ground
    draw rect 0, 460, 640, 20, 0
   
    '   Box
    set color Box
    draw rect BoxX, BoxY, BoxW, BoxH, 0
   
    '   Pixel
   
    '   This indicator pixel is drawn one pixel below the box center.
    '   The actual pixel being tested is below the left corner of the box.
    set color 255, 0, 255
    draw pixel BoxX + 15, BoxY + BoxH
endfunc

Unfortunately this did not work... "Read the manual", I hear you say... "pixel(x,y) Returns the color at position (x, y) as an array [r, g, b, a] with RGBA intensities in the range [0 .. 255].

I am hoping that my programing skills are the source of the problem otherwise this maybe another "can of worms".... sorry...
Logic is the beginning of wisdom.
Reply


Messages In This Thread
Pixel Collision - by johnno56 - 04-09-2024, 09:08 AM
RE: Pixel Collision - by johnno56 - 04-09-2024, 11:25 AM
RE: Pixel Collision - by 1micha.elok - 04-09-2024, 03:37 PM
RE: Pixel Collision - by johnno56 - 04-09-2024, 06:21 PM
RE: Pixel Collision - by 1micha.elok - 04-10-2024, 02:04 AM
RE: Pixel Collision - by johnno56 - 04-10-2024, 02:26 AM
RE: Pixel Collision - by 1micha.elok - 04-10-2024, 03:22 AM
RE: Pixel Collision - by kevin - 04-10-2024, 06:52 AM
RE: Pixel Collision - by 1micha.elok - 04-10-2024, 08:05 AM
RE: Pixel Collision - by johnno56 - 04-11-2024, 01:19 AM
RE: Pixel Collision - by kevin - 04-11-2024, 06:14 AM
RE: Pixel Collision - by johnno56 - 04-11-2024, 09:30 AM

Forum Jump:


Users browsing this thread: 2 Guest(s)