Physics Bodies Not Colliding

The bodies just pass right through each other. Please help.



--# Main

function setup() 
    parameter.watch(PlayerY) 
    displayMode(FULLSCREEN_NO_BUTTONS)
    supportedOrientations(LANDSCAPE_ANY) 
    floor1 = physics.body(POLYGON,vec2(-501,HEIGHT/2 - 200),vec2(1000,HEIGHT/2 - 200),vec2(-501,HEIGHT/2 - 200),vec2(1000,HEIGHT/2 - 200)) 
    floor2 = physics.body(POLYGON,vec2(490,HEIGHT/2 + 186),vec2(1000,HEIGHT/2 + 186),vec2(450,HEIGHT/2 + 215),vec2(1000,HEIGHT/2 + 215)) 

    Player = physics.body(POLYGON,vec2(WIDTH/2 + 10,HEIGHT/2 - 10),vec2(WIDTH/2 - 10,HEIGHT/2 - 10),vec2(WIDTH/2 + 10,HEIGHT/2 - 10),vec2(WIDTH/2 - 10,HEIGHT/2 + 10))
    
    -- moved the following line from draw
    
    
    physics.gravity(0,-300)     -- changed
    parameter.watch("PlayerX") 
    parameter.watch("PlayerY") 
     parameter.watch("Floor1Y")
    Scene = 1 
    scrollTitleScreen = 0 
    xVel = 0 
    yVel = 0
    Player.y = 500
    Player.x = 500

floor1.gravityScale=0
    floor2.gravityScale=0
floor1.x = 200
floor1.y = 200 
floor1.density=5000
end

function draw()
    --Draws the appropritate stagese
    --Scenes:
    --1 = Title Screen
    --2 = Gamemode Select
    --3 = Stage select
    --4 = Ingame
    scene = Scene
    PlayerX = Player.x
    PlayerY = floor1.y
    
    textMode(CENTER)
   
    
    -- moved this line to setup
    --Player.x = WIDTH/2 Player.y = HEIGHT/2 floor1.x = 500

    if Scene == 1 then 
        physics.pause()
        strokeWidth(5) 
        background(0, 255, 251, 255) 
        font("Copperplate-Bold") 
        fill(0, 0, 0, 255) 
        fontSize(87) 
        text("THE PROTO PROGRAM",WIDTH/2,HEIGHT -78) 
        rectMode(CENTER) 
        fill(0, 255, 251, 255) 
        stroke(0, 0, 0, 255) 
        rect(WIDTH/2,HEIGHT/2,250,126) 
        fill(0, 0, 0, 255) 
        fontSize(60) text("PLAY",WIDTH/2,HEIGHT/2)
    elseif Scene == 2 then
        physics.pause()
        strokeWidth(5)
        fontSize(84)
        stroke(0, 0, 0, 255)
        fill(51,51,51,255)
        background(0,255,251,255)
        rectMode(CENTER)    
        fill(0, 0, 0, 255)
        text("CHOOSE A GAMEMODE",WIDTH/2,HEIGHT -78)
        fill(0, 255, 251, 255)
        rect(WIDTH/2,HEIGHT/2,250,126) 
        fontSize(27)
        fill(0,0,0,250)
        text("DEATHMATCH",WIDTH/2,HEIGHT/2)
        rectMode(CORNER)
        fill(0, 255, 251, 255)
        rect(-3,-3,100,50)
        rectMode(CENTER)
        textMode(CORNER)
        fontSize(25)
        fill(0, 0, 0, 255)
        text("BACK",9,10)
   elseif Scene == 3 then
    physics.pause()
        background(0,255,251,255)
        strokeWidth(5)
        fontSize(84)
        stroke(0, 0, 0, 255)
        background(0,255,251,255)
        rectMode(CENTER)
        fill(0, 0, 0, 255)
        text("CHOOSE A STAGE",WIDTH/2,HEIGHT -78)
        fill(0, 255, 251, 255)
        rect(WIDTH/2,HEIGHT/2,250,126) 
        fontSize(49)
        fill(0,0,0,250)
        text("METRO",WIDTH/2,HEIGHT/2)
        rectMode(CORNER)
        fill(0, 255, 251, 255)
        rect(-3,-3,100,50)
        rectMode(CENTER)
        textMode(CORNER)
        fontSize(25)
        fill(0, 0, 0, 255)
        text("BACK",9,10)
        
   elseif Scene == 4 then
    physics.resume()
        rectMode(CENTER)
        stroke(0, 0, 0, 255)
        fill(127, 127, 127, 255)
        strokeWidth(5)
        fontSize(84)
        stroke(0, 0, 0, 255)
        background(35, 47, 47, 255)
        rectMode(CENTER)
        fill(0, 0, 0, 255)
         rect(floor1.x,floor1.y,1200,90)
        rect(floor2.x + 1080,floor2.y,1207,20)
        fontSize(49)
        fill(0,0,0,250)
        rectMode(CORNER)
        fill(0, 255, 251, 255)
        rect(-3,-3,200,100)
        rectMode(CENTER)
        textMode(CORNER)
        fontSize(55)
        fill(0, 0, 0, 255)
        text("LEFT",9,10) 
        rectMode(CORNER)
        fill(0, 255, 251, 255)
        stroke(0, 0, 0, 255)
        rect(197,-3,200,100)
        rectMode(CENTER)
        textMode(CORNER)
        fontSize(55)
        fill(0, 0, 0, 255)
        text("RIGHT",209,10) 
        rectMode(CENTER)
                rectMode(CORNER)
        fill(0, 255, 251, 255)
        stroke(0, 0, 0, 255)
        rect(95,97,200,100)
        rectMode(CENTER)
        textMode(CORNER)
        fontSize(55)
        fill(0, 0, 0, 255)
        text("RIGHT",209,10) 
        rectMode(CENTER)
        
        sprite("Documents:Cyborg",Player.x,Player.y,73,90)
