Help with collision

Hi,

I reinstalled codea and am trying for the 3rd time to learn it. Am working on a breakout game ATM but my collision detection is not working.

Am looking for a little pointer in the right direction, pls don’t rewrite the while code for me as that makes me feel bad.

Please try this and see why it’s failed, thank you.

-- Break

-- Use this function to perform your initial setup
displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
xpos=512
ballx=512
bally=450
end

function draw()
  -- define screen space and add collision detection
strokeWidth(5)
background(40,40,50)
line(0,0,0,768)  -- draw left wall
line(0,768,1024,768)  -- draw top wall
line(1024,0,1024,768) -- draw right wall
physics.body(EDGE,vec2(0,0),vec2(0,768))
physics.body(EDGE,vec2(0,768),vec2(1024,768))
physics.body(EDGE,vec2(1024,0),vec2(1024,768))


  -- add 'bat' sprite and setup touch movement
sprite("Planet Cute:Plain Block",xpos,100,150,55)
  -- set bat screen limits and collision
xpos=CurrentTouch.x
  if xpos<=77 then xpos=78
  end
  if xpos>=946 then xpos=945
  end
physics.body(POLYGON,vec2(xpos,100),vec2(xpos+54,154))

  -- add ball and move towards bat
sprite("Tyrian Remastered:Explosion Ball",ballx,bally)
physics.body(CIRCLE ,100)
bally=bally-2

end

Typo. ‘Whole’, not ‘while’.

@Paul123 I don’t really have time to look at your code, but for typos you can use the edit button :wink:

Didn’t see it up there. Sorry

It doesn’t work for two reasons:

  1. You are remaking the physics bodies 60 times per second. You should make them once in setup

  2. You are changing a variable Bally and xpos and etc. and not the physics body variables.

Ex:

If in setup you said: BALL = physics.body(CIRCLE, 100)

Then in draw you could say: sprite(ballImg, BALL.x, BALL.y)

(physics gravity will move it downwards for you)

Thanks.

I do need to keep the physics for the bat in the main loop as that changes position.
I’ll try fix it with what you said but I’m not sure i understand but I’ll try.

I have tutorials on physics, see chapter 9 of this ebook

https://www.dropbox.com/s/t5im6tl14ky5t08/Codea%20for%20beginners.pdf

Made slight changes but not the ball vanishes?

-- Break

-- Use this function to perform your initial setup
displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
xpos=512
ballx=512
bally=450
physics.body(EDGE,vec2(0,0),vec2(0,768))
physics.body(EDGE,vec2(0,768),vec2(1024,768))
physics.body(EDGE,vec2(1024,0),vec2(1024,768))
BALL = physics.body(CIRCLE, 100)
end

function draw()
  -- define screen space and add collision detection
strokeWidth(5)
background(40,40,50)
line(0,0,0,768)  -- draw left wall
line(0,768,1024,768)  -- draw top wall
line(1024,0,1024,768) -- draw right wall

  -- add 'bat' sprite and setup touch movement
sprite("Planet Cute:Plain Block",xpos,100,150,55)
  -- set bat screen limits and collision
xpos=CurrentTouch.x
  if xpos<=77 then xpos=78
  end
  if xpos>=946 then xpos=945
  end
physics.body(POLYGON,vec2(xpos,100),vec2(xpos+54,154))

  -- add ball and move towards bat
sprite("Tyrian Remastered:Explosion Ball",BALL.x,BALL.y)
end

@Paul123 you never assigned a possition to the BALL, so under your BALL = physics…
do something like

BALL.x, BALL.y = WIDTH/2, HEIGHT/2

tho it still falls through the paddle

Thanks. It does? Oh… Thought I’d assigned collision to the bat. Ok thanks

@Paul123 you define your bat as a polygon, which is also affected by gravity, so it actually ‘falls’ , tho you don’t see it cause you use other coordinates to draw your sprite, but keep in mind that if you assign your physics.body in the draw, it stays there, so if you move across the screen, you’ll have your whole screen filled with bats, just not visible…

@Paul123 You can set the gravityScale of the bat to 0 so it won’t be affected by gravity.

@Paul Move the physics.body for the bat out of draw(). Do the same thing for the bat as you’re do for the ball. Also, you don’t need a polygon for the bat. You can use edge since the ball is only going to bounce off the top area of the bat. The polygon you define for the bat is wrong anyways. There should be 4 vec2 values, one for each corner.

Stevon8ter - confusing, first I’m told to take the physics out of the draw loop and you tell me to put it back?

Thanks Dave, I’ll try more.

If you put the physics in draw then you are creating 60 bats per second. They will be deleted 60 seconds later because you haven’t set them to a variable, eg

