Page 1 of 1
[SOLVED]Love2d physics body and hump camera rotation
Posted: Tue Jul 28, 2015 2:23 pm
by frisbro
Hello Lovers!
I'm using the hump camera lib in my game, but i cant figure out how to rotate the camera to the same angle as a physic body rotation
I tried using the code bellow but, the rotation is very strange.
Code: Select all
cam:rotateTo(object.body:getAngle())
--Thanks
Re: Love2d physics body and hump camera rotation
Posted: Tue Jul 28, 2015 2:45 pm
by RagingDave
Does your getAngle() function return radians? Maybe you need to convert from degrees.
angle in rad = angle in degree * (pi/180)
edit: sorry I just checked and the function indeed returns radians. maybe post more code.
Re: Love2d physics body and hump camera rotation
Posted: Tue Jul 28, 2015 2:51 pm
by frisbro
I'm just creating a rectangle and then i try to rotate the hump camera :/
Re: Love2d physics body and hump camera rotation
Posted: Tue Jul 28, 2015 3:02 pm
by RagingDave
Especially if it's a simple example you should provide the complete .love package.. else it's just guessing.
Did you attach the camera to the scene before drawing your rectangle?
Like shown in
http://vrld.github.io/hump/#hump.cameracamera:attach
Re: Love2d physics body and hump camera rotation
Posted: Tue Jul 28, 2015 3:15 pm
by frisbro
Yes i attach it, but i think there are to many files for the other parts of the game but here is what im trying in code
i dont think this code is runnable and it's not my full code it's just a code to illustrate what i'm trying to do -- sorry for my bad english
Code: Select all
Camera = require "libs/hump/camera"
function love.load()
love.physics.setMeter(32)
World = love.physics.newWorld(0, 0, true)
playerBody = love.physics.newBody(World, x, y, "dynamic")
playerShape = love.physics.newRectangleShape(32, 32)
playerFixture = love.physics.newFixture(self.body, self.shape, 1)
playerFixture:setDensity(1)
playerFixture:setFriction(1)
playerFixture:setRestitution(0)
end
function love.update(dt)
World:update(dt)
cam:rotateTo(playerBody:getAngle())
end
function love.draw()
cam:attach()
love.graphics.polygon("fill", playerBody:getWorldPoints(playerBody:getFixtureList()[1]:getShape():getPoints()))
cam:detach()
end
Re: Love2d physics body and hump camera rotation
Posted: Tue Jul 28, 2015 3:36 pm
by RagingDave
I don't know what you are trying to achieve but your code seems fine from what I can see. I adjusted the example to be runnable on its own. You can rotate by 1 with the tab key.
I guess this is not what you wanted but what do you expect to behave different?
Code: Select all
Camera = require "libs/hump/camera"
cam = Camera(0,0,1,0)
function love.load()
love.physics.setMeter(32)
World = love.physics.newWorld(0, 0, true)
playerBody = love.physics.newBody(World, 0, 0, "dynamic")
playerShape = love.physics.newRectangleShape(32, 32)
playerFixture = love.physics.newFixture(playerBody, playerShape, 1)
playerFixture:setDensity(1)
playerFixture:setFriction(1)
playerFixture:setRestitution(0)
end
function love.update(dt)
World:update(dt)
cam:rotateTo(playerBody:getAngle())
end
function love.draw()
cam:attach()
love.graphics.polygon("fill", playerBody:getWorldPoints(playerBody:getFixtureList()[1]:getShape():getPoints()))
cam:detach()
end
function love.keypressed(key)
if key == "tab" then
playerBody:setAngle(playerBody:getAngle() + 1 )
print(playerBody:getAngle())
end
end
Re: Love2d physics body and hump camera rotation
Posted: Tue Jul 28, 2015 4:28 pm
by frisbro
The problem is that the camera doesen't rotate to the body rotation :/
Re: Love2d physics body and hump camera rotation
Posted: Tue Jul 28, 2015 5:35 pm
by RagingDave
Ok I "think" I understand now. You want the camera to follow the player? So not the player rotates but the world around him?
Test this code.. I think it is what you want.
Code: Select all
Camera = require "libs/hump/camera"
cam = Camera(0,0,1,0)
function love.load()
love.physics.setMeter(32)
World = love.physics.newWorld(0, 0, true)
boxBody = love.physics.newBody(World, -50, -50, "dynamic")
boxShape = love.physics.newRectangleShape(5, 5)
boxFixture = love.physics.newFixture(boxBody, boxShape, 1)
boxFixture:setDensity(1)
boxFixture:setFriction(1)
boxFixture:setRestitution(0)
playerBody = love.physics.newBody(World, 0, 0, "dynamic")
playerShape = love.physics.newRectangleShape(32, 32)
playerFixture = love.physics.newFixture(playerBody, playerShape, 1)
playerFixture:setDensity(1)
playerFixture:setFriction(1)
playerFixture:setRestitution(0)
end
function love.update(dt)
World:update(dt)
local angle = playerBody:getAngle()
cam:rotateTo(-angle)
end
function love.draw()
cam:attach()
love.graphics.setColor(255, 0, 0, 255)
love.graphics.polygon("fill", playerBody:getWorldPoints(playerBody:getFixtureList()[1]:getShape():getPoints()))
love.graphics.setColor(255, 255, 255, 255)
love.graphics.polygon("fill", boxBody:getWorldPoints(boxBody:getFixtureList()[1]:getShape():getPoints()))
cam:detach()
end
function love.keypressed(key)
if key == "left" then
playerBody:setAngle(playerBody:getAngle() - 0.2 ) -- turn left a bit
end
if key == "right" then
playerBody:setAngle(playerBody:getAngle() + 0.2 ) -- turn right a bit
end
print(playerBody:getAngle())
end
Re: Love2d physics body and hump camera rotation
Posted: Tue Jul 28, 2015 5:51 pm
by frisbro
Yes thank-you very much for your answer and your patience.
- felix
Re: [SOLVED]Love2d physics body and hump camera rotation
Posted: Tue Jul 28, 2015 6:16 pm
by RagingDave
no problem. you always learn yourself if you help somebody