weird behavior with hump camera and player 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
snillor
Prole
Posts: 1
Joined: Wed Oct 30, 2019 6:30 pm

weird behavior with hump camera and player rotation

Post by snillor »

Hey there new to the love2d scene. What I am trying to do is make the camera follow the player sprite based off the x and y values of player.body. What ends up happening with the code I've written below is the camera does indeed follow the players sprite. However. There is some weird behavior when the sprite rotates. the sprite no longer rotates according to the location of the mouse. Once the camera is implemented it tends to through off the player rotation for some reason. I don't need the camera to rotate to the players sprite, I just need it to follow the player based off movement.

I've attached a screen shot to get a better reference to whats going on. the white dot represents the mouse...
thank you guys!


Image


Code: Select all

function love.load()
  world = love.physics.newWorld(0, 0, false)
  sprites = {}
    sprites.player1 = love.graphics.newImage("sprites/Soldier1/soldier1_hold.png")
    sprites.player2 = love.graphics.newImage("sprites/Soldier1/soldier1_gun.png")
    sprites.weapon1 = love.graphics.newImage("sprites/weapon_gun.png")

  require("player")
  cameraFile = require('library/hump-master/camera')

  cam = cameraFile()

  bullets = {}
end


function love.update(dt)
    for i, b in ipairs(bullets) do
      b.x = b.x + math.cos(b.direction) * b.speed * dt
      b.y = b.y + math.sin(b.direction) * b.speed * dt
  end

  cam:lookAt(player.body:getX(), player.body:getY())

  world:update(dt)
  playerUpdate(dt)
end


function love.draw()
cam:attach()
  if player.hasWeapon == false then
    love.graphics.draw(sprites.player1, player.body:getX(), player.body:getY(), playerRotation(), nil, nil, sprites.player1:getWidth()/2, sprites.player1:getHeight()/2)
  elseif player.hasWeapon == true then
    love.graphics.draw(sprites.player2, player.body:getX(), player.body:getY(), playerRotation(), nil, nil, sprites.player2:getWidth()/2, sprites.player2:getHeight()/2)
  end

  for i, b in ipairs(bullets) do
    love.graphics.draw(sprites.weapon1, b.x, b.y, nil, 0.25, 0.5, sprites.weapon1:getWidth()/2, sprites.weapon1:getHeight()/2)
  end
cam:detach()
end


function spawnBullet()
  bullet = {}
    bullet.x = player.body:getX()
    bullet.y = player.body:getY()
    bullet.direction = playerRotation()
    bullet.speed = 500
    bullet.dead = false

  table.insert(bullets, bullet)
end


function playerRotation()
  return math.atan2(player.body:getY() - love.mouse.getY(), player.body:getX() - love.mouse.getX()) + math.pi
end
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: weird behavior with hump camera and player rotation

Post by pgimeno »

Hi snillor, welcome to the forums.
snillor wrote: Wed Oct 30, 2019 6:55 pm Hey there new to the love2d scene. What I am trying to do is make the camera follow the player sprite based off the x and y values of player.body. What ends up happening with the code I've written below is the camera does indeed follow the players sprite. However. There is some weird behavior when the sprite rotates. the sprite no longer rotates according to the location of the mouse. Once the camera is implemented it tends to through off the player rotation for some reason. I don't need the camera to rotate to the players sprite, I just need it to follow the player based off movement.
I don't understand your problem very well, but it seems to have to do with coordinate systems.

The mouse coordinates are always in the screen coordinate system. The player coordinates are typically in the world coordinate system. You need to convert one to the other before subtracting them: either convert the player to screen coordinates, or the mouse to world coordinates. The former is easier, because the player is typically at the centre of the screen, so the coordinate is where the camera centre points to. The latter is more powerful because it's also useful for e.g. detecting clicks on world objects.

Hump has camera:cameraCoords and camera:worldCoords to perform this conversion.

Other than that, you will have to provide a complete runnable example if you want help.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests