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

Is realistic jumping even possibru without love.physics ?

Post by Jack5500 »

I looking through a dozen of games by now. But the easiest way I found was in mario 7.2 and that used love.physics. So by now I'm using Hardon Collider and Pixel movement. Would be any good to use love.physics or should I just use another method?
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: Is realistic jumping even possibru without love.physics

Post by GijsB »

it's easily achieved. You have a yvelocity, and every 0.1 second this yvelocity goes up with +0.1, then update the position of the thing + yvelocity. When the thing has to jump you change the yvelocity to like -5!

(the numbers i named will not achieve a good looking jump effect, you have to change it a little)
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 »

I extracted this from your text, but I made something terribly wrong, would you mind helping me?

Code: Select all

player.velocity.y = 0.1 * dt
if player.velocity.y == 10 then
player.velocity.y = -5
end
User avatar
Metalcookie
Prole
Posts: 16
Joined: Sat Dec 10, 2011 2:57 pm
Location: Netherlands

Re: Is realistic jumping even possibru without love.physics

Post by Metalcookie »

Jack5500 wrote:I extracted this from your text, but I made something terribly wrong, would you mind helping me?
I think it's more like this:

Code: Select all

player.velocity.y = player.velocity.y + 0.1 * dt     --each second the velocity is increased by 0.1
player.y = player.y + player.velocity.y              --moves the player with the calculated velocity
and to go up(jump) the velocity.y has to be negative
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Is realistic jumping even possibru without love.physics

Post by tentus »

Here's an example program that should be pretty extensible. Sorry it's not very pretty, but I didn't want any excess code confusing anyone.

Code: Select all

function love.load()
	player = {
		x = 400,
		y = 480,
		w = 20,
		h = 20,
		v = false	-- not just 0 velocity, but NO velocity at all
	}
	ground = {
		x = 0,
		y = 500,
		w = 800,
		h = 100
	}
	initial = -200	-- start with an upward (negative) for of 200 px per second
	gravity = 50	-- we gain 50 px of gravitational thrust per second
end

function love.update(dt)
	if player.v then
		player.v = player.v + (gravity * dt)
		player.y = player.y + (player.v * dt)
	end
	if player.y + player.h >= ground.y then
		player.y = ground.y - player.h
		player.v = false
	end
end

function love.draw()
	love.graphics.rectangle("fill", player.x, player.y, player.w, player.h)
	love.graphics.rectangle("fill", ground.x, ground.y, ground.w, ground.h)
end

function love.keypressed()
	if not player.v then	-- can't jump if we're already jumping
		player.v = initial
	end
end
Kurosuke needs beta testers
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 »

Okay if I understand your code right, this part...

Code: Select all

   if player.v then
      player.v = player.v + (gravity * dt)
      player.y = player.y + (player.v * dt)
   end
...calculates the movement speed per "update" and this part...

Code: Select all

   if player.y + player.h >= ground.y then 
      player.y = ground.y - player.h
      player.v = false
   end
...stops it when it touches the ground. Or am I wrong?
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Is realistic jumping even possibru without love.physics

Post by tentus »

Jack5500 wrote:Okay if I understand your code right, this part...

Code: Select all

   if player.v then
      player.v = player.v + (gravity * dt)
      player.y = player.y + (player.v * dt)
   end
...calculates the movement speed per "update" and this part...

Code: Select all

   if player.y + player.h >= ground.y then 
      player.y = ground.y - player.h
      player.v = false
   end
...stops it when it touches the ground. Or am I wrong?
Yep!
Kurosuke needs beta testers
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 »

That means you just made HC completly unneccessary. I'm thinking about removeing it, since those too are interfering, because I created the player and the player.box now. IT's all achieveable without it, am i right?
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Is realistic jumping even possibru without love.physics

Post by Taehl »

You can do anything you want. Things like Box2D, HardonCollider, and such are things you could fully remake if you desired - they're just there to try to make it easier for you.

My game Underlife is a platformer, and doesn't use any physics library. Here's a trimmed-down version of what I have:

Code: Select all

pc = {x=0,y=0, xv=0,yv=0 }	-- player character has a position and velocity
local maxSpeed = 28.9999	-- terminal velocity, in meters per second
local framerate = 1/58 -- 1/58 is based on maxSpeed (must move less than .5 in an update to prevent penetration in my engine)
local p = pc	-- not only saves me a little typing, but also gives a minuscule performance boost!