if Scene == 5 then
    
    physics.pause()
    tint(0, 0, 0, 255)
         rectMode(CENTER)
        stroke(0, 0, 0, 255)
        fill(127, 127, 127, 255)
        strokeWidth(5)
        fontSize(84)
        stroke(0, 0, 0, 255)
        background(35, 47, 47, 255)
        rectMode(CENTER)
        fill(0, 0, 0, 255)
         rect(floor1.x,floor1.y,1200,90)
        fontSize(49)
        fill(0,0,0,250)
        rectMode(CORNER)
        fill(0, 255, 251, 255)
        rect(-3,-3,200,100)
        rectMode(CENTER)
        textMode(CORNER)
        fontSize(55)
        fill(0, 0, 0, 255)
        text("LEFT",9,10) 
        rectMode(CORNER)
        fill(0, 255, 251, 255)
        stroke(0, 0, 0, 255)
        rect(197,-3,200,100)
        rectMode(CENTER)
        textMode(CORNER)
        fontSize(55)
        fill(0, 0, 0, 255)
        text("RIGHT",209,10) 
        rectMode(CENTER)
                rectMode(CORNER)
        fill(0, 255, 251, 255)
        stroke(0, 0, 0, 255)
        rect(95,97,200,100)
        rectMode(CENTER)
        textMode(CORNER)
        fontSize(55)
        fill(0, 0, 0, 255)
        text("RIGHT",209,10) 
        rectMode(CENTER)
        
        sprite("Documents:Cyborg",Player.x,Player.y,20,1000)   
        rect(WIDTH/2,HEIGHT/2,500,400)
end
end
end


--Buttons 
--DeathMatch button 
function touched(touch) 
    if touch.state == ENDED and touch.x > WIDTH/2 - 125 and 
            touch.x < WIDTH /2 + 125 and touch.y < HEIGHT/2+63 and 
            touch.y > HEIGHT/2- 63 and Scene == 2 then
        Scene = 3
        sound(SOUND_BLIT, 16900)
        floor1.x = 500
        Player.y = 500
    end 
    --Play button 
    if touch.state == ENDED and touch.x > WIDTH/2 - 125 and 
            touch.x < WIDTH /2 + 125 and touch.y < HEIGHT/2+63 and 
            touch.y > HEIGHT/2- 63 and Scene == 1 then
        Scene = 2
        sound(SOUND_BLIT, 16900)
    end
    --Metro stage button 
    if touch.state == ENDED and touch.x > WIDTH/2 - 125 and 
        touch.x < WIDTH /2 + 125 and touch.y < HEIGHT/2+63 and 
            touch.y > HEIGHT/2- 63 and Scene == 3 then
        Scene = 4
        sound(SOUND_BLIT, 16900)
    end
    --Back button (title screen) 
    if touch.state == ENDED and touch.x > 0 and touch.x < 100 and 
        touch.y < 100 and touch.y > 0 and Scene ~= 1 and Scene ~= 4 then
        Scene = Scene -1
        sound(SOUND_BLIT, 16900)
    end  
    --Ingame buttons
    --Left button
    if Scene == 4 then
        if touch.x > 0 and touch.x < 200 and 
        touch.y < 100 and touch.y > 0 then
            if touch.state == BEGAN or touch.state == MOVING then
                xVel = 100
              yVel = 0
                updatePlayerVel()
            end
        elseif touch.state == ENDED then
            xVel = 0
            
            updatePlayerVel()
        end
        --Right button
        if touch.x > 200 and touch.x < 400 and 
        touch.y < 100 and touch.y > 0 then
            if touch.state == BEGAN or touch.state == MOVING then
                xVel = -100
                yVel = 0
                
                updatePlayerVel()
            end
        elseif touch.state == ENDED then
            xVel = 0
            updatePlayerVel()
        end
        --Jump button
       if Player:testOverlap(floor1) then
       if touch.x > 95 and touch.x < 295 and 
        touch.y < 200 and touch.y > 100 then
            if touch.state == BEGAN then
                yVel = 300
                
              
            updatePlayerVel()
        end
    end
