Page 2 of 2

Re: Is realistic jumping even possibru without love.physics

Posted: Sat Dec 10, 2011 9:06 pm
by The Burrito
I think the problem is everybody has slightly different ways they like to handle this stuff. You could try directly setting player.velocity.y instead of player.v, I think that might do something.

A good way to start is just make the jump key push the player up, then put in checks like only allow it to happen for a fraction of a second and reset the timer when the player lands, etc until you get something that does roughly what you think it should.

love.physics (box2D) is actually really poor for creating "realistic" jumping. I use it for In The Dark and it takes a lot of tweaking just to make non exploitable jump code. One big problem with making jumps feel right is variation in speed. I do this:

Code: Select all

player.b:applyLinearImpulse(0,((-113 + math.abs((jumpLocY-90-bump.y)/3))) *physdt )
Which I'll grant is a horrible mess, but what I do is define the maximum height the player can reach (jumpLocY), and apply increasing acceleration until the player gets about half way, then apply less and less force for the rest of the jump. It works reasonably well and allows for pretty decent "mario like" small jumps by quickly tapping the jump key.

Re: Is realistic jumping even possibru without love.physics

Posted: Sat Dec 10, 2011 10:58 pm
by Jack5500

Code: Select all

function love.load()


 -- credit to tentus
 
   player = {
      x = 400,
      y = 465,
      w = 20,
      h = 70,
      v = false   -- not just 0 thrust, but NO thrust at all
   }

      ground= {
      x = 0,
      y = 535,
      w = 800,
      h = 100
   }
 
   initial = -200   -- start with an upward for of 200 px per second
   falloff = -50   -- we lose 50 px of upward thrust per second
   


 Tileset = love.graphics.newImage('images/foresttiles01.png')
 TileW, TileH = 60,60
 local tilesetW, tilesetH = Tileset:getWidth(),Tileset:getHeight() 

 love.graphics.setBackgroundColor( 54, 54, 54)
 
 
local quadInfo = {
  { 'a', 0, 300 }, -- air
  { '#', 60,  0 }, -- grass
  { '*', 60, 120  }, -- flowers
  { '^', 120, 120 },  -- other flowers
  { 'g', 0, 0 } --other grass
}


Quads={}
for _,info in ipairs(quadInfo) do
  -- info[1] = character, info[2]= x, info[3] = y
Quads[info[1]] = love.graphics.newQuad(info[2], info[3], TileW, TileH, tilesetW, tilesetH)
end


 --Map Tile Layout
local tileString = [[
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
*^^***^^**^^**
#g#g#g#g#g#g#g
]]


 -- Translate the characters into the right tile parts of the Tileset
TileTable = {}

local width = #(tileString:match("[^\n]+"))

for x = 1,width,1 do TileTable[x] = {} end

local rowIndex,columnIndex = 1,1
for row in tileString:gmatch("[^\n]+") do
  assert(#row == width, 'Map is not aligned: width of row ' .. tostring(rowIndex) .. ' should be ' .. tostring(width) .. ', but it is ' .. tostring(#row))
  columnIndex = 1
  for character in row:gmatch(".") do
    TileTable[columnIndex][rowIndex] = character
    columnIndex = columnIndex + 1
  end
  rowIndex=rowIndex+1
end
end





function love.update(dt)

xc = love.mouse.getX( )
yc = love.mouse.getY( )



if player.v then --movement per update
	player.v = player.v + (falloff * dt)
	player.y = player.y + (player.v * dt)
end

if player.y + player.h >= ground.y then -- stop the box when touching ground
	player.y = ground.y - player.h
	player.v = false
end

end

function love.keypressed (key)
	if key == "w" then
		if not player.v then   -- can't jump if we're already jumping
		player.v = initial
		end
	end
end

function love.draw()
lg = love.graphics
--draw the actors
   lg.rectangle("fill", player.x, player.y, player.w, player.h)
   lg.rectangle("fill", ground.x, ground.y, ground.w, ground.h)

  
--print me some debug info
lg.print("FPS: "..love.timer.getFPS(), 8,10)
lg.print("Mouse X: " ..xc, 8, 20)
lg.print("Mouse Y: " ..yc, 8, 30)
lg.print("Player Y: " ..player.x, 8, 40)
lg.print("Player X: " ..player.y, 8, 50)

-- draw the Tilemap
  for columnIndex,column in ipairs(TileTable) do
    for rowIndex,char in ipairs(column) do
      local x,y = (columnIndex-1)*TileW, (rowIndex-1)*TileH
      love.graphics.drawq(Tileset, Quads[char], x, y)
    end
  end


end
Okay, sorry for that messy code, I followed Burrito's advise and cleaned the code and decided to fully remove HC. But somehow the player now spawns and escapes into the offscreen via -y. Can you please take a look?. I don't see any mistakes... :(

Re: Is realistic jumping even possibru without love.physics

Posted: Sat Dec 10, 2011 11:58 pm
by The Burrito
Make falloff positive (your jump applys negative force, so adding more negative force sends the player off into space) try setting initial and falloff to something like -500, and 400 and you'll have a jump that feels pretty decent.

Re: Is realistic jumping even possibru without love.physics

Posted: Thu Mar 22, 2012 5:47 am
by cattail
I fix it since it's a simple demo.
Add player.vx , add key with up\left\right , a\d.
Not nice for havn't check key isdown , but it's works.

Re: Is realistic jumping even possibru without love.physics

Posted: Thu Mar 22, 2012 6:09 am
by tsturzl
Middle school science class man: Acceleration and Gravity.

This is exactly how love.physics does it, but love.physics is more than a simple gravity equation. It's a lot to load into memory for how much of it you aren't using. Its like using a Shotgun to hammer in a nail, just use the hammer.

Re: Is realistic jumping even possibru without love.physics

Posted: Thu Mar 22, 2012 9:19 am
by Jasoco
tsturzl wrote:Middle school science class man: Acceleration and Gravity.

This is exactly how love.physics does it, but love.physics is more than a simple gravity equation. It's a lot to load into memory for how much of it you aren't using. Its like using a Shotgun to hammer in a nail, just use the hammer.
What about a nailgun? Which is a shotgun that fires nails.

Re: Is realistic jumping even possibru without love.physics

Posted: Fri Mar 23, 2012 5:48 pm
by tsturzl
Jasoco wrote:
tsturzl wrote:Middle school science class man: Acceleration and Gravity.

This is exactly how love.physics does it, but love.physics is more than a simple gravity equation. It's a lot to load into memory for how much of it you aren't using. Its like using a Shotgun to hammer in a nail, just use the hammer.
What about a nailgun? Which is a shotgun that fires nails.
That would work fine.