# Physics/Touch Issue tearing my emotions apart.

``````-- PhysicsProj

-- Use this function to perform your initial setup
function setup()
w1 = physics.body(EDGE, vec2(0,0), vec2(WIDTH, 0))
w2 = physics.body(EDGE, vec2(0,0), vec2(0, WIDTH))
w3 = physics.body(EDGE, vec2(WIDTH, 0), vec2(WIDTH, HEIGHT))
garbagey = 40
linedestroy = true
onlyonce = true
balls={}
balls= CreateBall()

end

function CreateBall()
local p = physics.body(CIRCLE,50)
p.restitution = .8
p.linearVelocity = vec2(math.random(100,400), math.random(100,400))
p.x = math.random(60,250)
p.y = math.random(400,600)
p.color = color(math.random(0,255), math.random(0,255), math.random(0,255),100)
return p
end

function touched(t)
if t.state == BEGAN and not tw then
if CurrentTouch.x < otherx1 and CurrentTouch.x > otherx2 then
print("Success!")
gingerx = CurrentTouch.x
gingery = CurrentTouch.y

else
if CurrentTouch.x > otherx1 or CurrentTouch.x < otherx2 then
print("Error!")
ellipse(balls.x, balls.y, 200)
end
end
end

end

function draw()
background(220)
-- This sets a dark background color

fill(255,255,0)
fill (balls.color)
otherx1 = balls.x + 45
otherx2 = balls.x - 45

if CurrentTouch.x > otherx1 or CurrentTouch.x < otherx2 then
print("Test.")
end
if CurrentTouch.x < otherx1 or CurrentTouch.x > otherx2 then
balls.x = CurrentTouch.x
balls.y = CurrentTouch.y
ellipse(balls.x, balls.y, 100)
end

strokeWidth(5)

end
``````

This is one of my feeble attempts to use a physics object. Literally, the ONLY thing I am trying to achieve, is that when you touch the ball, it moves where you drag it. If you don’t touch in the premise of it, it won’t move. That’s it. I am scared I am so bad at this.
godhelpusall

In serious terms, I dunno. I can’t get the physics object to not use gravity without doing balls.x/y = CurrentTouch.x/y in draw, but that breaks EVERYTHING! The only reason I use physics instead of a normal ol’ ball is so I can use collision/overlapping detection, which I intend to use later on. Help, please?

@Treshure I modified your code just to show you how to do the touch. I’m not sure what else you want to do. I set up 2 balls.

``````
-- PhysicsProj

function setup()
hold=0
w1 = physics.body(EDGE, vec2(0,0), vec2(WIDTH, 0))
w2 = physics.body(EDGE, vec2(0,0), vec2(0, WIDTH))
w3 = physics.body(EDGE, vec2(WIDTH, 0), vec2(WIDTH, HEIGHT))
garbagey = 40
linedestroy = true
onlyonce = true
balls={}
table.insert(balls,CreateBall())
table.insert(balls,CreateBall())
end

function CreateBall()
local p = physics.body(CIRCLE,50)
p.restitution = .8
p.linearVelocity = vec2(math.random(100,400), math.random(100,400))
p.x = math.random(60,250)
p.y = math.random(400,600)
return p
end

function touched(t)
if t.state == BEGAN or t.state==MOVING then
for a,b in pairs(balls) do
v1=vec2(t.x,t.y)
d1=v1:dist(vec2(b.x,b.y))
if d1<50 then
b.x=t.x
b.y=t.y
b.linearVelocity=vec2(0,0)
hold=a
end
end
end
if t.state==ENDED and hold>0 then
balls[hold].type=STATIC
hold=0
end
end

function draw()
background(220)
fill(255, 0, 0, 255)
for a,b in pairs(balls) do
ellipse(b.x,b.y,100)
end
end

``````

My main goal is to have them as physics objects but not move. How can I have it so that when I let it go, it stays in place, BUT if I touch away from it, it doesn’t move? Thanks.

@Treshure I modified the above code. Try that.

That works great! Thanks!
(I’ll have to study on how you exactly pulled that off… for statement and pairs, precisely.)