Skip to content

Commit

Permalink
Initial commit with basic box2d bodys and joints in place
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Del Core committed Jan 8, 2016
0 parents commit d87cfc5
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 0 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# LOVE2D

To get started go to the following:
https://love2d.org/wiki/Getting_Started

## Mac OS X
On Mac OS X, a folder or .love file can be dropped onto the love application bundle. On the Mac Terminal (command line), you can use love like this (assuming it's installed to the Applications directory):

open -n -a love "~/path/to/mygame"
However, the above method will not output printed text to the terminal window. To do that, you will need to execute the love binary inside the application bundle directly:

/Applications/love.app/Contents/MacOS/love ~/path/to/mygame
You can set up an alias in your Terminal session to call the binary when you use love by adding an alias to your ~/.bash_profile.

Open the file with

open -a TextEdit ~/.bash_profile
You may have to run

touch ~/.bash_profile
first if the file does not yet exist.

Then paste in the following code and save the file:

\# alias to love

alias love="/Applications/love.app/Contents/MacOS/love"
Now you can call love from the command line like Linux and Windows:

love "~/path/to/mygame"
Android
As long as you have the love app installed from the playstore or another source the following methods will work:

Method 1 Transfer your game folder to /sdcard/lovegame where main.lua is found at /sdcard/lovegame/main.lua. Then run the app.
Method 2 Transfer a .love of your game to the device and click on it. Most file explorers will run the .love using the love app. If it does not you can install ES File Explorer which will.
41 changes: 41 additions & 0 deletions conf.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
function love.conf(t)
t.identity = nil -- The name of the save directory (string)
t.version = "0.10.0" -- The LÖVE version this game was made for (string)
t.console = false -- Attach a console (boolean, Windows only)
t.accelerometerjoystick = true -- Enable the accelerometer on iOS and Android by exposing it as a Joystick (boolean)
t.gammacorrect = false -- Enable gamma-correct rendering, when supported by the system (boolean)

t.window.title = "Untitled" -- The window title (string)
t.window.icon = nil -- Filepath to an image to use as the window's icon (string)
t.window.width = 800 -- The window width (number)
t.window.height = 600 -- The window height (number)
t.window.borderless = false -- Remove all border visuals from the window (boolean)
t.window.resizable = false -- Let the window be user-resizable (boolean)
t.window.minwidth = 1 -- Minimum window width if the window is resizable (number)
t.window.minheight = 1 -- Minimum window height if the window is resizable (number)
t.window.fullscreen = false -- Enable fullscreen (boolean)
t.window.fullscreentype = "desktop" -- Choose between "desktop" fullscreen or "exclusive" fullscreen mode (string)
t.window.vsync = true -- Enable vertical sync (boolean)
t.window.msaa = 0 -- The number of samples to use with multi-sampled antialiasing (number)
t.window.display = 1 -- Index of the monitor to show the window in (number)
t.window.highdpi = false -- Enable high-dpi mode for the window on a Retina display (boolean)
t.window.x = nil -- The x-coordinate of the window's position in the specified display (number)
t.window.y = nil -- The y-coordinate of the window's position in the specified display (number)

t.modules.audio = true -- Enable the audio module (boolean)
t.modules.event = true -- Enable the event module (boolean)
t.modules.graphics = true -- Enable the graphics module (boolean)
t.modules.image = true -- Enable the image module (boolean)
t.modules.joystick = true -- Enable the joystick module (boolean)
t.modules.keyboard = true -- Enable the keyboard module (boolean)
t.modules.math = true -- Enable the math module (boolean)
t.modules.mouse = true -- Enable the mouse module (boolean)
t.modules.physics = true -- Enable the physics module (boolean)
t.modules.sound = true -- Enable the sound module (boolean)
t.modules.system = true -- Enable the system module (boolean)
t.modules.timer = true -- Enable the timer module (boolean), Disabling it will result 0 delta time in love.update
t.modules.touch = true -- Enable the touch module (boolean)
t.modules.video = true -- Enable the video module (boolean)
t.modules.window = true -- Enable the window module (boolean)
t.modules.thread = true -- Enable the thread module (boolean)
end
122 changes: 122 additions & 0 deletions main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
-- Load some default values for our rectangle.
function love.load()
love.physics.setMeter(64) --the height of a meter our worlds will be 64px
world = love.physics.newWorld(0, 9.81*64, true) --create a world for the bodies to exist in with horizontal gravity of 0 and vertical gravity of 9.81

objects = {} -- table to hold all our physical objects

--let's create the ground
objects.ground = {}
objects.ground.body = love.physics.newBody(world, 650/2, 650-50/2) --remember, the shape (the rectangle we create next) anchors to the body from its center, so we have to move it to (650/2, 650-50/2)
objects.ground.shape = love.physics.newRectangleShape(650, 50) --make a rectangle with a width of 650 and a height of 50
objects.ground.fixture = love.physics.newFixture(objects.ground.body, objects.ground.shape); --attach shape to body

--let's create a couple blocks to play around with
objects.block1 = {}
objects.block1.body = love.physics.newBody(world, 200, 550, "dynamic")
objects.block1.shape = love.physics.newRectangleShape(0, 0, 50, 100)
objects.block1.fixture = love.physics.newFixture(objects.block1.body, objects.block1.shape, 5) -- A higher density gives it more mass.

objects.block2 = {}
objects.block2.body = love.physics.newBody(world, 200, 400, "dynamic")
objects.block2.shape = love.physics.newRectangleShape(0, 0, 100, 50)
objects.block2.fixture = love.physics.newFixture(objects.block2.body, objects.block2.shape, 2)

-- Softbody
particleNumber = 16;
particleDistance = 50;

objects.core = createSphere(650/2, 650/2, 20)
objects.nodes = {};

for i=1, particleNumber do
local angle = (2 * math.pi) / particleNumber * i
local posX = (650/2) + particleDistance * math.cos(angle)
local posY = (650/2) + particleDistance * math.sin(angle)
local b = createSphere(posX,posY,2)
local j = love.physics.newDistanceJoint(objects.core.body, b.body, posX, posY, posX, posY, false);
j:setDampingRatio(0.5);
j:setFrequency(12*(30/particleDistance));

table.insert(objects.nodes, b)
end

-- connect nodes to eachother
for i=1, #objects.nodes do
if i == #objects.nodes then
local b1 = objects.nodes[i].body
local b2 = objects.nodes[1].body
local j2 = love.physics.newDistanceJoint( b1, b2,
b1:getX(), b1:getY(),
b2:getX(), b2:getY(), false )
elseif i > 0 then
local b1 = objects.nodes[i].body
local b2 = objects.nodes[i+1].body
local j2 = love.physics.newDistanceJoint( b1, b2,
b1:getX(), b1:getY(),
b2:getX(), b2:getY(), false )
end
end

--initial graphics setup
-- love.graphics.setBackgroundColor(104, 136, 248) --set the background color to a nice blue
love.window.setMode(650, 650) --set the window dimensions to 650 by 650
end

-- Increase the size of the rectangle every frame.
function love.update(dt)
world:update(dt) --this puts the world into motion

--here we are going to create some keyboard events
if love.keyboard.isDown("right") then --press the right arrow key to push the ball to the right
objects.core.body:applyForce(200, 0)
objects.core.body:applyTorque( 5000 )
elseif love.keyboard.isDown("left") then --press the left arrow key to push the ball to the left
objects.core.body:applyForce(-200, 0)
objects.core.body:applyTorque( -5000 )
end

if love.keyboard.isDown("up") then
objects.core.body:setPosition(650/2, 650/2)
objects.core.body:setLinearVelocity(0, 0) --we must set the velocity to zero to prevent a potentially large velocity generated by the change in position

for i,v in ipairs(objects.nodes) do
local angle = (2 * math.pi) / #objects.nodes * i
local posX = (650/2) + particleDistance * math.cos(angle)
local posY = (650/2) + particleDistance * math.sin(angle)
v.body:setX(posX)
v.body:setY(posY)
v.body:setLinearVelocity(0, 0)
end
end
end

-- Draw a coloured rectangle.
function love.draw()
love.graphics.setWireframe( true )
love.graphics.setColor(72, 160, 14) -- set the drawing color to green for the ground
love.graphics.polygon("fill", objects.ground.body:getWorldPoints(objects.ground.shape:getPoints())) -- draw a "filled in" polygon using the ground's coordinates

-- love.graphics.setColor(193, 47, 14) --set the drawing color to red for the ball
-- love.graphics.circle("fill", objects.ball.body:getX(), objects.ball.body:getY(), objects.ball.shape:getRadius())

love.graphics.setColor(193, 47, 14) --set the drawing color to red for the ball

-- Softbody
love.graphics.circle("fill", objects.core.body:getX(), objects.core.body:getY(), objects.core.shape:getRadius())
for i,b in ipairs(objects.nodes) do
love.graphics.circle("fill", b.body:getX(), b.body:getY(), b.shape:getRadius())
end

love.graphics.setColor(50, 50, 50) -- set the drawing color to grey for the blocks
love.graphics.polygon("fill", objects.block1.body:getWorldPoints(objects.block1.shape:getPoints()))
love.graphics.polygon("fill", objects.block2.body:getWorldPoints(objects.block2.shape:getPoints()))
end

function createSphere(pX, pY,r)
local sphere = {}
sphere.body = love.physics.newBody(world, pX, pY, "dynamic") --place the body in the center of the world and make it dynamic, so it can move around
sphere.shape = love.physics.newCircleShape(r) --the ball's shape has a radius of 20
sphere.fixture = love.physics.newFixture(sphere.body, sphere.shape, 1) -- Attach fixture to body and give it a density of 1.
return sphere
end

0 comments on commit d87cfc5

Please sign in to comment.