04-09-2024, 09:08 AM
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...
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...
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
endfuncUnfortunately 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.
Live long and prosper.
Live long and prosper.

