# Math for perfect collision - Help?

## Recommended Posts

I'm struggling with my math and I could use a little help:

The player collides with pfpixel, pfpixel gets turned off, but the collision detection is wonky. The player sprite is 8x8.

x = (player0x-18)/4 : y = (player0y)/8
x = (player0x-18)/4 : y = (player0y)/8
if collision(playfield,player0) then pfpixel x y off

Entire code:

rem Generated 30/01/2020 19:42:08 by Visual bB Version 1.0.0.554
rem **********************************
rem *<filename>                      *
rem *<description>                   *
rem *<author>                        *
rem *<contact info>                  *
rem **********************************

start

set smartbranching on

dim rand16 = z

dim _P0_L_R = player0x.k
dim _P0_U_D = player0y.l

a = 10
player0x = 50
player0y = 10

rem FIX THIS CODE! MAKE IT SPAWN 10 PFPIXELS:
Loop_Lettuce
d = 0
Place_Lettuce

temp1 = (rand&31)
temp2 = (rand&7) - 1
pfpixel temp1 temp2 flip
d = d +1
if d = 10 then goto main
goto Place_Lettuce

main

COLUP0=\$0E
COLUP1=\$33

COLUBK=\$D8
COLUPF= \$DC

rem PLAYER0 Bunny
if a = 10 then player0:
%00110100
%00111100
%01111100
%00111110
%00011101
%00000110
%00000100
%00000100
end

if a = 20 then player0:
%00110000
%00100000
%00011010
%00111100
%00011110
%00001101
%00000010
%00001100
end

if a = 30 then player0:
%00000100
%00001100
%00011110
%00111101
%01110110
%10100100
%00001000
%00000000
end

if a > 30 then a = 0

rem flip p0 sprite
if c = 0 then REFP0=0
if c = 1 then REFP0=8

drawscreen
rem ANIMATE AND MOVE BUNNY
if joy0up then _P0_U_D = _P0_U_D - 0.50 : a = a + 1
if joy0down then _P0_U_D = _P0_U_D + 0.50 : a = a + 1
if joy0left then _P0_L_R = _P0_L_R - 0.50 :  a = a + 1 : c = 1
if joy0right then _P0_L_R = _P0_L_R + 0.50 :  a = a + 1 : c = 0
if !joy0left && !joy0right && !joy0up && !joy0down then a = 10

rem BUNNY EATS LETTUCE
x = (player0x-18)/4 : y = (player0y)/8
x = (player0x-18)/4 : y = (player0y)/8

if collision(playfield,player0) then pfpixel x y off

goto main

Edited by Captain Spazer

##### Share on other sites

The numbers are slightly different depending on which direction you move. Make a test program where you turn on pixels, play with the numbers until they are perfect, then use those numbers in your program.

##### Share on other sites

Ahh, that was very hard, but I got it working now that I understood that the direction of movement mattered.

##### Share on other sites

While on the subject of math, how would I go about looping the spawn of the pfpixels for example 10 times?

My current code seems to be wonky, sometimes I get 8 pfpixels, sometimes 9 and sometimes 10:

Loop_Lettuce
d = 0
Place_Lettuce

temp1 = (rand&31)
temp2 = (rand&14) - 1
pfpixel temp1 temp2 on
d = d +1
if d >= 10 then goto main
goto Place_Lettuce

##### Share on other sites

You could check for a pfpixel using pfread so you never plop on top of a pixel that is already there.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.