function pc.update(dt)
	local accum = dt
	while accum > 0 do		-- accumulator for physics! no more penetration!
		local dt = math.min( framerate, accum )
		accum = accum - dt
		
		do	-- normal movement
			if not p.falling then	-- touching the ground
				if control.tap.jump then p.xv, p.yv = p.xv+control.horiz*4, (control.vert-.875)*5.5 end	-- jumping
				p.xv = p.xv + control.horiz*30*(control.walk and .4 or 1)*(p.resetX and 0 or 1) * dt	-- running
				p.xv = p.xv*(1-dt*(p.crawling and 10 or 4))		-- horizontal friction
			else
				p.xv = p.xv*.99 + control.horiz*5*dt		-- air control
			end
		end
		
		do	-- collision
			-- Underlife is tile-based, and tiles are stored in form of map[x][y] = something if there's a tile, or nil if not (where x and y are integers)
			p.falling = true
			p.yv = math.clamp(-maxSpeed, p.yv+30*dt, maxSpeed)	-- gravity
			p.xv = math.clamp(-maxSpeed, p.xv, maxSpeed)
			
			-- vertical
			p.y = p.y + p.yv * dt
			if map[math.round(p.x-.1)][math.ceil(p.y)] or map[math.round(p.x+.1)][math.ceil(p.y)] then
				p.falling, p.y, p.yv = false, p.y-(p.y-math.round(p.y)), 0
			end
			if not p.crawling and map[math.round(p.x-.15)][math.floor(p.y-1)] or map[math.round(p.x+.15)][math.floor(p.y-1)] then
				p.y, p.yv = p.y+(math.round(p.y)-p.y), 0
			end
			
			-- horizontal
			p.x = p.x + p.xv * dt
			if map[math.floor(p.x)][math.round(p.y)] then p.x, p.xv = math.ceil(p.x), p.falling and p.xv*-.1 or 0 end
			if map[math.ceil(p.x)][math.round(p.y)] then p.x, p.xv = math.floor(p.x), p.falling and p.xv*-.1 or 0 end
			if not p.crawling then
				if map[math.floor(p.x)][math.round(p.y-1)] then p.x, p.xv = math.ceil(p.x), p.falling and p.xv*-.1 or 0 end
				if map[math.ceil(p.x)][math.round(p.y-1)] then p.x, p.xv = math.floor(p.x), p.falling and p.xv*-.1 or 0 end
			end
			
			-- corner pushers (you slip off of corners, instead of standing on them Mario-style)
			if p.falling then
				if map[math.floor(p.x)][math.ceil(p.y)] then p.x = p.x + 2*dt end
				if map[math.ceil(p.x)][math.ceil(p.y)] then p.x = p.x - 2*dt end
			end
		end
	end
end
(Note: This isn't something you can copy-paste into your game and expect it to work - it's custom-tailored to Underlife. It's an example, not a solution)
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
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 »

Somehow the code seems not to work. The jump isn't performed at all. Where did i go wrong?

Code: Select all

HC = require"libs"

--function to deal with collides (duh)
function on_collide (dt, shape_a, shape_b)
ground_touched = false
--Who is who?
local ground
if shape_a == player then
ground = shape_b
ground_touched = true
else  ground = shape_a
end

local bx, by = player:center()
local px,py = ground:center()
player.velocity.y = 0
player.velocity.x = 0 --not sure if that might interfere :/
end

function love.load()

Collider = HC(100, on_collide)
 
 -- credit to tentus
 
   player_box = {
      x = 400,
      y = 0,
      w = 25,
      h = 75,
      v = false   -- not just 0 thrust, but NO thrust at all
   }

      ground_box = {
      x = 0,
      y = 535,
      w = 800,
      h = 20
   }
 
   initial = -200   -- start with an upward for of 200 px per second
   falloff = -50   -- we lose 50 px of upward thrust per second
   
player = Collider:addRectangle(player_box.x,player_box.y, player_box.w, player_box.h)
ground = Collider:addRectangle(ground_box.x, ground_box.y, ground_box.w, ground_box.h)

player.velocity = {x=0, y=300}

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


player:move(player.velocity.x * dt, player.velocity.y *dt)

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

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


if love.keyboard.isDown('w') then
--player:move(0, -100 * dt)


   if not player.v then   -- can't jump if we're already jumping
      player.v = initial
	  end
ground_touched = false
elseif love.keyboard.isDown('s')  and ground_touched == false then
player:move(0, 100 * dt)
elseif love.keyboard.isDown('a') then
player:move(-10, 0 * dt)
ground_touched = false
elseif love.keyboard.isDown('d') then
player:move(10, 0 * dt)
ground_touched = false
end
Collider:update(dt)

end

function love.draw()

--Hadron Collider
player:draw('fill')
ground:draw('fill')


	local lg = love.graphics
lg.print("FPS: "..love.timer.getFPS(), 8,8)
lg.print("Mouse X:" ..xc, 10, 18)
lg.print("Mouse Y" ..yc, 10, 28)

  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
  	local lg = love.graphics
	lg.setColor(255,255,255, 255)


end
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests