Those physics
Posted: Wed Aug 11, 2010 6:08 pm
Hello, i have been messing with this awesome system. Apparently for some reason i cant get physics work properly.
And i have video to show where is problem. (the red boxes are map collision boxes)
(Video may still be processing.)
Thank you.
Code: Select all
tile = {}
tile_s = 24
map=
{
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,8,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}
}
tile[0] = love.graphics.newImage("images/map/tileEmpty.png")
tile[1] = love.graphics.newImage("images/map/tileGrass1.png")
tile[2] = love.graphics.newImage("images/map/tileDirt1.png")
tile[3] = love.graphics.newImage("images/map/tileDirtEdge1.png")
tile[4] = love.graphics.newImage("images/map/tileGrassDirtEdge1.png")
tile[5] = love.graphics.newImage("images/map/tileGrassEdge1.png")
tile[6] = love.graphics.newImage("images/map/tileDirtEdge2.png")
tile[7] = love.graphics.newImage("images/map/tileGrassDirtEdge2.png")
tile[8] = love.graphics.newImage("images/map/tileGrassEdge2.png")
boxes = {}
boxes[0] = love.graphics.newImage("images/other/box.png")
boxes[1] = love.graphics.newImage("images/other/collisionbox.png")
font['menu'] = love.graphics.newFont(24)
font['small'] = love.graphics.newFont(10)
love.graphics.setFont(font['small'])
map_offset_x = 0
map_offset_y = 0
mapScrollSpeed = 500
world = love.physics.newWorld(2400, 600)
world:setGravity(0, 100)
world:setMeter(64)
bodies = {}
shapes = {}
mapbodies = {}
mapshapes = {}
n = 0
for y, yl in pairs(map) do
for x, xl in pairs(map[1]) do
if map[y][x] == 0 then
else
mapbodies[n] = love.physics.newBody(world, ((x - 1) * tile_s) + map_offset_x, ((y - 1) * tile_s) + map_offset_y, 0, 0)
mapshapes[n] = love.physics.newRectangleShape(mapbodies[n], 0, 0, 24, 24)
n = n + 1
end
end
end
for i = 0, 10 do
bodies[i] = love.physics.newBody(world, 650/2+i*30, 50, 15, 1) --place the body in the center of the world, with a mass of 15
shapes[i] = love.physics.newRectangleShape(bodies[i], 0, 0, 24, 24)
end
function love.draw()
for i = 0, 10 do
love.graphics.draw(boxes[0], bodies[i]:getX() + map_offset_x, bodies[i]:getY() + map_offset_y, bodies[i]:getAngle())
end
generateMap()
n = 0
for i, l in pairs(mapbodies) do
love.graphics.draw(
boxes[1],
mapbodies[i]:getX() + map_offset_x,
mapbodies[i]:getY() + map_offset_y,
mapbodies[i]:getAngle()
)
n = n + 1
end
end
function love.keypressed(key)
if key == "escape" then
love.filesystem.load("main.lua")()
love.load()
end
end
function love.update(dt)
world:update(dt)
if love.keyboard.isDown("left") then
map_offset_x = map_offset_x + mapScrollSpeed * dt
end
if love.keyboard.isDown("right") then
map_offset_x = map_offset_x - mapScrollSpeed * dt
end
if love.keyboard.isDown("d") then --press the right arrow key to push the ball to the right
bodies[1]:applyForce(400, 0)
elseif love.keyboard.isDown("a") then --press the left arrow key to push the ball to the left
bodies[1]:applyForce(-400, 0)
elseif love.keyboard.isDown("w") then --press the up arrow key to set the ball in the air
bodies[1]:applyForce(0, -400)
end
end
function generateMap()
for y, yl in pairs(map) do
for x, xl in pairs(map[1]) do
love.graphics.draw(tile[map[y][x]], ((x - 1) * tile_s)+ map_offset_x, ((y - 1) * tile_s) + map_offset_y)
end
end
end
(Video may still be processing.)
Thank you.