-
-
Save videlais/636e334582f4f851f71422d63a34d2c2 to your computer and use it in GitHub Desktop.
Updated main.lua with physics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function love.load() | |
-- A meter will be 64px | |
love.physics.setMeter(64) | |
-- Set the X gravity, Y gravity, and if objects can sleep | |
world = love.physics.newWorld( 0, 0, true ) | |
-- Create a table table | |
player = {} | |
-- Create a width (for later drawing) | |
player.width = 20 | |
-- Create a height (for later drawing) | |
player.height = 20 | |
-- Create a new physics body in the world and make it "dynamic" | |
-- The new coordinates will be where the 'body' is going forward. | |
-- Any time (like for drawing) the x,y is neededm use getX() and getY() | |
player.body = love.physics.newBody( world, 100, 100, "dynamic" ) | |
-- Make a rectangle shape with: | |
-- Starting at 100, 100 (player.body:getX() and player.body:getY() ) | |
-- a width of player.width | |
-- a height of player.height | |
player.shape = love.physics.newRectangleShape(player.body:getX(), player.body:getY(), player.width, player.height) | |
-- Attach the shape to the body | |
player.fixture = love.physics.newFixture(player.body, player.shape, 1); | |
end | |
function love.update(dt) | |
-- Feed the world the current delta time (dt) | |
world:update(dt) | |
if love.keyboard.isDown("up") then | |
player.body:applyForce(0, -40) | |
end | |
if love.keyboard.isDown("down") then | |
player.body:applyForce(0, 40) | |
end | |
if love.keyboard.isDown("left") then | |
player.body:applyForce(-40, 0) | |
end | |
if love.keyboard.isDown("right") then | |
player.body:applyForce(40, 0) | |
end | |
end | |
function love.draw() | |
-- Draw the rectangle using: | |
-- "fill" (fill in the shape) | |
-- Using the X of player.body | |
-- Using the Y of player.body | |
-- Using the player.width | |
-- Using the player.height | |
love.graphics.rectangle("fill", player.body:getX(), player.body:getY(), player.width, player.height ) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment