Newbie trying to understand love.physics

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Nike
Prole
Posts: 1
Joined: Wed Apr 11, 2012 5:11 am

Newbie trying to understand love.physics

Post by Nike »

Hey guys,
Im a newbie to lua and have zero experience with any language. I was wondering if you guys could assist me with love.physics... Anyhow, I downloaded a snippet example somewhere on the love site with a red ball and a green rectangle. I first ran the program and realized it was for an earlier version of Love, so I spent some time fixing it. Now I have fixed the program so it will run, but now I have an issue with the ball moving straight through the green rectangle. Im sure its some silly mistake but if somone could help me figure this out it would be much appreicated.

Thanks

Code: Select all

local lg = love.graphics
function love.load()
  world = love.physics.newWorld(800, 600, "sleep") --create a world for the bodies to exist in with width and height of 650
  world:setGravity(0, 700) --the x component of the gravity will be 0, and the y component of the gravity will be 700
  
  objects = {} -- table to hold all our physical objects
  
  --let's create the ground
  objects.ground = {}
  --we need to give the ground a mass of zero so that the ground wont move
  objects.ground.body = love.physics.newBody(world, 225, 400, "static") --remember, the body anchors from the center of the shape
  objects.ground.shape = love.physics.newRectangleShape( 0, 500, 800, 50) --anchor the shape to the body, and make it a width of 650 and a height of 50
  --let's create a ball
  objects.ball = {}
  objects.ball.body = love.physics.newBody(world, 400, 300, "dynamic") --place the body in the center of the world, with a mass of 15
  objects.ball.shape = love.physics.newCircleShape (20) --the ball's shape has no offset from it's body and has a radius of 20

  --initial graphics setup
  love.graphics.setBackgroundColor(104, 136, 248) --set the background color to a nice blue
  love.graphics.setMode(800,600, 0, 1, 0) --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.ball.body:applyForce(400, 0)
  elseif love.keyboard.isDown("left") then --press the left arrow key to push the ball to the left
    objects.ball.body:applyForce(-400, 0)
  elseif love.keyboard.isDown("up") then --press the up arrow key to set the ball in the air
    objects.ball.body:setY(800/2)
  end
end

function love.draw()
  love.graphics.setColor(72, 160, 14) -- set the drawing color to green for the ground
  love.graphics.polygon("fill", 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(), 20) -- we want 20 line segments to form the "circle"
end
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Newbie trying to understand love.physics

Post by kikito »

Don't start with love.physics. It's the most complex part in LÖVE. Get familiar with images, keyboard, mouse and Lua in general first.
When I write def I mean function.
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Newbie trying to understand love.physics

Post by veethree »

Yeah love.physics isn't a good starting point if you have no prior experience. Try following some of the tutorials perhaps?
monkyyy
Citizen
Posts: 52
Joined: Fri Mar 16, 2012 5:29 pm

Re: Newbie trying to understand love.physics

Post by monkyyy »

they updated the example, i would suggest just re-copy-pasting the new one from the wiki
it seems they added a "love.physics.newFixture" for some reason(it broke what im currently working on and i dont see much reason for it), and w/o it the ball and ground are shapeless ghosts, w/ the image being drawn actually just being hardcoded to fit the body,
this isnt much of a mistake on ur part u just got here as they changed everything

edit- "zero experience with any language" did not see that >__>, i would suggest reading the lua manual cover to cover before u try to much more, or else ur progress learning box2d will be slow and painful
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest