Permalink
Browse files

added pong paddle-ball collision code, not working yet

  • Loading branch information...
mmmmhack committed Nov 5, 2011
1 parent 4c6a783 commit 60fca0411016fbf17700e14b25b0ea3c34270dfe
Showing with 279 additions and 113 deletions.
  1. +0 −79 demo/paddle.lua
  2. +210 −34 demo/pong.lua
  3. +56 −0 demo/rebound.lua
  4. +13 −0 devlog-wknight.html
  5. BIN exp/pong_collisions.png
View
@@ -1,79 +0,0 @@
--- paddle.lua : tests pong paddle drawing and movement
-require('gamelib')
-
-local fps = 60
-local frm_time = 1.0 / fps
-
-local paddle_accel = 20
-local paddel_vel = 0
-local rect = {
- x = 600,
- y = 400,
- w = 40,
- h = 100,
-}
-
-function draw_paddle()
- gamelib.draw_rect(rect.x, rect.y, rect.w, rect.h)
-end
-
-function do_input()
- paddel_vel = 0
- -- down key
- local dn = glfw.getKey(glfw.GLFW_KEY_DOWN) == glfw.GLFW_PRESS
- if dn then
- paddel_vel = -paddle_accel
- end
- -- up key
- local up = glfw.getKey(glfw.GLFW_KEY_UP) == glfw.GLFW_PRESS
- if up then
- paddel_vel = paddle_accel
- end
-end
-
-function move_paddle()
- rect.y = rect.y + paddel_vel
- -- clamp to boundary
- if rect.y < 0 then
- rect.y = 0
- end
- if rect.y + rect.h > gamelib.win_height() then
- rect.y = gamelib.win_height() - rect.h
- end
-end
-
-function main()
- gamelib.open_window()
-
- -- game main loop
- local done=false
- while not done do
- local beg_time = sys.double_time()
-
- -- draw paddle
- draw_paddle()
-
- -- handle input
- do_input()
-
- -- update paddle pos
- move_paddle()
-
- -- check for quit
- gamelib.update()
- local quit_key = glfw.getKey(glfw.GLFW_KEY_ESC) == glfw.GLFW_PRESS
- if gamelib.window_closed() or quit_key then
- done = true
- end
-
- -- sleep
- local end_time = sys.double_time()
- local wrk_time = end_time - beg_time
- local slp_time = frm_time - wrk_time
- if slp_time > 0 then
- sys.usleep(slp_time * 1000000)
- end
- end
-end
-
-main()
View
@@ -1,56 +1,232 @@
--- pong.lua : demo game for gamelib
---package.path=package.path .. ";../debugger/?.lua"
---require('strict')
---require('debugger')
-package.path=package.path .. ";../gamelib/?.lua"
+-- pong.lua : demo pong game
require('gamelib')
---require('sys')
-local r = { x=50, y=50, w=50, h=50 }
-local xdir = 1
-local ydir = 1
+local fps = 60
+local frm_time = 1.0 / fps
---local win_w = 800 -- TODO: replace with gamelib.win_width()
---local win_h = 600 -- TODO: replace with gamelib.win_height()
+local paddle = {
+ accel = 20,
+ vel = 0,
+ pos = {
+ x = 600,
+ y = 400,
+ },
+ w = 40,
+ h = 100,
+ color = {1.0, 1.0, 1.0}
+}
+
+local ball = {
+ pos = {
+ x=50,
+ y=50,
+ },
+ w=30,
+ h=30,
+ xdir = 1,
+ ydir = 1,
+ speed = 10,
+ color = {1.0, 1.0, 0}
+}
+
+function draw_paddle()
+ gl.color3f(paddle.color[1], paddle.color[2], paddle.color[3])
+ gamelib.draw_rect(paddle.pos.x, paddle.pos.y, paddle.w, paddle.h)
+end
+
+function draw_ball()
+ gl.color3f(ball.color[1], ball.color[2], ball.color[3])
+ gamelib.draw_rect(ball.pos.x, ball.pos.y, ball.w, ball.h)
+end
function draw()
- gl.color3f(0, 0.75, 0)
- gamelib.draw_rect(r.x, r.y, r.w, r.h)
+ draw_paddle()
+ draw_ball()
+end
+
+function do_input()
+ paddle.vel = 0
+ -- down key
+ local dn = glfw.getKey(glfw.GLFW_KEY_DOWN) == glfw.GLFW_PRESS
+ if dn then
+ paddle.vel = -paddle.accel
+ end
+ -- up key
+ local up = glfw.getKey(glfw.GLFW_KEY_UP) == glfw.GLFW_PRESS
+ if up then
+ paddle.vel = paddle.accel
+ end
+end
+
+function update_paddle()
+ paddle.pos.y = paddle.pos.y + paddle.vel
+ -- clamp to boundary
+ if paddle.pos.y < 0 then
+ paddle.pos.y = 0
+ end
+ if paddle.pos.y + paddle.h > gamelib.win_height() then
+ paddle.pos.y = gamelib.win_height() - paddle.h
+ end
+end
+
+function get_boundary_intersects(r1, r2)
+ local isc = {
+ [1] = {
+ [1] = 0,
+ [2] = 0,
+ },
+ [2] = {
+ [1] = 0,
+ [2] = 0,
+ },
+ }
+ local r1x1 = r1.pos.x
+ local r1x2 = r1.pos.x + r1.w
+ local r2x1 = r2.pos.x
+ local r2x2 = r2.pos.x + r2.w
+ if r1x1 < r2x1 then
+ isc[1][1] = -1
+ elseif r1x1 > r2x2 then
+ isc[1][1] = 1
+ end
+ if r1x2 < r2x1 then
+ isc[1][2] = -1
+ elseif r1x2 > r2x2 then
+ isc[1][2] = 1
+ end
+ local r1y1 = r1.pos.y
+ local r1y2 = r1.pos.y + r1.h
+ local r2y1 = r2.pos.y
+ local r2y2 = r2.pos.y + r2.h
+ if r1y1 < r2y1 then
+ isc[2][1] = -1
+ elseif r1y1 > r2y2 then
+ isc[2][1] = 1
+ end
+ if r1y2 < r2y1 then
+ isc[2][2] = -1
+ elseif r1y2 > r2y2 then
+ isc[2][2] = 1
+ end
+ return isc
+end
+
+function get_edge_intersects(r1, r2)
+ local intersects = {
+ [1] = {
+ [1] = 0,
+ [2] = 0,
+ },
+ [2] = {
+ [1] = 0,
+ [2] = 0,
+ },
+ }
+ local bi = get_boundary_intersects(r1, r2)
+ for dim = 1, 2 do
+ local other_dim = dim == 1 and 2 or 1
+ for edge = 1, 2 do
+ -- first condition: edge 'boundary' must intersect other rect edge boundaries
+ if bi[dim][edge] == 1 then
+ -- second condition: other-dim boundaries:
+ -- (lower boundary intersect OR upper boundary intersect) OR
+ if (bi[other_dim][1] == 1 or bi[other_dim][2] == 1) or
+ -- (lower boundary below AND upper boundary above)
+ (bi[other_dim][1] == -1 and bi[other_dim][2] == 1)
+ then
+ intersects[dim][edge] = 1
+ end
+ end
+ end
+ end
+ return intersects
+end
+
+function get_edge_intersect_counts(edge_intersects)
+ local counts = {
+ x = 0,
+ y = 0,
+ }
+ for dim = 1, 2 do
+ for edge = 1, 2 do
+ local isc = edge_intersects[dim][edge]
+ if isc == 1 then
+ if dim == 1 then
+ counts.x = counts.x + 1
+ else
+ counts.y = counts.y + 1
+ end
+ end
+ end
+ end
+ return counts
end
-function update()
+function update_ball()
local win_w = gamelib.win_width()
local win_h = gamelib.win_height()
- r.x = r.x + xdir
- r.y = r.y + ydir
+ ball.pos.x = ball.pos.x + ball.xdir * ball.speed
+ ball.pos.y = ball.pos.y + ball.ydir * ball.speed
- if (r.x + r.w >= win_w) or (r.x < 0) then
- xdir = -xdir
+ -- wall collision
+ if (ball.pos.x + ball.w >= win_w) or (ball.pos.x < 0) then
+ ball.xdir = -ball.xdir
end
- if (r.y + r.h >= win_h) or (r.y < 0) then
- ydir = -ydir
+ if (ball.pos.y + ball.h >= win_h) or (ball.pos.y < 0) then
+ ball.ydir = -ball.ydir
end
-end
-function quit_key_pressed()
- local state = glfw.getKey(glfw.GLFW_KEY_ESC)
- return state == glfw.GLFW_PRESS
+ -- paddle collision
+ local bei = get_edge_intersects(ball, paddle)
+ local ic = get_edge_intersect_counts(bei)
+
+ if ic.x == 1 then
+ ball.xdir = -ball.xdir
+ if ic.y == 1 then
+ ball.ydir = -ball.ydir
+ end
+ elseif ic.y == 1 then
+ ball.ydir = -ball.ydir
+ end
+
end
function main()
- gamelib.open_window()
+ gamelib.open_window()
- local run = true
- while run do
- draw()
- update()
+ -- game main loop
+ local done=false
+ while not done do
+ local beg_time = sys.double_time()
- gamelib.update()
- if gamelib.window_closed() or quit_key_pressed() then
- run = false
- end
- end
+ -- draw paddle
+ draw()
+
+ -- handle input
+ do_input()
+
+ -- update paddle pos
+ update_paddle()
+
+ -- update ball
+ update_ball()
+
+ -- check for quit
+ gamelib.update()
+ local quit_key = glfw.getKey(glfw.GLFW_KEY_ESC) == glfw.GLFW_PRESS
+ if gamelib.window_closed() or quit_key then
+ done = true
+ end
+
+ -- sleep
+ local end_time = sys.double_time()
+ local wrk_time = end_time - beg_time
+ local slp_time = frm_time - wrk_time
+ if slp_time > 0 then
+ sys.usleep(slp_time * 1000000)
+ end
+ end
end
main()
Oops, something went wrong.

0 comments on commit 60fca04

Please sign in to comment.