# Physics Ball

Query: I have a physics ball to be hit by two paddles (comprised of other physics balls) (think air hockey), however, when the ball is hit, it moves, but does not slide like a normal ball. Thought so is probably because I am not very experienced in the field of physics coding, and am missing some necessary code. Would it be possible to add code to the ball to make it slide while having no additional code applied to the paddles? If not, what would be the best alternative way to make the ball slide?

``````-- variables for various functions and positions
-- ballpos = vector 2 starting position of the ball
-- pos1 = vector 2 constant position of Player 1's paddle
-- pos2 = vector 2 constant position of Player 2's paddle

function setup()
-- Ball
ball = physics.body(CIRCLE,25)
ball.type = DYNAMIC
ball.position = ballpos
ball.gravityScale = 0
ball.sleepingAllowed = false

Player1 = physics.body(CIRCLE,50)
Player1.type = KINEMATIC
Player1.position = pos1
Player1.gravityScale = 0
Player1.sleepingAllowed = false

Player2 = physics.body(CIRCLE,50)
Player2.type = KINEMATIC
Player2.position = pos2
Player2.gravityScale = 0
Player2.sleepingAllowed = false

end

function draw()

-- Ball
ballpos = ball.position
strokeWidth(0)
fill(255, 255, 255, 255)
ellipse(ball.position.x,ball.position.y,25)

-- Player 1
pos1.x = math.max(70,pos1.x)
pos1.x = math.min(WIDTH-70,pos1.x)
pos1.y = math.max(70,pos1.y)
pos1.y = math.min(HEIGHT/2-50,pos1.y)
Player1.position = pos1
strokeWidth(30)
stroke(255,0,0,255)
fill(0, 0, 0, 255)
ellipse(Player1.position.x, Player1.position.y, 50)

-- Player 2
pos2.x = math.max(70,pos2.x)
pos2.x = math.min(WIDTH-70,pos2.x)
pos2.y = math.max(HEIGHT/2+50,pos2.y)
pos2.y = math.min(HEIGHT-70,pos2.y)
Player2.position = pos2
strokeWidth(30)
stroke(0,255,255,255)
fill(0, 0, 0, 255)
ellipse(Player2.position.x, Player2.position.y, 50)

end
``````

@CayDay47 Here’s your code modified for what I think you’re after.

``````
displayMode(FULLSCREEN)

function setup()
et=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
eb=physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))

-- Ball
ball = physics.body(CIRCLE,25)
ball.position = vec2(WIDTH/2,HEIGHT/2)
ball.gravityScale=0
ball.sleepingAllowed = false
ball.restitution=.8
ball.linearVelocity=vec2(-300,300)

Player1 = physics.body(CIRCLE,50)
Player1.position = vec2(50,HEIGHT/2)
Player1.sleepingAllowed = false
Player1.type=KINEMATIC

Player2 = physics.body(CIRCLE,50)
Player2.position = vec2(WIDTH-50,HEIGHT/2)
Player2.sleepingAllowed = false
Player2.type=KINEMATIC
end

function draw()
background(0)
stroke(255)
strokeWidth(2)
line(0,0,WIDTH,0)
line(0,HEIGHT,WIDTH,HEIGHT)
line(WIDTH/2,0,WIDTH/2,HEIGHT)

noStroke()
-- Ball
fill(255, 255, 255, 255)
ellipse(ball.x,ball.y,50)

-- Player 1
fill(0, 10, 255, 255)
ellipse(Player1.x, Player1.y,100)

-- Player 2
fill(61, 255, 0, 255)
ellipse(Player2.x, Player2.y,100)
end

function touched(t)
if t.state==BEGAN then
if t.x<WIDTH/2 then
Player1.position=vec2(t.x,t.y)
id1=t.id
else
Player2.position=vec2(t.x,t.y)
id2=t.id
end
end
if t.state==MOVING then
if t.id==id1 then
Player1.linearVelocity=vec2(t.deltaX*65,t.deltaY*65)
end
if t.id==id2 then
Player2.linearVelocity=vec2(t.deltaX*65,t.deltaY*65)
end
end
if t.state==ENDED then
if t.id==id1 then
id1=0
Player1.linearVelocity=vec2(0,0)
end
if t.id==id2 then
id2=0
Player2.linearVelocity=vec2(0,0)
end
end
end