end 
end
end
function updatePlayerVel() 
    floor1.linearVelocity = vec2(xVel,yVel) 
    floor2.linearVelocity = vec2(xVel,yVel) 
    Player.linearVelocity = vec2(0,yVel)
    end

Hi. Have you set already the physics.body type? (Dynamic, static, etc.) See: http://twolivesleft.com/Codea/Reference/Physics.html#DYNAMIC

Edit: yeah, sorry. Dynamic is default.

Yes

@EpicPotato Here are your vec2 values for your physics.body formatted so they’re more readable. The floor1 1st and 3rd, 2nd and 4th values overlap instead of forming a polygon. The Player 1st and 3rd values also overlap. I think that if polygon values overlap and don’t form a true polygon, then they don’t work right which might explain why the bodies pass through each other. You need to redo the vec2 values to form a true polygon going clockwise or counterclockwise.


    floor1 = physics.body(POLYGON,
        vec2(-501,HEIGHT/2 - 200),
        vec2(1000,HEIGHT/2 - 200),
        vec2(-501,HEIGHT/2 - 200),
        vec2(1000,HEIGHT/2 - 200)) 
   
    floor2 = physics.body(POLYGON,
        vec2(490,HEIGHT/2 + 186),
        vec2(1000,HEIGHT/2 + 186),
        vec2(450,HEIGHT/2 + 215),
        vec2(1000,HEIGHT/2 + 215)) 

    Player = physics.body(POLYGON,
        vec2(WIDTH/2 + 10,HEIGHT/2 - 10),
        vec2(WIDTH/2 - 10,HEIGHT/2 - 10),
        vec2(WIDTH/2 + 10,HEIGHT/2 - 10),
        vec2(WIDTH/2 - 10,HEIGHT/2 + 10))

Still doesn’t work.
EDIT: Now they collide but when the player touches the floor he goes zipping around the screen and creates chaos!

The floor looks wrong. Floor1 has 4 points, but points 1 and 2 are the same as points 3 and 4. They should all be different. Floor2 looks odd too.

Why don’t you just make the floor one physics object, an EDGE, then you just make it a line from left to right, and everything will bounce off it and not go through.

The player body is strange too. If you draw a line between the points in the order you have used, you get a strange shape. I think you need to swap points 3 and 4. Also, why is the first point in the middle of the screen? You put your object in the middle of the screen with x and y, not with the body points. For the body points, just centre your object on 0,0.

So when designing this physics object, I would use the following points (-10,-10) (10,0) (10,10) (-10,10) which goes around the rectangle anti clockwise, and the rectangle is centred on 0,0. Then you can set the x and y values for the body, to the middle of the screen.

Sorry, that’s outdated code.
I can’t make the floor an EDGE because its a scrolling game.

Here’s the new code:



function setup() 
    parameter.watch(PlayerY) 
    displayMode(FULLSCREEN_NO_BUTTONS)
    supportedOrientations(LANDSCAPE_ANY) 


    floor1 = physics.body(POLYGON,
        vec2(-501,HEIGHT/2 - 200),
        vec2(-501,HEIGHT/2 + 200),
        vec2(1000,HEIGHT/2 - 200),
        vec2(1000,HEIGHT/2 + 200)) 
 

    Player = physics.body(POLYGON,
        vec2(WIDTH/2 - 10,HEIGHT/2 - 10),
        vec2(WIDTH/2 - 10,HEIGHT/2 + 10),
        vec2(WIDTH/2 + 10,HEIGHT/2 + 10),
        vec2(WIDTH/2 + 10,HEIGHT/2 - 10))


    -- moved the following line from draw
    
    
    physics.gravity(0,-300)     -- changed
    parameter.watch("PlayerX") 
    parameter.watch("PlayerY") 
     parameter.watch("Floor1Y")
    Scene = 1 
    scrollTitleScreen = 0 
    xVel = 0 
    yVel = 0
    Player.y = HEIGHT/2 + 300
    Player.x = 500
