[SOLVED]Love2d physics body and hump camera rotation

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.
Post Reply
frisbro
Prole
Posts: 5
Joined: Tue Jul 28, 2015 11:25 am

[SOLVED]Love2d physics body and hump camera rotation

Post 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
Last edited by frisbro on Tue Jul 28, 2015 5:53 pm, edited 1 time in total.
User avatar
RagingDave
Prole
Posts: 23
Joined: Mon Jul 27, 2015 3:12 pm

Re: Love2d physics body and hump camera rotation

Post 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.
frisbro
Prole
Posts: 5
Joined: Tue Jul 28, 2015 11:25 am

Re: Love2d physics body and hump camera rotation

Post by frisbro »

I'm just creating a rectangle and then i try to rotate the hump camera :/
User avatar
RagingDave
Prole
Posts: 23
Joined: Mon Jul 27, 2015 3:12 pm

Re: Love2d physics body and hump camera rotation

Post 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
frisbro
Prole
Posts: 5
Joined: Tue Jul 28, 2015 11:25 am

Re: Love2d physics body and hump camera rotation

Post 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

User avatar
RagingDave
Prole
Posts: 23
Joined: Mon Jul 27, 2015 3:12 pm

Re: Love2d physics body and hump camera rotation

Post 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
frisbro
Prole
Posts: 5
Joined: Tue Jul 28, 2015 11:25 am

Re: Love2d physics body and hump camera rotation

Post by frisbro »

The problem is that the camera doesen't rotate to the body rotation :/
User avatar
RagingDave
Prole
Posts: 23
Joined: Mon Jul 27, 2015 3:12 pm

Re: Love2d physics body and hump camera rotation

Post 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
frisbro
Prole
Posts: 5
Joined: Tue Jul 28, 2015 11:25 am

Re: Love2d physics body and hump camera rotation

Post by frisbro »

Yes thank-you very much for your answer and your patience.
- felix
User avatar
RagingDave
Prole
Posts: 23
Joined: Mon Jul 27, 2015 3:12 pm

Re: [SOLVED]Love2d physics body and hump camera rotation

Post by RagingDave »

no problem. you always learn yourself if you help somebody :)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 60 guests