``````

Thanks for the basics in physics, however, is there a way to simulate the linear velocity with the variables from my original version, pos1 and pos2?
The reason being that I am not moving the paddles based on the exact area of my touch. Instead, there will be another way of control which will give me the vec2 of pos1 and pos2.

@CayDay47 To properly move a physics object, you need to use linearVelocity. You can still move a physics object without using linearVelocity, but while it’s being moved it’s ignoring some of its physics properties. It’s interaction with other physics objects won’t work the way it should. Since I don’t know exactly how you’re planning to use pos1 and pos2, I would just be guessing at how to change the code.

@dave1707 The way Pos1 and Pos2 work is that they recieve a vec2 from a controller style input(think joystick) before forawrding the vec2 forward to the physics balls. The controllers rely on a position and change of touch, meaning that the code you sent me may not have to move from the touched function.
Because I saw you used the deltaX and deltaY on touch, maybe a deltaX and deltaY could be facilitated for Pos1 and Pos2, maning that the majority of the same code could be used.

@CayDay47 Here’s what I think you’re asking for with pos1 and pos2. I also extende the top and bottom lines to go up and down the sides.

``````
displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
physics.continuous=true
pos1=vec2(0,0)
pos2=vec2(0,0)
et=physics.body(CHAIN,false,vec2(0,HEIGHT-250),vec2(0,HEIGHT),vec2(WIDTH,HEIGHT),vec2(WIDTH,HEIGHT-250))
eb=physics.body(CHAIN,false,vec2(0,250),vec2(0,0),vec2(WIDTH,0),vec2(WIDTH,250))

-- Ball
ball = physics.body(CIRCLE,25)
ball.position = vec2(WIDTH/2,HEIGHT/2)
ball.gravityScale=0
ball.sleepingAllowed = false
ball.restitution=.8
ball.bullet=true
ball.linearVelocity=vec2(math.random(300,500),math.random(300,500))

Player1 = physics.body(CIRCLE,50)
Player1.position = vec2(50,HEIGHT/2)
Player1.sleepingAllowed = false
Player1.type=KINEMATIC

Player2 = physics.body(CIRCLE,50)
Player2.position = vec2(WIDTH-50,HEIGHT/2)
Player2.sleepingAllowed = false
Player2.type=KINEMATIC
end

function draw()
background(0)
stroke(255)
strokeWidth(2)
line(WIDTH/2,0,WIDTH/2,HEIGHT)

line(0,HEIGHT-250,0,HEIGHT)
line(0,HEIGHT,WIDTH,HEIGHT)
line(WIDTH,HEIGHT,WIDTH,HEIGHT-250)

line(0,250,0,0)
line(0,0,WIDTH,0)
line(WIDTH,0,WIDTH,250)

noStroke()
-- Ball
fill(255, 255, 255, 255)
ellipse(ball.x,ball.y,50)

-- Player 1
fill(0, 10, 255, 255)
Player1.linearVelocity=pos1
ellipse(Player1.x, Player1.y,100)

-- Player 2
fill(61, 255, 0, 255)
Player2.linearVelocity=pos2
ellipse(Player2.x, Player2.y,100)
end

function touched(t)
if t.state==BEGAN then
if t.x<WIDTH/2 then
pos1=vec2(0,0)
id1=t.id
else
pos2=vec2(0,0)
id2=t.id
end
end
if t.state==MOVING then
if t.id==id1 then
pos1=vec2(t.deltaX*60,t.deltaY*60)
end
if t.id==id2 then
pos2=vec2(t.deltaX*60,t.deltaY*60)
end
end
if t.state==ENDED then
if t.id==id1 then
id1=0
pos1=vec2(0,0)
end
if t.id==id2 then
id2=0
pos2=vec2(0,0)
end
end
end

``````

Thats alright, I already have a border, the code I sent you was taken out of the full project and simplified.