Page 1 of 1

Physics Engine

Posted: Wed May 13, 2015 12:19 pm
by Hyperion Hyperspace
Hi guys, so recently I have been having some problems with this function, where it does not seem to actually interact with other objects. I am pretty sure its the shape being made, but feel free to correct me if I am wrong.

Code: Select all

function CreateObject(X, Y, Width, Height, Image)
  Buffer.CurrentImage = love.graphics.newImage(Image)
  Buffer.CurrentBody =   love.physics.newBody(world, X, Y)
  Buffer.CurrentShape = love.physics.newRectangleShape(Y, X, Width, Height)
  Buffer.CurrentFixture = love.physics.newFixture(Buffer.CurrentBody, Buffer.CurrentShape)
  love.graphics.draw(objects.GrassBlock.image, X, Y, Buffer.CurrentBody:getAngle(), 1, 1, Width, Height)
end
If you want my entire file, here you go:

Code: Select all

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
  Buffer = {}
  
  objects.GrassBlock = {}
  objects.GrassBlock.image = love.graphics.newImage("Grass+Block.png")
  --objects.GrassBlock.body = love.physics.newBody(world, 0, 638)
  --objects.GrassBlock.shape = love.physics.newRectangleShape(16,16)
  --objects.GrassBlock.fixture = love.physics.newFixture(objects.GrassBlock.body, objects.GrassBlock.shape)

  --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 ball
  objects.ball = {}
  objects.ball.body = love.physics.newBody(world, 650/2, 650/2, "dynamic") --place the body in the center of the world and make it dynamic, so it can move around
  objects.ball.shape = love.physics.newCircleShape(20) --the ball's shape has a radius of 20
  objects.ball.fixture = love.physics.newFixture(objects.ball.body, objects.ball.shape, 1) -- Attach fixture to body and give it a density of 1.
  objects.ball.fixture:setRestitution(0.1) --let the ball bounce

  --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)

  objects.block3 = {}
  objects.block3.image = love.graphics.newImage("Terraria.png")
  objects.block3.body = love.physics.newBody(world, 500, 200, "dynamic")
  objects.block3.shape = love.physics.newRectangleShape(0, 0, objects.block3.image:getWidth(), objects.block3.image:getWidth())
  objects.block3.fixture = love.physics.newFixture(objects.block3.body, objects.block3.shape, 5)

  function CreateObject(X, Y, Width, Height, Image)
  Buffer.CurrentImage = love.graphics.newImage(Image)
  Buffer.CurrentBody =   love.physics.newBody(world, X, Y)
  Buffer.CurrentShape = love.physics.newRectangleShape(Y, X, Width, Height)
  Buffer.CurrentFixture = love.physics.newFixture(Buffer.CurrentBody, Buffer.CurrentShape)
  love.graphics.draw(objects.GrassBlock.image, X, Y, Buffer.CurrentBody:getAngle(), 1, 1, Width, Height)
  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


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.block3.body:applyForce(100, 0)
  elseif love.keyboard.isDown("left") then --press the left arrow key to push the ball to the left
    objects.block3.body:applyForce(-100, 0)
  elseif love.keyboard.isDown("up") then --press the up arrow key to set the ball in the air
    objects.block3.body:applyForce(0, -400)
  end
end

function love.draw()
  love.graphics.setColor(255,255,255)	
  love.graphics.draw(objects.block3.image, objects.block3.body:getX(), objects.block3.body:getY(), objects.block3.body:getAngle(), 1, 1, objects.block3.image:getWidth()/2, objects.block3.image:getHeight()/2)
  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(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()))

  love.graphics.setColor(255,255,255)	
  CreateObject(30, 400, 16, 16, "Grass+Block.png")

end
Thanks! :awesome: