Page 1 of 1

Why is this not working?

Posted: Fri Dec 07, 2012 1:12 pm
by ixjackinthebox
Why is the physics not working in this (I am not good with love physics) please help

main

Code: Select all

require "Code/GameCode"
require "Levels/Level"
GameCode

Code: Select all

--PlayersHealth = 100

local Move = 2.2

function love.update(dt)
World:update(dt)
----------------------------------------------------------------------------------
--movement
----------------------------------------------------------------------------------
	--left
	if love.keyboard.isDown("a") or love.keyboard.isDown("left") then
		Players_x = Players_x - Move
	end
	--right
	if love.keyboard.isDown("d") or love.keyboard.isDown("right")then
		Players_x = Players_x + Move
	end
	--up
	if love.keyboard.isDown("w") or love.keyboard.isDown("up")then
		Players_y = Players_y - Move
	end
	--down
	if love.keyboard.isDown("s") or love.keyboard.isDown("down")then
		Players_y = Players_y + Move
	end
-----------------------------------------------------------------------------------
--Health
-----------------------------------------------------------------------------------

end
Level

Code: Select all

Players_x = 76
Players_y = 118

function love.load()
	love.physics.setMeter( 4 )
	World = love.physics.newWorld( 0, 500, true)
	
-------------------------------------------------------------------------
	-- Player
	PlayerSprite = love.graphics.newImage ("Sprites/Player/Player.png")
	-- Player Physics
	PlayerB = love.physics.newBody( World, Players_x, Players_y, "dynamic")
	PlayerS = love.physics.newRectangleShape( 22, 34 )
	PlayerF = love.physics.newFixture( PlayerB, PlayerS, 1)
	-- Background
	Background = love.graphics.newImage("Sprites/Background.png")
-------------------------------------------------------------------------
	-- Physics
	
	floor1B = love.physics.newBody( World, 61, 152, "static")
	floor1S = love.physics.newRectangleShape( 300, 30)
	floor1F = love.physics.newFixture( floor1B, floor1S, 1)
	
	floor2B = love.physics.newBody( World, 435, 182, "static")
	floor2S = love.physics.newRectangleShape( 300, 30)
	floor2F = love.physics.newFixture( floor2B, floor2S, 1)
	
	groundB = love.physics.newBody( World, 0, 570, "static")
	groundS = love.physics.newRectangleShape( 800, 30)
	groundF = love.physics.newFixture( groundB, groundS, 1)
	
end

function love.draw()
	love.graphics.draw( Background, 0, 0)
	love.graphics.draw (PlayerSprite, Players_x, Players_y)
end
edit: uploaded love file

Re: Why is this not working?

Posted: Fri Dec 07, 2012 2:54 pm
by Robin
Could you please pack your project in a .love and upload it as an attachment? As the rules state, it is much easier for us to help you with a .love.

Re: Why is this not working?

Posted: Fri Dec 07, 2012 6:41 pm
by Boolsheet
You don't see the results of the physics simulation because you don't use its results to draw the character. A very simple way to change that is to get the position of the player body and use it to draw the player image.

Code: Select all

love.graphics.draw (PlayerSprite, PlayerB:getX(), PlayerB:getY())
Now it still won't move much because you don't exert any forces on the player body. You can do something like this in your movement code, but that is just a very simple example again.

Code: Select all

PlayerB:applyLinearImpulse(50, 0) -- Pushes body to the right.
The wiki warns that love.physics may not be the best thing for a very simple platformer. I'm sure Box2D can be configured in some way to give you the results you want, but you'll need a very good understanding of how everything works.

Re: Why is this not working?

Posted: Sat Dec 08, 2012 10:22 am
by ixjackinthebox
Thanks for that
I saw the warning but I am not trying to make a game just learn love.physics

Re: Why is this not working?

Posted: Sat Dec 08, 2012 11:04 am
by ixjackinthebox
Ok so I used Body:applyForce to try to make the player move but it does not seem to do anything.

Re: Why is this not working?

Posted: Sat Dec 08, 2012 11:50 am
by Ellohir
I fumbled a bit with the numbers and managed to make the guy move. I changed gravity from 500 to 10, and the force applied from 750 to 2000. He still didn't jump, though.