#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Why doesn't the collision work?

edited December 2016 Posts: 87

@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 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)

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

• Posts: 7,604

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

• Posts: 87

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

• Posts: 7,604

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.

• edited December 2016 Posts: 87

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

• Posts: 7,604

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

• edited December 2016 Posts: 87

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!

• Posts: 7,604
``````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
``````
• edited December 2016 Posts: 87

@dave1707
What about using a table like this?

N=No block
1=Block

``````    local world={
'nnn1n1111',
'1111111111',
'n1n1n1n11',
'1nn111n11'
``````
• Posts: 7,604
``````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 do
if tab[x][y]==1 then
sprite("Platformer Art:Block Grass",y*70,(#tab+1-x)*70)
end
end
end
end
``````
• edited December 2016 Posts: 87

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 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)

end
function collide(contact)
print("collide")
end
``````
• Posts: 7,604

@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.

• edited December 2016 Posts: 87

@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 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)

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

Also fixed code above

• Posts: 7,604

@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

• edited December 2016 Posts: 87

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

• Posts: 7,604

@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.

• edited December 2016 Posts: 87

@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

• Posts: 7,604

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

• edited December 2016 Posts: 87

@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

• Posts: 7,604

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.

• Posts: 87

@dave1707
That was what I was trying to do the whole time but I don't understand why the ball still goes through the ground and how to fix it

• Posts: 7,604

Try this.

``````function setup()
p_rect = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
p_rect.x = 0
p_rect.y = 103
p_rect.type=STATIC
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)
for x=1,#tab do
for y=1,#tab 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)
end
``````
• edited December 2016 Posts: 7,604

@magicskillz I didn't like my above code, so I wrote this. It creates a physics object at each Sprite position making table changes easier.

EDIT: I also changed the way the character position is calculated.

``````supportedOrientations(LANDSCAPE_ANY)

