Is realistic jumping even possibru without 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.
User avatar
The Burrito
Party member
Posts: 153
Joined: Mon Sep 21, 2009 12:14 am
Contact:

Re: Is realistic jumping even possibru without love.physics

Post 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.
User avatar
Jack5500
Party member
Posts: 148
Joined: Wed Dec 07, 2011 8:38 pm
Location: Hamburg, Germany

Re: Is realistic jumping even possibru without love.physics

Post 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... :(
Attachments
no-phys.love
Player escapes via -Y somehow
(105.92 KiB) Downloaded 102 times
User avatar
The Burrito
Party member
Posts: 153
Joined: Mon Sep 21, 2009 12:14 am
Contact:

Re: Is realistic jumping even possibru without love.physics

Post 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.
User avatar
cattail
Citizen
Posts: 56
Joined: Mon Feb 13, 2012 4:11 pm

Re: Is realistic jumping even possibru without love.physics

Post 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.
Attachments
no-phys-jump.love
This one can jump while left|right.
(106.26 KiB) Downloaded 89 times
User avatar
tsturzl
Party member
Posts: 161
Joined: Fri Apr 08, 2011 3:24 am

Re: Is realistic jumping even possibru without love.physics

Post 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.
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Is realistic jumping even possibru without love.physics

Post 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.
User avatar
tsturzl
Party member
Posts: 161
Joined: Fri Apr 08, 2011 3:24 am

Re: Is realistic jumping even possibru without love.physics

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 3 guests