# Why doesn't the collision work?

@dave1707
I’m using physics but I can’t figure out why the collision doesn’t work

``````function setup()
p_rect = physics.body(POLYGON,vec2(-10,10),vec2(-10,0),vec2(10,-10),vec2(10,0))
p_rect.x = 0
p_rect.y = 7
p_rect.gravityScale=0
character = physics.body(CIRCLE,50)
character.x = WIDTH/2
character.y = 300
-- Start in the center of the screen
position = vec2( WIDTH/2, HEIGHT/2 )

-- Create two parameters to play with
parameter.number( "yCalibration", 0, 2.0, 0.6 )
parameter.number( "tiltStrength", 1, 40, 10 )
tab={     -- top      1=draw  0=no draw
{0,0,0,0,0,0,0,0,1,0},
{1,1,1,1,1,1,1,1,1,1},  -- bottom
}
end

function draw()
background(0, 145, 255, 255)
--fill(0, 145, 255, 255)
rect(p_rect.x,p_rect.y,WIDTH,100)
for x=1,#tab do
for y=1,#tab[1] do
if tab[x][y]==1 then
sprite("Platformer Art:Block Grass",y*70,(#tab+1-x)*70)
end
end
end

moveVec = vec2(Gravity.x, Gravity.y) + vec2( 0, yCalibration )

position = position + moveVec * tiltStrength
fill(255, 0, 0, 255)
ellipse(  position.x, character.y, character.radius*2)

end
function collide(contact)
print("collide")
end
``````

Not sure what you’re doing, but cam.x=cam.x-10 but xx=xx-1 .

I’m working on a 2d side scroller game
xx is for the sprite
Cam.x is for the camera

OK, but you’re moving xx at a speed of -1 and cam at a speed of -10. So cam is moving 10 times faster then xx.

@dave1707
Even once I change x to move at -10
The sprite still disappears off the screen

@magicskillz Do a forum search for camera scrolling. There are a lot of examples doing what you’re trying.

New Question:
Can someone explain go you can use a table to store all the ‘tiles’ for the game
Ps:I already saw @Ignatz’s 2d platform game on his blog!

``````supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)

function setup()
tab={}
for z=1,14 do
table.insert(tab,vec2(z*70,100))    -- 70 is the width of the sprite
end
end

function draw()
background(40, 40, 50)
for a,b in pairs(tab) do
sprite("Platformer Art:Block Grass",b.x,b.y)
end
end
``````

@dave1707
What about using a table like this?

N=No block
1=Block

``````    local world={
'nnn1n1111',
'1111111111',
'n1n1n1n11',
'1nn111n11'
``````
``````displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT_ANY)

function setup()
tab={   {0,0,1,1,0,0,1,1,1,0},  -- top      1=draw  0=no draw
{0,1,1,1,1,0,0,1,1,1},
{1,0,0,1,1,0,0,0,1,1},
{0,1,1,1,1,1,0,0,0,0},
{0,0,0,1,1,1,0,0,0,1},
{1,1,1,0,0,0,1,0,1,0},
{0,0,0,0,0,1,1,1,1,1},
{1,0,0,1,1,0,0,0,1,1},
{0,1,1,1,1,1,0,0,0,0},
{0,0,0,1,1,1,0,0,0,1},
{0,0,0,0,0,1,1,1,1,1},
{1,0,0,1,1,0,0,0,1,1},
{0,1,1,1,1,1,0,0,0,0},  -- bottom
}
end

function draw()
background(40, 40, 50)
for x=1,#tab do
for y=1,#tab[1] do
if tab[x][y]==1 then
sprite("Platformer Art:Block Grass",y*70,(#tab+1-x)*70)
end
end
end
end
``````

I’m using physics but I can’t figure out why the collsiom doesn’t work

Correct Code:

``````function setup()
p_rect = physics.body(POLYGON,vec2(-10,10),vec2(-10,0),vec2(10,-10),vec2(10,0))
p_rect.x = 0
p_rect.y = 7
p_rect.gravityScale=0
character = physics.body(CIRCLE,50)
character.x = WIDTH/2
character.y = 300
-- Start in the center of the screen
position = vec2( WIDTH/2, HEIGHT/2 )

-- Create two parameters to play with
parameter.number( "yCalibration", 0, 2.0, 0.6 )
parameter.number( "tiltStrength", 1, 40, 10 )
tab={     -- top      1=draw  0=no draw
{0,0,0,0,0,0,0,0,1,0},
{1,1,1,1,1,1,1,1,1,1},  -- bottom
}
end

function draw()
background(0, 145, 255, 255)
--fill(0, 145, 255, 255)
rect(p_rect.x,p_rect.y,WIDTH,100)
for x=1,#tab do
for y=1,#tab[1] do
if tab[x][y]==1 then
sprite("Platformer Art:Block Grass",y*70,(#tab+1-x)*70)
end
end
end

moveVec = vec2(Gravity.x, Gravity.y) + vec2( 0, yCalibration )

position = position + moveVec * tiltStrength
fill(255, 0, 0, 255)
ellipse(  position.x, character.y, character.radius*2)

end
function collide(contact)
print("collide")
end
``````

@magicskillz The collision does work. When the 2 circles collide, the word collision prints. Apparently you’re expecting something to happen that you’re not writing code for. Be more specific about what you think isn’t happening.

@dave1707
Feel so dumb I used the wrong project

This is the correct code

``````function setup()
p_rect = physics.body(POLYGON,vec2(-10,10),vec2(-10,0),vec2(10,-10),vec2(10,0))
p_rect.x = 0
p_rect.y = 7
p_rect.gravityScale=0
character = physics.body(CIRCLE,50)
character.x = WIDTH/2
character.y = 300
-- Start in the center of the screen
position = vec2( WIDTH/2, HEIGHT/2 )

-- Create two parameters to play with
parameter.number( "yCalibration", 0, 2.0, 0.6 )
parameter.number( "tiltStrength", 1, 40, 10 )
tab={     -- top      1=draw  0=no draw
{0,0,0,0,0,0,0,0,1,0},
{1,1,1,1,1,1,1,1,1,1},  -- bottom
}
end

function draw()
background(0, 145, 255, 255)
--fill(0, 145, 255, 255)
rect(p_rect.x,p_rect.y,WIDTH,100)
for x=1,#tab do
for y=1,#tab[1] do
if tab[x][y]==1 then
sprite("Platformer Art:Block Grass",y*70,(#tab+1-x)*70)
end
end
end

moveVec = vec2(Gravity.x, Gravity.y) + vec2( 0, yCalibration )

position = position + moveVec * tiltStrength
fill(255, 0, 0, 255)
ellipse(  position.x, character.y, character.radius*2)

end
function collide(contact)
print("collide")
end
``````

Also fixed code above

@magicskillz Maybe this link will be of interest to you. I wrote the code more than 2 years ago. https://codea.io/talk/discussion/5302/starter-game-4#latest

@dave1707
Thanks for the link but I still don’t see how I could use that for my program above

@magicskillz I thought it might be similar to what you were starting. Does the code you show above work the way you want or is there something wrong. I’m not sure from your comment if you fixed the code. When I run your code, the ball falls thru the blocks. Not sure if it isn’t supposed to or what your intent is.

@dave1707
I don’t want the ball to fall through the ground that’s why I’m asking this question and I didn’t fix my code
I also can’t mess around with the x and y positions of the ground without the code not working or looking weird

@magicskillz You’re going to have to create a physics object for each Sprite position you draw.

@dave1707
Why can’t I just create a large rectangle at the bottom like what I did before?

The reason you couldn’t see the rect when you play the code is because I gave it the same colour as the background

I guess you can do it that way if you’re not going to change the 0’s and 1’s in the table once it’s set up.