Fix collitions
This commit is contained in:
parent
a9f606dac5
commit
ff7683b726
Binary file not shown.
Before Width: | Height: | Size: 9.2 KiB After Width: | Height: | Size: 13 KiB |
Binary file not shown.
Before Width: | Height: | Size: 13 KiB |
Binary file not shown.
Before Width: | Height: | Size: 8.7 KiB After Width: | Height: | Size: 7.6 KiB |
50
main.lua
50
main.lua
@ -22,28 +22,31 @@ function love.load()
|
|||||||
love.physics.setMeter(64) -- Height earth in meters
|
love.physics.setMeter(64) -- Height earth in meters
|
||||||
world = love.physics.newWorld(0, gravity * 64, true) -- Make earth
|
world = love.physics.newWorld(0, gravity * 64, true) -- Make earth
|
||||||
-- Ship
|
-- Ship
|
||||||
ship = { x = canvas.width / 2, y = 0 , power = 100 }
|
ship = { x = canvas.width / 2, y = 0 , power = 100 , size_collition = 28, polygons_collition = 8 }
|
||||||
ship.img = love.graphics.newImage('assets/img/ship.png')
|
ship.img = love.graphics.newImage('assets/img/ship.png')
|
||||||
ship.body = love.physics.newBody(world, (canvas.width / 2) - (ship.img:getWidth() / 2) , ship.y, "dynamic")
|
ship.body = love.physics.newBody(world, (canvas.width / 2) - (ship.img:getWidth() / 2) , ship.y, "dynamic")
|
||||||
ship.shape = love.physics.newCircleShape(20)
|
ship.shape = love.physics.newCircleShape(20)
|
||||||
ship.fixture = love.physics.newFixture(ship.body, ship.shape, 1)
|
ship.fixture = love.physics.newFixture(ship.body, ship.shape, 1)
|
||||||
ship.fixture:setRestitution(0.9)
|
ship.fixture:setRestitution(0.9)
|
||||||
-- Generates initial asteroids
|
-- Generates initial asteroids
|
||||||
num_asteroids = 50
|
asteroids_collision = 40
|
||||||
|
asteroids_collision_polygon = 8
|
||||||
|
num_asteroids = 5
|
||||||
max_speed_asteroids = 5
|
max_speed_asteroids = 5
|
||||||
img_asteroide = {
|
img_asteroide = {
|
||||||
love.graphics.newImage('assets/img/asteroid1.png'),
|
love.graphics.newImage('assets/img/asteroid1.png'),
|
||||||
love.graphics.newImage('assets/img/asteroid2.png'),
|
love.graphics.newImage('assets/img/asteroid2.png')
|
||||||
love.graphics.newImage('assets/img/asteroid3.png')
|
|
||||||
}
|
}
|
||||||
asteroids = {}
|
asteroids = {}
|
||||||
for i=1, num_asteroids do
|
for i=1, num_asteroids do
|
||||||
temp_img = img_asteroide[math.random(1, table_length(img_asteroide))]
|
local temp_img = img_asteroide[math.random(1, table_length(img_asteroide))]
|
||||||
asteroids[i] = { x = math.random(0, canvas.width - temp_img:getWidth()),
|
asteroids[i] = {
|
||||||
|
x = math.random(0, canvas.width - temp_img:getWidth()),
|
||||||
y = math.random(200, canvas.height - temp_img:getHeight()),
|
y = math.random(200, canvas.height - temp_img:getHeight()),
|
||||||
speed = math.random(1, max_speed_asteroids),
|
speed = math.random(1, max_speed_asteroids),
|
||||||
img = temp_img,
|
img = temp_img,
|
||||||
angle = math.random(0, 90)}
|
angle = math.random(0, 90)
|
||||||
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -52,12 +55,10 @@ function love.update(dt)
|
|||||||
if play then
|
if play then
|
||||||
-- Phytics world
|
-- Phytics world
|
||||||
world:update(dt)
|
world:update(dt)
|
||||||
|
|
||||||
-- Controls
|
-- Controls
|
||||||
if love.keyboard.isDown('escape') or love.keyboard.isDown('q') then
|
if love.keyboard.isDown('escape') or love.keyboard.isDown('q') then
|
||||||
love.event.push('quit')
|
love.event.push('quit')
|
||||||
end
|
end
|
||||||
|
|
||||||
if love.keyboard.isDown("right") then
|
if love.keyboard.isDown("right") then
|
||||||
ship.body:applyForce(ship.power, 0)
|
ship.body:applyForce(ship.power, 0)
|
||||||
elseif love.keyboard.isDown("left") then
|
elseif love.keyboard.isDown("left") then
|
||||||
@ -71,11 +72,9 @@ function love.update(dt)
|
|||||||
|
|
||||||
-- Rotate asteroids
|
-- Rotate asteroids
|
||||||
for key, value in pairs(asteroids) do
|
for key, value in pairs(asteroids) do
|
||||||
-- Rota
|
|
||||||
value.angle = asteroids[key].angle + (dt * math.pi / 10)
|
value.angle = asteroids[key].angle + (dt * math.pi / 10)
|
||||||
value.x = asteroids[key].x - asteroids[key].speed
|
value.x = asteroids[key].x - asteroids[key].speed
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Destroy asteroids
|
-- Destroy asteroids
|
||||||
for key, value in pairs(asteroids) do
|
for key, value in pairs(asteroids) do
|
||||||
if value.x + asteroids[key].img:getWidth() < 0 then
|
if value.x + asteroids[key].img:getWidth() < 0 then
|
||||||
@ -85,7 +84,7 @@ function love.update(dt)
|
|||||||
|
|
||||||
-- Create asteroids
|
-- Create asteroids
|
||||||
if table_length(asteroids) < num_asteroids then
|
if table_length(asteroids) < num_asteroids then
|
||||||
temp_img = img_asteroide[math.random(1, table_length(img_asteroide))]
|
local temp_img = img_asteroide[math.random(1, table_length(img_asteroide))]
|
||||||
asteroids[table_length(asteroids) + 1] = {
|
asteroids[table_length(asteroids) + 1] = {
|
||||||
x = canvas.width + temp_img:getWidth(),
|
x = canvas.width + temp_img:getWidth(),
|
||||||
y = math.random(0, canvas.height - temp_img:getHeight()),
|
y = math.random(0, canvas.height - temp_img:getHeight()),
|
||||||
@ -123,7 +122,17 @@ function love.update(dt)
|
|||||||
end
|
end
|
||||||
|
|
||||||
for key, value in pairs(asteroids) do -- Asteroids
|
for key, value in pairs(asteroids) do -- Asteroids
|
||||||
if CheckCollision(asteroids[key].x - (asteroids[key].img:getWidth() / 2), asteroids[key].y - (asteroids[key].img:getHeight() / 2), asteroids[key].img:getWidth(), asteroids[key].img:getHeight(), ship.body:getX(), ship.body:getY(), ship.img:getWidth(), ship.img:getHeight()) then
|
local asteroid_temp = {
|
||||||
|
x = asteroids[key].x,
|
||||||
|
y = asteroids[key].y,
|
||||||
|
radius = asteroids_collision
|
||||||
|
}
|
||||||
|
local ship_temp = {
|
||||||
|
x = ship.body:getX() + (ship.img:getWidth() / 2),
|
||||||
|
y = ship.body:getY() + (ship.img:getHeight() / 2),
|
||||||
|
radius = ship.size_collition
|
||||||
|
}
|
||||||
|
if checkCircleCollision(asteroid_temp, ship_temp) then
|
||||||
play = false
|
play = false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -143,13 +152,13 @@ function love.draw()
|
|||||||
for key, value in pairs(asteroids) do
|
for key, value in pairs(asteroids) do
|
||||||
love.graphics.draw(value.img, value.x, value.y, value.angle, 1, 1, value.img:getWidth() / 2, value.img:getHeight() / 2)
|
love.graphics.draw(value.img, value.x, value.y, value.angle, 1, 1, value.img:getWidth() / 2, value.img:getHeight() / 2)
|
||||||
if debug then
|
if debug then
|
||||||
love.graphics.rectangle("line", value.x - (value.img:getWidth() / 2), value.y - (value.img:getHeight() / 2), value.img:getWidth(), value.img:getHeight())
|
love.graphics.circle("line", value.x, value.y, asteroids_collision, ship.polygons_collition)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- Ship
|
-- Ship
|
||||||
love.graphics.draw(ship.img, ship.body:getX(), ship.body:getY())
|
love.graphics.draw(ship.img, ship.body:getX(), ship.body:getY())
|
||||||
if debug then
|
if debug then
|
||||||
love.graphics.rectangle("line", ship.body:getX(), ship.body:getY(), ship.img:getWidth(), ship.img:getHeight())
|
love.graphics.circle("line", ship.body:getX() + (ship.img:getWidth() / 2), ship.body:getY() + (ship.img:getHeight() / 2), ship.size_collition, ship.polygons_collition)
|
||||||
end
|
end
|
||||||
-- Texts
|
-- Texts
|
||||||
if not play and not win then -- Game over
|
if not play and not win then -- Game over
|
||||||
@ -177,12 +186,7 @@ function sleep(n) -- seconds
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Collision detection function.
|
-- Collision detection function.
|
||||||
-- Returns true if two boxes overlap, false if they don't
|
function checkCircleCollision(circle1, circle2)
|
||||||
-- x1,y1 are the left-top coords of the first box, while w1,h1 are its width and height
|
local distance = math.sqrt((circle1.x - circle2.x) ^ 2 + (circle1.y - circle2.y) ^ 2)
|
||||||
-- x2,y2,w2 & h2 are the same, but for the second box
|
return distance <= circle1.radius + circle2.radius
|
||||||
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
|
|
||||||
return x1 < x2+w2 and
|
|
||||||
x2 < x1+w1 and
|
|
||||||
y1 < y2+h2 and
|
|
||||||
y2 < y1+h1
|
|
||||||
end
|
end
|
Loading…
Reference in New Issue
Block a user