Bat = physics.body…

You only want one bat, and you don’t want it to be deleted, so your code needs fixing.

I realise this is very confusing, but I explain all this in my ebook I referenced above. Please read it.

Thank you mr ignatz, I did download your book to my iBooks app and am reading it but I really don’t understand, sorry I’m not really a programmer. I will keep trying

Been reading ignatz guide and stealing some of it’s code (sorry)

It’s getting better now but still problems.

1: ball sometimes goes through right side wall
2: bat collision not working right, ball bounces off it but if you miss it still bounces.bat physics not right I think.

Here’s the code to look at

-- Break

-- Use this function to perform your initial setup
displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
xpos=512
ballx=512
bally=450
physics.body(EDGE,vec2(0,0),vec2(0,768))
physics.body(EDGE,vec2(0,768),vec2(1024,768))
physics.body(EDGE,vec2(1024,0),vec2(1024,768))
ball_diam=40
p_ball = physics.body(CIRCLE,ball_diam/2) 
p_ball.gravityScale = 1 
p_ball.restitution = .8 
p_ball.friction = 0 
p_ball.linearVelocity = vec2(math.random(400),math.random(400))  
p_ball.x = math.random(75,150) 
p_ball.y = math.random(450,650)
end

function draw()
  -- define screen space and add collision detection
strokeWidth(5)
background(40,40,50)
line(0,0,0,768)  -- draw left wall
line(0,768,1024,768)  -- draw top wall
line(1024,0,1024,768) -- draw right wall

  -- add 'bat' sprite and setup touch movement
sprite("Planet Cute:Plain Block",xpos,100,150,55)
  -- set bat screen limits and collision
xpos=CurrentTouch.x
  if xpos<=77 then xpos=78
  end
  if xpos>=948 then xpos=945
  end
physics.body(EDGE,vec2(xpos-70,109),vec2(xpos+71,109))
  -- add ball and move towards bat
sprite("Tyrian Remastered:Explosion Ball",p_ball.x,p_ball.y,40,40)
end

Try this. I’ve numbered some lines, see the notes for them underneath

-- Break

-- Use this function to perform your initial setup
--displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    xpos=512  --[0]
    ballx=512
    bally=450
    e1=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))   --[1]  --[2]
    e2=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
    e3=physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
    ball_diam=40
    p_ball = physics.body(CIRCLE,ball_diam/2) 
    p_ball.gravityScale = 1 
    p_ball.restitution = .8 
    p_ball.friction = 0 
    p_ball.linearVelocity = vec2(math.random(400),math.random(400))  
    p_ball.x = math.random(75,150) 
    p_ball.y = math.random(450,650)
    --add bat
    bat=physics.body(EDGE,vec2(-70,109),vec2(70,109))   --[3]
    bat.gravityScale = 1 
    bat.restitution = 1.2  --[4]
    bat.friction = 0 
end

function draw()
      -- define screen space and add collision detection
    strokeWidth(5)
    background(40,40,50)
    color(255)
    line(0,0,0,HEIGHT) -- draw left wall
    line(0,HEIGHT,WIDTH,HEIGHT) -- draw top wall
    line(WIDTH,0,WIDTH,HEIGHT) -- draw right wall
    
      -- add 'bat' sprite and setup touch movement
          -- set bat screen limits and collision
    xpos=CurrentTouch.x
    if xpos<=77 then xpos=78
    elseif xpos>=948 then xpos=945  --[5]
    end
    bat.x=xpos  --[6]
    sprite("Planet Cute:Plain Block",xpos,100,150,55)
      -- add ball and move towards bat
    sprite("Tyrian Remastered:Explosion Ball",p_ball.x,p_ball.y,40,40)
end

[0] --indent your code, it makes it harder to make mistakes
[1] --assign the edges to a variable, otherwise Codea will delete them after about a minute
[2] --never hard code widths, height etc unless you need to. Get the actual width and height
[3]–create the bat in setup, not draw
[4]–set the bat restitution high so it is springy
[5]–learn how to use elseif (Lua tutorials!!!)
[6]–this is how to update the physics body x position

Finally, none of us mind helping as long as you’ve taken the trouble to look at the help and tutorials we’ve provided. You’ll appreciate we get the same questions all the time, and it makes our lives much easier if you try to learn the basics from tutorials, before asking.

Sorry I don’t understand several things you mention.

Okay… We’ll I’m really sorry for asking stupid questions, please forgive me. No offence intended.

It’s ok, there aren’t any stupid questions, we all started by feeling ignorant and clumsy - but try reading everything you can, just to save us explaining the same things over and over - and if you’re still stuck, ask.