floor1.gravityScale = 0
floor1.x = 200
floor1.y = 200 
floor1.type = DYNAMIC
Player.restitution = .3
Player.density = 200
Player.type = DYNAMIC
floor1.x = 500
floor1.y = 500 
end

function draw()
    --Draws the appropritate stagese
    --Scenes:
    --1 = Title Screen
    --2 = Gamemode Select
    --3 = Stage select
    --4 = Ingame
    scene = Scene
    PlayerX = Player.x
    PlayerY = Player.y
    
    textMode(CENTER)
   
    
    -- moved this line to setup
    --Player.x = WIDTH/2 Player.y = HEIGHT/2 floor1.x = 500

    if Scene == 1 then 
        physics.pause()
        strokeWidth(5) 
        background(0, 255, 251, 255) 
        font("Copperplate-Bold") 
        fill(0, 0, 0, 255) 
        fontSize(87) 
        text("THE PROTO PROGRAM",WIDTH/2,HEIGHT -78) 
        rectMode(CENTER) 
        fill(0, 255, 251, 255) 
        stroke(0, 0, 0, 255) 
        rect(WIDTH/2,HEIGHT/2,250,126) 
        fill(0, 0, 0, 255) 
        fontSize(60) text("PLAY",WIDTH/2,HEIGHT/2)
    elseif Scene == 2 then
        physics.pause()
        strokeWidth(5)
        fontSize(84)
        stroke(0, 0, 0, 255)
        fill(51,51,51,255)
        background(0,255,251,255)
        rectMode(CENTER)    
        fill(0, 0, 0, 255)
        text("CHOOSE A GAMEMODE",WIDTH/2,HEIGHT -78)
        fill(0, 255, 251, 255)
        rect(WIDTH/2,HEIGHT/2,250,126) 
        fontSize(27)
        fill(0,0,0,250)
        text("DEATHMATCH",WIDTH/2,HEIGHT/2)
        rectMode(CORNER)
        fill(0, 255, 251, 255)
        rect(-3,-3,100,50)
        rectMode(CENTER)
        textMode(CORNER)
        fontSize(25)
        fill(0, 0, 0, 255)
        text("BACK",9,10)
   elseif Scene == 3 then
    physics.pause()
        background(0,255,251,255)
        strokeWidth(5)
        fontSize(84)
        stroke(0, 0, 0, 255)
        background(0,255,251,255)
        rectMode(CENTER)
        fill(0, 0, 0, 255)
        text("CHOOSE A STAGE",WIDTH/2,HEIGHT -78)
        fill(0, 255, 251, 255)
        rect(WIDTH/2,HEIGHT/2,250,126) 
        fontSize(49)
        fill(0,0,0,250)
        text("METRO",WIDTH/2,HEIGHT/2)
        rectMode(CORNER)
        fill(0, 255, 251, 255)
        rect(-3,-3,100,50)
        rectMode(CENTER)
        textMode(CORNER)
        fontSize(25)
        fill(0, 0, 0, 255)
        text("BACK",9,10)
        
   elseif Scene == 4 then
    physics.resume()
        rectMode(CENTER)
        stroke(0, 0, 0, 255)
        fill(127, 127, 127, 255)
        strokeWidth(5)
        fontSize(84)
        stroke(0, 0, 0, 255)
        background(35, 47, 47, 255)
        rectMode(CENTER)
        fill(0, 0, 0, 255)
         rect(floor1.x,floor1.y,1200,90)
        fontSize(49)
        fill(0,0,0,250)
        rectMode(CORNER)
        fill(0, 255, 251, 255)
        rect(-3,-3,200,100)
        rectMode(CENTER)
        textMode(CORNER)
        fontSize(55)
        fill(0, 0, 0, 255)
        text("LEFT",9,10) 
        rectMode(CORNER)
        fill(0, 255, 251, 255)
        stroke(0, 0, 0, 255)
        rect(197,-3,200,100)
        rectMode(CENTER)
        textMode(CORNER)
        fontSize(55)
        fill(0, 0, 0, 255)
        text("RIGHT",209,10) 
        rectMode(CENTER)
                rectMode(CORNER)
        fill(0, 255, 251, 255)
        stroke(0, 0, 0, 255)
        rect(95,97,200,100)
        rectMode(CENTER)
        textMode(CORNER)
        fontSize(55)
        fill(0, 0, 0, 255)
        text("RIGHT",209,10) 
        rectMode(CENTER)
        
        sprite("Documents:Cyborg",Player.x,Player.y,73,90)
