Physics - Reducing friction upon contact

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
cakexploder
Prole
Posts: 1
Joined: Sun Aug 22, 2010 3:51 am

Physics - Reducing friction upon contact

Post by cakexploder » Sun Aug 22, 2010 4:04 am

Hey everyone. Total Lua/Löve newbie here. I'm working on a physics-based 2D sidescroller (I know, I know) and I'm having some trouble with jumping. I managed to get applyForce under control to prevent the character from flying off in to space, but upon landing friction brings the character to a stop, regardless of any x/y velocity. I'm assuming I'm missing/doing something obvious/wrong, so any input would be incredibly helpful. I've added all the source code below, feel free to pick apart anything I'm doing wrong at all - I'm here to learn!

Code: Select all

function love.load()
	love.graphics.setMode(900, 400, false, true, 0)
	world = love.physics.newWorld(900, 400)
  	world:setGravity(0, 700)
 	world:setMeter(64)

 	bodies = {} 
 	shapes = {}
 
  	bodies[0] = love.physics.newBody(world, 900/2, 400, 0, 0)
  	shapes[0] = love.physics.newRectangleShape(bodies[0], 0, 0, 900, 50, 0) -- This is the ground
  
 	bodies[1] = love.physics.newBody(world, 100, 400/2, 10, 0) 
  	shapes[1] = love.physics.newCircleShape(bodies[1], 0, 0, 20) -- Invisible shape that the sprite follows

 	love.graphics.setBackgroundColor(104, 136, 248)
 	char = love.graphics.newImage("sprite-r.gif");
 	char_x = 100
	char_y = 100
	jumplimit = false -- Default the jump limit (has the sprite jumped to max height allowed) to "no"
end

function love.update(dt)
  world:update(dt) --this puts the world into motion
  
  vel_x, vel_y = bodies[1]:getLinearVelocity( )
  
  if love.keyboard.isDown("right") then
    bodies[1]:applyForce(300, 0)
    if vel_x > 200 then
    	bodies[1]:setLinearVelocity(200, vel_y) -- Max speed
    end
    char = love.graphics.newImage("sprite-r.gif"); -- Turn the sprite around
  elseif love.keyboard.isDown("left") then
    bodies[1]:applyForce(-300, 0)
     if vel_x < -200 then
    	bodies[1]:setLinearVelocity(-200, vel_y)
    end
    char = love.graphics.newImage("sprite-l.gif"); -- Turn the sprite around
  end
  if love.keyboard.isDown("up") then
     if jumplimit == false then -- If it's okay to jump...
     	if vel_y < -600 then
     		jumplimit = true -- You've jumped enough! (This is a little broken, allows for multiple jumps pre -600. working on it!)
     	else
     		bodies[1]:applyForce(0, -800) -- Do some jumping!
     	end
     end
     if vel_y == 0 then
  		jumplimit = false -- Occurs upon landing, okay to jump again!
  	 end
  end
  box_x, box_y = bodies[1]:getPosition( ) -- Get the shape position so the sprite can follow
end

function love.draw()
  love.graphics.draw(char, box_x, box_y-70) -- Sprite always follows the physics circle
  local x1, y1, x2, y2, x3, y3, x4, y4 = shapes[0]:getBoundingBox()
  local boxwidth = x3 - x2 --calculate the width of the box
  local boxheight = y2 - y1 --calculate the height of the box
  love.graphics.circle("fill", bodies[1]:getX(), bodies[1]:getY(), 0, 0) -- invisible
end

User avatar
The Burrito
Party member
Posts: 153
Joined: Mon Sep 21, 2009 12:14 am
Contact:

Re: Physics - Reducing friction upon contact

Post by The Burrito » Sun Aug 22, 2010 6:37 am

Reducing the friction probably isn't the best way to do it, but you can try shapes[1]:setFriction( .1 ) somewhere after you've defined shapes[1].

A few helpful things:

Try setting up something with keypressed for jumping, and then using some kind of collision detection to say if jumping is valid.

Avoid using setWhatever when you can and use applyForce or applyImpulse (impulse works better for character movement IMO)

When drawing sprites use offset instead of adjusting your x/y values (it doesn't matter if there's no rotation, but its a good habit) http://love2d.org/wiki/love.graphics.draw

Post Reply

Who is online

Users browsing this forum: Exabot [Bot], pedrosgali and 5 guests