Page 1 of 1

Need help applying Physics

Posted: Wed Feb 10, 2016 5:10 am
by BlakeBarnes00
So I have been relearning LUA and the LOVE engine here recently, and I have been making small little games. I have been looking into adding some physics (where I jump and then fall back down towards the ground, friction, and so forth), I have looked up some tutorials, looked at some other projects, and so much, but can never figure this out. Can I get a little help?

Re: Need help applying Physics

Posted: Wed Feb 10, 2016 12:56 pm
by sefan
Take a look at Bump, viewtopic.php?f=5&t=79223
Its a nice collision detection library.

Re: Need help applying Physics

Posted: Thu Feb 11, 2016 12:46 am
by BlakeBarnes00
sefan wrote:Take a look at Bump, viewtopic.php?f=5&t=79223
Its a nice collision detection library.
I want to know how to do this without using libraries just so I get a learning experience. Is there any tutorial for this?

Re: Need help applying Physics

Posted: Thu Feb 11, 2016 2:12 am
by bobbyjones
Bump isn't physics BTW. It's just collision detection. I would say that as long as you understand aabb you could just experiment until you get the feel you want or reach a specific problem. Physics in games is something you design around your game. I'm assuming you know how to move things on the screen and if you Google "kinetic physics forumlas" you should be able to design something to fit your needs yourself. It's the best way to learn. Because then you can apply that to anything.

Edit: now that I am on my pc i can expand a bit. in physics to determine ones displacement there is this formula.

Code: Select all

 
 	d = posInitial + Vel*time --this uses vectors
 	--this determines displacement on each axes seperatly
 	dx = xInitial + Velx*time 
 	dy = yInitial + Vely*time 
 	
 	--heres how to calculate Velocity 
 	vel = velinitial + at
 	
 	--these formulas can be found by googling along with formulas for friction and such.
 	
There is 2 approaches presented here that you can take you can use vectors (a magnitude and angle) or indivual components. Its easier to use components. now we just have to plug in information to make it relevant for our game.

Code: Select all

	--x and y initial can be our players pos at that frame. 
	--Velocity is your velocity from the previous frame with acceleration added to it.
	--acceleration will be how you can control the player or entity. 
	--t will be deltaTime.
	
	local player = {
		x = 0,
		y = 0,
		velx = 0,
		vely = 0,
		accelerationx = 0, 
	}
	
	function love.update(dt)
		player.accelerationx = 0
		player.accelerationy = 0
		-- by controlling acceleration we control the player
		if love.keyboard.isDown('a') then
			player.accelerationx = -50
		elseif  love.keyboard.isDown('d') then
			player.accelerationx = 50
		end
		
		--first calculate x velocity
		player.velx = player.velx + player.accelerationx*dt
		--we use gravity as y acceleration.
		player.vely = player.vely + 9.8*dt --assuming we are using meters, gravity is 9.8 
		
		--then we calculate the new x and y.
		player.x = player.x + player.velx*dt
		player.y = player.y + player.vely*dt
	end
	
	function love.draw()
		love.graphics.rectangle("fill",player.x,player.y,20,20)
	end
	
Now this is a very simple example. If you add collision then you will have working physics. Basically the more you want the more you do. If you want friction and mass then google the formula for them and how they effect things. As you go through and implement each formula your game will end up with more realistic physics.

Re: Need help applying Physics

Posted: Thu Feb 11, 2016 2:32 am
by BlakeBarnes00
bobbyjones wrote:Bump isn't physics BTW. It's just collision detection. I would say that as long as you understand aabb you could just experiment until you get the feel you want or reach a specific problem. Physics in games is something you design around your game. I'm assuming you know how to move things on the screen and if you Google "kinetic physics forumlas" you should be able to design something to fit your needs yourself. It's the best way to learn. Because then you can apply that to anything.
Thanks man!

Re: Need help applying Physics

Posted: Thu Feb 11, 2016 3:12 am
by bobbyjones
BlakeBarnes00 wrote:
bobbyjones wrote:Bump isn't physics BTW. It's just collision detection. I would say that as long as you understand aabb you could just experiment until you get the feel you want or reach a specific problem. Physics in games is something you design around your game. I'm assuming you know how to move things on the screen and if you Google "kinetic physics forumlas" you should be able to design something to fit your needs yourself. It's the best way to learn. Because then you can apply that to anything.
Thanks man!
See me edited answer.
Also your .love is not made correctly