Problem with movement with rotating camera

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
murcelago
Prole
Posts: 2
Joined: Thu May 14, 2015 2:50 pm

Problem with movement with rotating camera

Post by murcelago »

Hi guys!

I'm starting to learn this wonderful physics engine, and just while I was working on my epic game project, some problem appeared and I can't solve it =(

So, here we have 2 bodies: "player" and "ball". Camera(I use kikito's "gamera", it's awesome btw) is fixed on "player"(angle and coordinates). Also "player" is being gravitated to "ball" and faces opposite direction of where "ball" is. "Player"'s movement can be controlled via WASD.

Problem is when I start to move player around, camera rotates and forces that control player start to act crazy changing their direction, no longer resembling gravity. I tried to use "cam:toWorld()" function to transform coordinates of "player" and "ball", but it didn't help. I think that maybe angles need some transformation too, but i'm too bad at geometry to figure it out :?

Help me please! What formulas should i use for this system to work correctly?

Code: Select all

function love.load()
    local gamera = require 'gamera'
    cam = gamera.new(-20000,-20000,40000,40000)

	artgrav = 40
 	
    world = love.physics.newWorld(0, 0, true)
    world:setCallbacks(beginContact, endContact, preSolve, postSolve)
    
    player = {}
    player.body=love.physics.newBody(world, 0, 0, "dynamic")
    player.body:setMass(10)
    player.width=10
    player.height=30
    player.shape=love.physics.newRectangleShape(player.width,player.height)
    player.shape2=love.physics.newRectangleShape(0,-10,5,5)
    player.fix=love.physics.newFixture(player.body, player.shape) 
    player.fix2=love.physics.newFixture(player.body, player.shape2) 
    player.fix:setUserData("Player")
    player.body:setFixedRotation(true)
    player.speed=2
    
   
    ball = {}
        ball.body = love.physics.newBody(world, mousex, mousey, "dynamic") 
        ball.shape = love.physics.newCircleShape(50) 
        ball.shape2=love.physics.newRectangleShape(0,-30,10,10)
        ball.fix = love.physics.newFixture(ball.body, ball.shape) 
        ball.fix2 = love.physics.newFixture(ball.body, ball.shape2) 
        ball.fix:setUserData("Ball")

end
 
function love.update(dt)
world:update(dt)
 
cam:setPosition(player.body:getPosition())
cam:setAngle(player.body:getAngle())

player.dx=-(player.body:getX() -ball.body:getX())
player.dy=-(player.body:getY() -ball.body:getY())
player.ang = math.atan2(player.dx,player.dy)

player.body:applyForce((artgrav*(math.sin(player.ang))),(artgrav*(math.cos(player.ang))))

player.body:setAngle(-player.ang)


    -- wasd controls
if love.keyboard.isDown("d") then
        player.body:applyLinearImpulse(-player.speed*math.cos(cam:getAngle()-math.pi), -player.speed*math.sin(cam:getAngle()-math.pi))
    elseif love.keyboard.isDown("a") then
        player.body:applyLinearImpulse(-player.speed*math.cos(cam:getAngle()), -player.speed*math.sin(cam:getAngle()))
    end
    if love.keyboard.isDown("w") then
        player.body:applyLinearImpulse(-player.speed*math.cos(cam:getAngle()+math.pi/2), -player.speed*math.sin(cam:getAngle()+math.pi/2))
    elseif love.keyboard.isDown("s") then
        player.body:applyLinearImpulse(-player.speed*math.cos(cam:getAngle()-math.pi/2), -player.speed*math.sin(cam:getAngle()-math.pi/2))
end

   
end

function love.draw()

cam:draw(function(l,t,h,w)

    love.graphics.setColor(255,255,255,255)
    love.graphics.polygon("line", player.body:getWorldPoints(player.shape:getPoints()))
    love.graphics.polygon("line", player.body:getWorldPoints(player.shape2:getPoints()))

       
    love.graphics.setColor(100,105,255,255)
    love.graphics.circle("line", ball.body:getX(), ball.body:getY(),ball.shape:getRadius())
    love.graphics.polygon("line", ball.body:getWorldPoints(ball.shape2:getPoints()))

    love.graphics.setColor(0,105,0,255)
    love.graphics.line(player.body:getX(),player.body:getY(),ball.body:getX(),ball.body:getY())

end)
end
 
murcelago
Prole
Posts: 2
Joined: Thu May 14, 2015 2:50 pm

Re: Problem with movement with rotating camera

Post by murcelago »

Update:
All this week long i've been ******* my brain out but still didn't manage to fix it =(
Nonetheless, i've some thoughts to share on the case:
-- the source of problem seems to be in Coriolis effect: a deflection of moving objects when the motion is described relative to a rotating reference frame.
-- now the problem is how to counter it: maybe apply force in opposite to deflection direction? But how can i find that direction?
-- maybe polar coordinates would do the trick? But how should i transform box2d physics into polar?

Any ideas please?
Did anyone else face similar problem with rotating camera?
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 12 guests