Physics problems.

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.
croftxd
Prole
Posts: 7
Joined: Tue Mar 11, 2014 4:58 pm

Physics problems.

Post by croftxd »

I've a small program which has an animated character Player { .... } which sets position, speed etc.. and objects.door (shamefully stolen from an example program). However, I'm having issues in allowing my player character to jump on to the door, bump in to the door etc...

My initial thought was that a physics.body would have to be applied to the player{} but this threw back errors (probably down to poor syntax or something)

So would this be the case? That a body needs to be applied to the player? Or am I way off?

Any help or suggestions would be greatly appreciated.



UPDATE Being playing with the code and think I'm closer now than I was before, but still a little confused as to whats going on and why the physics aren't working quite right.

Code: Select all

-- love 2d 0.8.0
require("AnAL")

function love.load()
--love.graphics.setMode(650, 650, false, true, 0) --set the window dimensions to 650 by 650
	door=love.graphics.newImage("door.png")
	love.physics.setMeter(64)
	world = love.physics.newWorld(0, 29.81*64, true)
	
	player = {}
    player.x=400
	player.y=450
	player.speed = 300
	player.y_velocity = 450
	player.tim=love.graphics.newImage("tim.png")
	player.body= love.physics.newBody(world, player.x,player.y, "dynamic")
	player.shape = love.physics.newRectangleShape(0,0,100,50)
	player.fixture=love.physics.newFixture(player.body, player.shape);

	
	gravity = 400
	jump_height = -300
	
	local imgwalk = love.graphics.newImage("timwalking.png")
	local imgwalk1 = love.graphics.newImage("timwalking1.png")
	local imgjump = love.graphics.newImage("timjumping.png")
	
	anim = newAnimation(imgwalk, 128.5, 150, 0.08, 0)
	anim:setMode("loop")
	anim1 = newAnimation(imgwalk1, 128.5, 150, 0.08, 0)
	anim1:setMode("loop")
	anim2 = newAnimation(imgjump, 128.5, 150, 0.08, 0)
	anim2:setMode("bounce")
	activeImage = "standing" -- character states
	
	objects = {}
	objects.door = {}
	objects.door.body = love.physics.newBody(world,50,550) --remember, the shape (the rectangle we create next) anchors to the body from its center, so we have to move it to 300/2, 300-50/2)
	objects.door.shape = love.physics.newRectangleShape(0,0,100,100) --make a rectangle with a width of 300 and a height of 50
	objects.door.fixture = love.physics.newFixture(objects.door.body, objects.door.shape);
	
end

function love.update(dt)
world:update(dt)



	if player.y_velocity ~= 450 then -- we're probably jumping
        player.y = player.y + player.y_velocity * dt		-- dt means we wont move at
        -- different speeds if the game lags
        player.y_velocity = player.y_velocity + gravity * dt
		activeImage = "jumping"
		anim2:update(dt)
        if player.y > 450 then -- we hit the ground again
            player.y_velocity = 450
            player.y = 450
        end
	end
	
	
    if love.keyboard.isDown("right") then
	  player.x = player.x + (player.speed * dt)
      activeImage = "walkingright"
      anim:update(dt)
	  else
	  activeImage = "standing"
	 
   end
   
	if love.keyboard.isDown("left") then
     player.x = player.x - (player.speed * dt)
	 activeImage = "walkingleft"
	 anim1:update(dt)
   end
	
	

   
end


function love.keypressed(key)
    if key == " " then
        if player.y_velocity == 450 then -- we're probably on the ground, let's jump
            player.y_velocity = jump_height
        end
    end
end

function love.draw()
		 

	x, y = objects.door.body:getPosition( )
	love.graphics.draw(door,x-50,y-50)
		
	if activeImage == "walkingright" then
	   love.graphics.print('Walking right', 400, 300)
	   anim:draw(player.x,player.y)	
	   end
    if activeImage == "walkingleft" then
	love.graphics.print('Walking left', 400, 300)
	   anim1:draw(player.x,player.y)
	   end
	if activeImage == "jumping" then
		love.graphics.print('Jumping', 400, 300)
		anim2:draw(player.x,player.y)
		end
	if activeImage == "standing" then
		love.graphics.print('Standing', 400, 300)
		love.graphics.draw(player.tim, player.x, player.y)
	end
	

	
	