if Scene == 5 then
    
    physics.pause()
    tint(0, 0, 0, 255)
         rectMode(CENTER)
        stroke(0, 0, 0, 255)
        fill(127, 127, 127, 255)
        strokeWidth(5)
        fontSize(84)
        stroke(0, 0, 0, 255)
        background(35, 47, 47, 255)
        rectMode(CENTER)
        fill(0, 0, 0, 255)
         rect(floor1.x,floor1.y,1200,90)
        fontSize(49)
        fill(0,0,0,250)
        rectMode(CORNER)
        fill(0, 255, 251, 255)
        rect(-3,-3,200,100)
        rectMode(CENTER)
        textMode(CORNER)
        fontSize(55)
        fill(0, 0, 0, 255)
        text("LEFT",9,10) 
        rectMode(CORNER)
        fill(0, 255, 251, 255)
        stroke(0, 0, 0, 255)
        rect(197,-3,200,100)
        rectMode(CENTER)
        textMode(CORNER)
        fontSize(55)
        fill(0, 0, 0, 255)
        text("RIGHT",209,10) 
        rectMode(CENTER)
                rectMode(CORNER)
        fill(0, 255, 251, 255)
        stroke(0, 0, 0, 255)
        rect(95,97,200,100)
        rectMode(CENTER)
        textMode(CORNER)
        fontSize(55)
        fill(0, 0, 0, 255)
        text("RIGHT",209,10) 
        rectMode(CENTER)
        
        sprite("Documents:Cyborg",Player.x,Player.y,20,1000)   
        rect(WIDTH/2,HEIGHT/2,500,400)
end
end
end


--Buttons 
--DeathMatch button 
function touched(touch) 
    if touch.state == ENDED and touch.x > WIDTH/2 - 125 and 
            touch.x < WIDTH /2 + 125 and touch.y < HEIGHT/2+63 and 
            touch.y > HEIGHT/2- 63 and Scene == 2 then
        Scene = 3
        sound(SOUND_BLIT, 16900)
        floor1.x = 500
        Player.y = 800
    end 
    --Play button 
    if touch.state == ENDED and touch.x > WIDTH/2 - 125 and 
            touch.x < WIDTH /2 + 125 and touch.y < HEIGHT/2+63 and 
            touch.y > HEIGHT/2- 63 and Scene == 1 then
        Scene = 2
        sound(SOUND_BLIT, 16900)
    end
    --Metro stage button 
    if touch.state == ENDED and touch.x > WIDTH/2 - 125 and 
        touch.x < WIDTH /2 + 125 and touch.y < HEIGHT/2+63 and 
            touch.y > HEIGHT/2- 63 and Scene == 3 then
        Scene = 4
        sound(SOUND_BLIT, 16900)
    end
    --Back button (title screen) 
    if touch.state == ENDED and touch.x > 0 and touch.x < 100 and 
        touch.y < 100 and touch.y > 0 and Scene ~= 1 and Scene ~= 4 then
        Scene = Scene -1
        sound(SOUND_BLIT, 16900)
    end  
    --Ingame buttons
    --Left button
    if Scene == 4 then
        if touch.x > 0 and touch.x < 200 and 
        touch.y < 100 and touch.y > 0 then
            if touch.state == BEGAN or touch.state == MOVING then
                xVel = 100
              yVel = 0
                updatePlayerVel()
            end
        elseif touch.state == ENDED then
            xVel = 0
            
            updatePlayerVel()
        end
        --Right button
        if touch.x > 200 and touch.x < 400 and 
        touch.y < 100 and touch.y > 0 then
            if touch.state == BEGAN or touch.state == MOVING then
                xVel = -100
                yVel = 0
                
                updatePlayerVel()
            end
        elseif touch.state == ENDED then
            xVel = 0
            updatePlayerVel()
        end
        --Jump button
       if Player:testOverlap(floor1) then
       if touch.x > 95 and touch.x < 295 and 
        touch.y < 200 and touch.y > 100 then
            if touch.state == BEGAN then
                yVel = 300
                
              
            updatePlayerVel()
        end
    end
end 
end
end
function updatePlayerVel() 
    floor1.linearVelocity = vec2(xVel,0) 
    Player.linearVelocity = vec2(0,yVel)
    end

Have you tried just to clamp the y position?

How do you do that?

Something like (please correct me, if I´m wrong, codea gurus):

Player.position.y = math.max(Player.position.y, HEIGHT/2 - 200)

Sorry if this is not what you are looking for.