function setup()
character = physics.body(CIRCLE,25)
character.x = WIDTH/2
character.y = HEIGHT
tab={     --  1=draw  0=no draw
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,0,0,0,0,1},
{1,0,0,0,0,0,1,1,1,1},
{1,0,0,1,1,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1},
}
spTab={}
for x=1,#tab do
for y=1,#tab do
if tab[x][y]==1 then
p1 = physics.body(POLYGON,vec2(0,0),vec2(70,0),vec2(70,70),vec2(0,70))
p1.x=y*70-35
p1.y=(#tab+1-x)*70-35
p1.type=STATIC
table.insert(spTab,p1)
p1=nil
end
end
end
end

function draw()
background(0, 145, 255, 255)
fill(255, 0, 0, 255)
character.linearVelocity=vec2(Gravity.x*1000,character.linearVelocity.y)
for x=1,#tab do
for y=1,#tab do
if tab[x][y]==1 then
sprite("Platformer Art:Block Grass",y*70,(#tab+1-x)*70)
end
end
end
end
``````
• Posts: 87

Sigh,
A new question:
When the camera moves the tiles don't work properly

Code:

``````--# Main
supportedOrientations(LANDSCAPE_ANY)

function setup()
canjump=nil
PlacesCantGo:init()
gamestate=1
cam = vec2(0,0)

parameter.watch("xpos")
parameter.watch("ypos")
parameter.watch("timer")

timer=0
starttimer=false

character = physics.body(CIRCLE,25)
character.x = 100
character.y = 300
tab={     --  1=grass  0=no draw 2=spike
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,2,0,0,0,0,0,0,2,0,1,2},--skip one row for spike
{0,0,0,0,1,0,0,0,0,0,0,1,1,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1},
}
spTab={}
for x=1,#tab do
for y=1,#tab do
if tab[x][y]==1 then
p1 = physics.body(POLYGON,vec2(0,0),vec2(70,0),vec2(70,70),vec2(0,70))
p1.x=y*70-35
p1.y=(#tab+1-x)*70-35
p1.type=STATIC
table.insert(spTab,p1)
p1=nil
end
end
end
end

function draw()

if gamestate==2
then
background(0, 145, 255, 255)
fill(255, 0, 0, 255)

character.linearVelocity=vec2(Gravity.x*1000,character.linearVelocity.y)

cam.x=cam.x-1
translate(cam.x,cam.y)
for x=1,#tab do
for y=1,#tab do
if tab[x][y]==1 then
sprite("Platformer Art:Block Grass",y*70,(#tab+1-x)*70)
end
end
end
for x=1,#tab do
for y=1,#tab do
if tab[x][y]==2 then
sprite("Platformer Art:Spikes",y*70,(#tab-x)*70)
end
end
end

ypos = character.y
ypos = string.format("%.0f", ypos)

xpos=character.x
xpos = string.format("%.0f", xpos)

if starttimer==true
then
character.y=character.y+5
timer=timer+1
end

for z=1,4 do
if xpos == PlacesCantGox[z]
then
if ypos == PlacesCantGoy[z]
then
restart()
end
end
end
if character.y < 0
then
restart()
end
end

if gamestate==1
then
sprite("Project:background",WIDTH/2,0,WIDTH,HEIGHT)
sprite("Project:background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)

--Text
fontSize(59)
fill(0, 214, 255, 255)
font("Didot-Bold")
text("Tap Screen to Start",WIDTH/2,HEIGHT/2)
-- Codea does not automatically call this method
end
function touched(t)
if t.state==BEGAN
then
gamestate=2
end
end

function touched(t)
if gamestate==2
then
if t.state==BEGAN and canjump==true and starttimer==false
then
starttimer=true
end
end
if gamestate==1
then
if t.state==BEGAN
then
gamestate=2
end
end
end
function collide( c )
if gamestate==2
then
if c.state==BEGAN
then
print("Collide")
canjump=true
starttimer=false
timer=0
end
end
end
end

--# PlacesCantGo
PlacesCantGo = class()

function PlacesCantGo:init(x)
-- you can accept and set parameters here
PlacesCantGox = {
"4",
"11",
"14"
}
PlacesCantGoy = {
"2",
"2",
"2"
}

end

function PlacesCantGo:draw()
-- Codea does not automatically call this method
end

function PlacesCantGo:touched(touch)
-- Codea does not automatically call this method
end
``````
• Posts: 7,604

@magicskillz Your indenting is so screwed up that you can't see what goes with what. You have 2 function touched() inside of the draw function. You only need one function touched(), and it shouldn't be within another function. You also have the function collide() within the draw function, again a no. You're trying to use a class, but you don't seem to know how to properly use it. You've been a member of the forum for a year, which means you should know basic programming by now. Every time I work on your code, I have to spend time reformatting it just to see what doesn't line up or what doesn't belong before I even execute you code.

• Posts: 87

Ok I'll work on reformatting my work! • edited December 2016 Posts: 7,604

@magicskillz Maybe this will help you. Press the up arrow to jump. Press the left or right arrows to scroll the blocks left or right. Tilt the iPad to move the character left or right. If the character hits the spikes, he dies. Look at this code to see how I did things so you can add whatever else you want. Any questions, just ask. Also, look at the indenting to see how easy it is to understand what belongs to what.

EDIT: Code removed. A better version is below.

• edited December 2016 Posts: 7,604

@magicskillz Here's a better version. The character always stays in the middle of the screen. Tilt the iPad to move the background left or right. Tap the screen to jump. Landing on the spikes kills the character.

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

function setup()
character = physics.body(CIRCLE,25)
character.x = WIDTH/2
character.y = 200
tab={
{1,0,4,0,0,0,0,4,0,0,4,0,0,0,0,0,0,0,0,4,0,0,4,0,0,1},
{1,0,4,0,0,0,0,4,0,0,4,0,0,0,0,0,0,0,0,4,0,0,4,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1},
{1,1,0,0,0,2,0,0,3,2,1,1,1,1,2,0,2,0,1,1,1,0,3,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},  -- bottom
}
spTab={}
for x=1,#tab do
for y=1,#tab do
if tab[x][y]>0 then
if tab[x][y]==1 then
p1 = physics.body(POLYGON,vec2(0,0),vec2(70,0),
vec2(70,70),vec2(0,70))
elseif tab[x][y]==2 then
p1 = physics.body(POLYGON,vec2(0,0),vec2(70,0),
vec2(70,10),vec2(0,10))
elseif tab[x][y]==3 then
p1 = physics.body(POLYGON,vec2(0,0),vec2(70,0),
vec2(70,50),vec2(0,50))
elseif tab[x][y]==4 then
p1 = physics.body(POLYGON,vec2(17,0),vec2(53,0),
vec2(53,50),vec2(17,50))
p1.loc=vec2(x,y)
end
p1.x=y*70-35
p1.y=(#tab+1-x)*70-35
p1.type=STATIC
p1.info=tab[x][y]
table.insert(spTab,p1)
p1=nil
end
end
end
dx=0
ch=character.x
coins=0
end

function draw()
background(0, 145, 255, 255)
fill(255, 0, 0, 255)
fontSize(50)
text("COINS  "..coins,WIDTH/2,HEIGHT-50)
pushMatrix()
translate(dx,0)
for x=1,#tab do
for y=1,#tab do
if tab[x][y]==1 then
sprite("Platformer Art:Block Grass",y*70,(#tab+1-x)*70)
elseif tab[x][y]==2 then
sprite("Platformer Art:Spikes",y*70,(#tab+1-x)*70)
elseif tab[x][y]==3 then
sprite("Platformer Art:Fence Broken",y*70,(#tab+1-x)*70)
elseif tab[x][y]==4 then
sprite("Platformer Art:Coin",y*70,(#tab+1-x)*70)
end
end
end
if character~=nil then
dx=dx-(character.x-ch)
ch=character.x
character.linearVelocity=vec2(Gravity.x*1000,character.linearVelocity.y)
sprite("Platformer Art:Guy Standing",character.x,character.y+10,50)
end
popMatrix()
if character==nil then
text("Character died",WIDTH/2,HEIGHT/2)
end
end

function touched(t)
if t.state==BEGAN then
if character~=nil then
if character.linearVelocity.y>-.001 and character.linearVelocity.y<1 then
character.linearVelocity=vec2(character.linearVelocity.x,250)
end
end
end
end

function collide(c)
if c.state==BEGAN then
if c.bodyA.info==2 or c.bodyB.info==2 then
character:destroy()
character=nil
dir=0
return
end
if c.bodyA.info==4 then
coins=coins+1
tab[c.bodyA.loc.x][c.bodyA.loc.y]=-1
c.bodyA:destroy()
end
if c.bodyB.info==4 then
coins=coins+1
tab[c.bodyB.loc.x][c.bodyB.loc.y]=-1
tab[x][y]=-1
c.bodyB:destroy()
end
end
end
``````
• edited December 2016 Posts: 7,604

Added code to the above program to show coins that can be collected for points.

• edited January 2017 Posts: 87

Thanks to the code @dave1707 provided I created this.
For some reason the background messed up when I took a photo Photo:

IMG_1069.JPG 295.6K