Hey, I’m having trouble with moving my physics objects stated in the setup function. There are tabs of code missing, but none of them are important in this case.
-- Neon Air Hockey
-- Main
supportedOrientations(PORTRAIT_ANY)
displayMode(OVERLAY)
rectMode(RADIUS)
ellipseMode(RADIUS)
function setup()
print("Pong")
-- Menu
menu = true
-- Button
button = Images:getButton()
buttonStart = Button("Start",WIDTH/2,HEIGHT/5*2,350,150,100,0,255,0,button,menuOff)
-- Ball
ball = physics.body(CIRCLE,25)
ball.type = DYNAMIC
ball.x = WIDTH/2
ball.y = HEIGHT/2
ball.gravityScale = 0
ball.sleepingAllowed = false
-- variables for Player 1 and 2 paddles
pos1 = vec2(WIDTH/2, HEIGHT/4)
pos2 = vec2(WIDTH/2, HEIGHT/4*3)
steer1 = vec2(0,0)
steer2 = vec2(0,0)
speed = 1600
-- Player 1's Controller
controller1 = VirtualStick {
moved = function(v) steer1 = v end,
released = function(v) steer1 = vec2(0,0) end,
x0=0,x1=1,y0=0,y1=0.25,name="Player 1",nameSide="right"}
-- Player 1's Paddle
Player1 = physics.body(CIRCLE,50)
Player1.type = KINEMATIC
Player1.x = pos1.x
Player1.y = pos1.y
Player1.gravityScale = 0
Player1.sleepingAllowed = false
-- Player 2's Controller
controller2 = VirtualStick {
moved = function(v) steer2 = v end,
released = function(v) steer2 = vec2(0,0) end,
x0=0,x1=1,y0=0.75,y1=1,name="Player 2",nameSide="left",orientation="back"}
-- Player 2's Paddle
Player2 = physics.body(CIRCLE,50)
Player2.type = KINEMATIC
Player2.x = pos2.x
Player2.y = pos2.y
Player2.gravityScale = 0
Player2.sleepingAllowed = false
allControllers = All({controller1,controller2})
end
function touched(touch)
allControllers:touched(touch)
end
function draw()
background(15, 15, 15, 255)
if menu == true then
drawMenu()
buttonStart:draw()
else
pushStyle()
strokeWidth(10)
stroke(0, 255, 0, 255)
lineCapMode(SQUARE)
line(0,HEIGHT/2,WIDTH,HEIGHT/2)
strokeWidth(20)
fill(0, 0, 0, 0)
rect(WIDTH/2,HEIGHT/2,WIDTH/2,HEIGHT/2)
popStyle()
-- Ball
ellipse(ball.x,ball.y,25)
-- Player 1
pos1 = pos1 + steer1*speed*DeltaTime
pos1.x = math.max(0,pos1.x)
pos1.x = math.min(WIDTH,pos1.x)
pos1.y = math.max(0,pos1.y)
pos1.y = math.min(HEIGHT/2,pos1.y)
fill(255,0,0,255)
ellipse(Player1.x, Player1.y, 50)
-- Player 2
pos2 = pos2 + steer2*speed*DeltaTime
pos2.x = math.max(0,pos2.x)
pos2.x = math.min(WIDTH,pos2.x)
pos2.y = math.max(HEIGHT/2,pos2.y)
pos2.y = math.min(HEIGHT,pos2.y)
fill(0,255,255,255)
ellipse(Player2.x, Player2.y, 50)
allControllers:draw()
end
end
There are 3 physics objects, and 2 of them would be moved by my virtual controller sticks. These controllers work fine on moving sprites by the 2D vectors ‘pos1’ and ‘pos2’. However, these don’t seem to apply to the kinematic circles named ‘Player1’ and ‘Player2’.
The physics lines corresponding to these physics objects are under ‘-- Player 1’s Paddle’ and ‘-- Player 2’s Paddle’.
Please help.