end
User avatar
tio
Citizen
Posts: 61
Joined: Thu Dec 12, 2013 1:04 pm
Location: BR BR?
Contact:

Re: Physics problems.

Post by tio »

Looks like you're moving the player manually and using love.physics for the door. The problem with that is that you'll have a lot of headache to sync both of them. The best approach here is either move your player also using love.physics, or make door physics by hand (IMO the best option).
A .love file would help a lot too :nyu:
croftxd
Prole
Posts: 7
Joined: Tue Mar 11, 2014 4:58 pm

Re: Physics problems.

Post by croftxd »

tio wrote:Looks like you're moving the player manually and using love.physics for the door. The problem with that is that you'll have a lot of headache to sync both of them. The best approach here is either move your player also using love.physics, or make door physics by hand (IMO the best option).
A .love file would help a lot too :nyu:

Ok, so any suggestions on how to make the door physics by hand?

It might be worth noting that I plan on adding more obstacles in the future, if that would be worth considering?
Last edited by croftxd on Thu Mar 13, 2014 4:31 pm, edited 1 time in total.
croftxd
Prole
Posts: 7
Joined: Tue Mar 11, 2014 4:58 pm

Re: Physics problems.

Post by croftxd »

Here's the love file.
Attachments
game.love
(4.28 MiB) Downloaded 146 times
User avatar
tio
Citizen
Posts: 61
Joined: Thu Dec 12, 2013 1:04 pm
Location: BR BR?
Contact:

Re: Physics problems.

Post by tio »

If the door should sustain the player above it, you can use an AABB approach (check rect positions, and compensate movement, you can see some tips here, or search about AABB physics. But don't mess with bounciness unless you're confident with the basic).
croftxd
Prole
Posts: 7
Joined: Tue Mar 11, 2014 4:58 pm

Re: Physics problems.

Post by croftxd »

tio wrote:If the door should sustain the player above it, you can use an AABB approach (check rect positions, and compensate movement, you can see some tips here, or search about AABB physics. But don't mess with bounciness unless you're confident with the basic).

Is that the easiest way to go about it do you think? Or is there any other way of getting similar results using love.physics?

Really seems over complicated for what I'm after (not disrespecting the help you gave, just seeing if there is anyway to simplify it)
croftxd
Prole
Posts: 7
Joined: Tue Mar 11, 2014 4:58 pm

Re: Physics problems.

Post by croftxd »

Can anyone else make any suggestions? Surely looking at the https://love2d.org/wiki/Tutorial:Physics there should be a simpler way to get the physics going?
Attachments
game1.love
(4.37 MiB) Downloaded 125 times
User avatar
tio
Citizen
Posts: 61
Joined: Thu Dec 12, 2013 1:04 pm
Location: BR BR?
Contact:

Re: Physics problems.

Post by tio »

Sure, no problem :)
You will need a body (use a static for the door and a dynamic for the char), a rect or polygonal shape for each one (I would use the first one) and a fixture to connect the shapes to the bodies. For the char body, you'll probably want to set fixed rotation since your char doesn't spin. Then, apply a force to the body so it start to move (apply on X to walk and to -Y to jump).

Try these and show your results ^^
User avatar
nfey
Citizen
Posts: 63
Joined: Tue Feb 18, 2014 9:50 am
Location: Romania

Re: Physics problems.

Post by nfey »

From what i can see, you are using player.x/playr.y to draw and move the caracter. You should be using player.body.x/y instead, because the way you are doing it now, your "player" object has the updated position after running/jumping, but the body object does not.
User avatar
tio
Citizen
Posts: 61
Joined: Thu Dec 12, 2013 1:04 pm
Location: BR BR?
Contact:

Re: Physics problems.

Post by tio »

nfey wrote:From what i can see, you are using player.x/playr.y to draw and move the caracter. You should be using player.body.x/y instead, because the way you are doing it now, your "player" object has the updated position after running/jumping, but the body object does not.
If he wants to use physics, it's not that "healthy" to move a body using absolute positions. Apply forces is better and don't break physics simulation calcs ;)
Post Reply

Who is online

Users browsing this forum: No registered users and 57 guests