Whenever I make two physics bodies and have the collide(c) function with an output print “collision” it spams the output with the statement and crashes the app. help!? thanks in advance!
Remove the print statement?..
no the problem is that it is detecting a collision even when the bodies are way apart from each other. Sorry i was unclear.
Yup, without the code it’s difficult to give any further help…but are you calling the collide function from draw? If so, there is no need to explicitly do this as collide will be called automatically when two bodies collide.
I’ve an example in my cook off code (though am sure that this isn’t the best way to do it, but it works!)
@compactbear Here’s some code. Compare this to yours
function setup()
c1 = physics.body(CIRCLE,100) -- create body for 1st circle
c1.type=STATIC
c1.x=WIDTH/2
c1.y=300
c2 = physics.body(CIRCLE,50) -- create body for 2nd circle
c2.x=WIDTH/2
c2.y=800
c2.restitution=1
end
function draw()
background(40,40,50)
stroke(255)
strokeWidth(1)
noFill()
ellipse(c1.x,c1.y,200,200) -- draw the 1st circle
ellipse(c2.x,c2.y,100,100) -- draw the 2nd circle
end
function collide(c)
if c.state==BEGAN then
print("\
collision")
print(c.id)
print("radius of body A ",c.bodyA.radius)
print("radius of body B ",c.bodyB.radius)
print("position of body A ",c.bodyA.x)
print("position of body A ",c.bodyA.y)
-- see physics.contact and physics.body in the reference manual
-- for other info that's available during a collision
end
end
ok. thanks! Ill check it out!
This just crashes. this is my code
-- Use this function to perform your initial setup
function setup()
print("Hello World!")
sx = WIDTH / 2
sy = HEIGHT / 2
x,y = 0,0
tx, ty = 0,0
c1 = physics.body(CIRCLE, 50)
c1.type = STATIC
c1.x = WIDTH / 2
c1.y = 100
end
-- This function gets called once every frame
function draw()
background(127, 127, 127, 255)
fill(0, 0, 0, 255)
player()
if move then
sx=sx+(tx - x)/25
sy=sy+(ty - y)/25
end
ellipse(c1.x, c1.y,50, 50)
end
function touched(t)
if t.x < WIDTH / 2 then
if t.state == BEGAN then
x = t.x
y = t.y
tx = x
ty = y
move = true
elseif t.state == MOVING then
tx = t.x
ty = t.y
move = true
elseif t.state == ENDED then
x,y = 0, 0
tx, ty = 0,0
move = false
end
end
end
function player()
playerc = physics.body(CIRCLE, 50)
playerc.x = sx
playerc.y = sy
playerc.info = "player"
sprite("Platformer Art:Guy Standing", playerc.x, playerc.y)
sprite("Platformer Art:Guy Standing",playerc.x,playerc.y)
end
function collide(c)
if c.state == BEGAN then
print "collide"
end
end
thanks…
Move this code from player() into setup().
playerc = physics.body(CIRCLE, 50)
playerc.x = sx
playerc.y = sy
playerc.info = "player"
Thanks again Dave
How do I get rid of gravity and still be able to move from touching?
playerc.gravityScale=0
thansk
If I have the player() code in setup how do I switch it so I can move my player?
@compactbear I changed a few things that I think you’re trying to do.
displayMode(FULLSCREEN)
function setup()
x,y = 0,0
tx, ty = 0,0
c1 = physics.body(CIRCLE, 50)
c1.type = STATIC
c1.x = WIDTH / 2
c1.y = 100
playerc = physics.body(CIRCLE, 50)
playerc.x = WIDTH/2
playerc.y = HEIGHT/2
playerc.info = "player"
playerc.sleepingAllowed=false
end
function draw()
background(127, 127, 127, 255)
fill(0, 0, 0, 255)
player()
if move then
playerc.x=playerc.x+(tx - x)/25
playerc.y=playerc.y+(ty - y)/25
playerc.linearVelocity=vec2(0,0)
end
ellipse(c1.x, c1.y,100)
end
function touched(t)
if t.x<WIDTH/2 then
if t.state == BEGAN then
x = t.x
y = t.y
tx = x
ty = y
move = true
elseif t.state == MOVING then
if move==false then
x=t.x
y=t.y
end
tx = t.x
ty = t.y
move = true
elseif t.state == ENDED then
x,y = 0, 0
tx, ty = 0,0
move = false
end
else
x,y = 0, 0
tx, ty = 0,0
move = false
end
end
function player()
sprite("Platformer Art:Guy Standing", playerc.x, playerc.y)
end
function collide(c)
if c.state == BEGAN then
print "collide"
end
end
I got it now nvm
Thanks!
@compactbear I modified the above code so the sprite stops moving under the control of your finger once you cross the middle of the screen. This is in response to your other post. The sprite is still controlled by gravity and starts to fall.