Need help

I made this code but for some reason the ball moves through the wall. Does anyone know how to fix it?

displayMode(FULLSCREEN)
function setup()
player=physics.body(CIRCLE,50)
player.type=DYNAMIC
player.x=WIDTH/2
player.y=HEIGHT/2

box=physics.body(POLYGON,vec2(0,10),vec2(0,0),vec2(500,0),vec2(500,10))
box.type=STATIC
box.x=200
box.y=20

end

function draw()

background(255, 255, 255, 255)

player.x = player.x + Gravity.x*20
player.y = player.y + Gravity.y*20

stroke(0, 0, 0, 255)
strokeWidth(2)
fill(0, 255, 245, 255)
ellipse(player.x,player.y,100,100)
rect(box.x,box.y,500,10)

end

When using physic bodies, dicrectly moving the position interferes with the collision detection, so you should use applyForce() to move the bodies insted.
But in your case you can use physics.gravity(x,y) to change the overall gravity.

@chunnoo, If I use physics.gravity(Gravity.x,Gravity.y) then the movement get really slow but It doesn’t move through the box. Is there a way to make the circle move normally?

Something like:

physics.gravity(Gavity.x * 10, Gravity.y * 10)

Or something similar should work i think.

@chunnoo, I tried it, I even Made it * 1000. This part is a little hard to explain, it moves with decent speed now but if you move the device it feels like the ball is heavy. It has a hard time turning.

@dave1707, i can’t move the circle now. know how to fix that?

You could try messing around with the balls mass and gravityScale and see if anything work for you.

I commented out the lines you don’t need for what you’re doing.


displayMode(FULLSCREEN) 
function setup()
    player=physics.body(CIRCLE,50) 
    --player.type=DYNAMIC 
    player.x=WIDTH/2 
    player.y=HEIGHT/2
    
    box=physics.body(POLYGON,vec2(0,10),vec2(0,0),vec2(500,0),vec2(500,10))
    box.type=STATIC
    box.x=200
    box.y=20
end

function draw()
    background(255, 255, 255, 255)    
    --player.x = player.x + Gravity.x --*20
    --player.y = player.y + Gravity.y --*20    
    stroke(0, 0, 0, 255)
    strokeWidth(2)
    fill(0, 255, 245, 255)
    ellipse(player.x,player.y,100,100)
    rect(box.x,box.y,500,10)
end

Sorry i was wrong you should do:

physics.gravity(Gravity)

And not (gravity.x, gravity.y)
Forgot that Gravity is 3D

@iampsimon Is this what you’re after.


supportedOrientations(PORTRAIT)
displayMode(FULLSCREEN)

function setup()
    player=physics.body(CIRCLE,50) 
    player.type=DYNAMIC 
    player.x=WIDTH/2 
    player.y=HEIGHT/2

    box=physics.body(POLYGON,vec2(0,10),vec2(0,0),vec2(500,0),vec2(500,10))
    box.type=STATIC
    box.x=200
    box.y=20
    box.sleepingAllowed=false
end

function draw()
    background(255, 255, 255, 255) 
    physics.gravity(Gravity.x*100,Gravity.y*100)   -- 100 might be too much
    stroke(0, 0, 0, 255)
    strokeWidth(2)
    fill(0, 255, 245, 255)
    ellipse(player.x,player.y,100,100)
    rect(box.x,box.y,500,10)
end

@dave1707, if you try the first code i posted, then you can se the ball moves very differently from the way you did it. I am trying to make it move like in the first code. Thanks for your help, I really appreciate it :slight_smile:

@chunno, it still move too slow, but thanks for your help btw. It’s really nice when people help
You if you have a problem :slight_smile:

@iampsimon If you’re using the physics objects, then you need to use the physics.gravity so it know when to stop when it hits another physics object. If you want to use Gravity.x and y like in the first code, then you’ll have to code for your own collisions. The line of code Player.y = Player.y + Gravity.y will force the Player through the box because nothing is stopping the value from Gravity.y .

@iampsimon Does this work better. Apparently what I said above isn’t 100% true.


supportedOrientations(PORTRAIT)

displayMode(FULLSCREEN) 
function setup() 
    player=physics.body(CIRCLE,50) 
    --player.type=DYNAMIC 
    player.x=WIDTH/2 
    player.y=HEIGHT/2
    player.gravityScale=0
    player.sleepingAllowed=false

    box=physics.body(POLYGON,vec2(0,10),vec2(0,0),vec2(500,0),vec2(500,10))
    box.type=STATIC
    box.x=200
    box.y=20
end

function draw()
    background(255, 255, 255, 255)
    player.x = player.x + Gravity.x*20
    player.y = player.y + Gravity.y*20  
    stroke(0, 0, 0, 255)
    strokeWidth(2)
    fill(0, 255, 245, 255)
    ellipse(player.x,player.y,100)
    rect(box.x,box.y-7,500,10)
end

displayMode(FULLSCREEN)

function setup()
    player=physics.body(CIRCLE,50)
    player.x=WIDTH/2
    player.y=HEIGHT/2
    
    box=physics.body(POLYGON,vec2(0,0),vec2(0,10),vec2(500,10),vec2(500,0))
    box.type=STATIC
    box.x=200
    box.y=20
    
    physics.gravity(Gravity*10)
    
    stroke(0, 0, 0, 255)
    strokeWidth(2)
    fill(0, 255, 245, 255)
    ellipseMode(RADIUS)
    rectMode(CORNERS)
end

function draw()
    background(255, 255, 255, 255)
    
    ellipse(player.x,player.y,player.radius)
    rect(box.x,box.y,box.x+box.points[3].x,box.y+box.points[3].y)
end

Adjust physics.gravity(Gravity*10) to your liking. No need to define player as dynamic since dynamic is default of physics bodies. I changed a few drawing modes so that the objects can be drawn based off of what you define in the physics bodies.

You do need to use physics.gravity like @chunnoo posted since it prevents the physics bodies from going through one another. The way you had it, you would add Gravity20 even if Gravity5 would have caused a collision. This forces the object to move to the location of Gravity20 and then it recognizes the collision and stays there.

@dave1707, that was exactly how I wanted it to move. @Slashin8r, @chunno, thanks for the help everyone :smiley: