Page 1 of 1
Help with Camera, Mouse, World Coordinates
Posted: Sun Feb 21, 2016 7:26 am
by BruceTheGoose
So what I'm trying to achieve is getting the mouse's coordinates translating them to world coordinates and make the player follow the mouse to world coordinates. However, my player is stuck...
This is PSEUDOCODE
This is how I'm converting mouse to world coordinates:
Code: Select all
worldX = mouseX * cam.scale + player.x
This does get the world coordinate but my player fails to move towards the mouse...
Code: Select all
dx = (goalX - player.x);
dy = (goalY - player.y);
player.x += (dx) * 1/50;
player.y += (dy) * 1/50;
Where goalX is worldX.
I print out both the worldX coordinates and mouseX coordinates and they equal the same even though the player is not following the mouse...
I also have a camera class and the basic code for the camera is:
scale(1,1);
Code: Select all
cam.x = ((-player.x + player.mass * 0.5) + Game.width/sX * 0.5)* sX;
cam.y = ((-player.y + player.mass * 0.5) + Game.height/sY * 0.5)* sY;
Where sX is the scale factor.
And to finish it off I have to methods that actually translate the screen based on the camera's coordinates
Code: Select all
g2.translate(x,y);
g2.scale(sX,sY);
-- Draw everything in the world
g2.translate(-x, -y);
This camera works well to center the player on the screen and translating everything relative to the player. But I do not know if it interrupts with my mouse to world coordinates.
Re: Help with Camera, Mouse, World Coordinates
Posted: Sun Feb 21, 2016 11:58 am
by Davidobot
The code to transform the mouse position should not be
Code: Select all
worldX = mouseX * cam.scale + player.x
But instead should be
Code: Select all
worldX = mouseX * cam.scale + cam.x
cam.x is basically the value by which the camera translates the screen, as shown in your code.
Re: Help with Camera, Mouse, World Coordinates
Posted: Sun Feb 21, 2016 3:20 pm
by BruceTheGoose
Changing player.x to cam.x just makes my player stuck within the boundaries of the frame. The player does not move towards the mouse and I still get an output that the mouseX is equal to the player x even though they are not the same position.
Re: Help with Camera, Mouse, World Coordinates
Posted: Sun Feb 21, 2016 3:41 pm
by Davidobot
BruceTheGoose wrote:Changing player.x to cam.x just makes my player stuck within the boundaries of the frame. The player does not move towards the mouse and I still get an output that the mouseX is equal to the player x even though they are not the same position.
I'm not sure what the problem is. Check out this old camera library I wrote some time ago:
Code: Select all
camera = {}
camera._x = 0
camera._y = 0
camera.scaleX = 1
camera.scaleY = 1
camera.rotation = 0
function camera:set()
love.graphics.push()
love.graphics.rotate(-self.rotation)
love.graphics.scale(1 / self.scaleX, 1 / self.scaleY)
love.graphics.translate(-self._x, -self._y)
end
function camera:unset()
love.graphics.pop()
end
function camera:move(dx, dy)
self._x = self._x + (dx or 0)
self._y = self._y + (dy or 0)
end
function camera:rotate(dr)
self.rotation = self.rotation + dr
end
function camera:scale(sx, sy)
sx = sx or 1
self.scaleX = self.scaleX * sx
self.scaleY = self.scaleY * (sy or sx)
end
function camera:setX(value)
if self._bounds then
self._x = math.clamp(value, self._bounds.x1, self._bounds.x2)
else
self._x = value
end
end
function camera:setY(value)
if self._bounds then
self._y = math.clamp(value, self._bounds.y1, self._bounds.y2)
else
self._y = value
end
end
function camera:setPosition(x, y)
if x then self:setX(x) end
if y then self:setY(y) end
end
function camera:setScale(sx, sy)
self.scaleX = sx or self.scaleX
self.scaleY = sy or self.scaleY
end
function camera:getBounds()
return unpack(self._bounds)
end
function camera:setBounds(x1, y1, x2, y2)
self._bounds = { x1 = x1, y1 = y1, x2 = x2, y2 = y2 }
end
function camera:mousePosition()
return love.mouse.getX() * self.scaleX + self._x, love.mouse.getY() * self.scaleY + self._y
end
function camera:RePositionX( co_ord_x )
return co_ord_x * self.scaleX + self._x
end
function camera:RePositionY( co_ord_y )
return co_ord_y * self.scaleY + self._y
end
function math.clamp(x, min, max)
return x < min and min or (x > max and max or x)
end
It's usage would be calling camera:set before drawing anything and camera:unset when you want to draw "outside" the camera.
You can set the position of the top left with camera:setPosition as well as bounds and all.
camera:mousePosition returns the relative mouse values and the RePosition functions reposition individual co-ordinates.
You can read through it and see if it helps?
Re: Help with Camera, Mouse, World Coordinates
Posted: Sun Feb 21, 2016 4:11 pm
by BruceTheGoose
Code: Select all
x =((-player.x + cell.mass * 0.5) + Game.width*sX * 0.5);
That line is the culprit. The player does follow the mouse's coordinates I just don't know how to make the camera follow the player. Any idea?
This is the fixed line:
Code: Select all
((cell.x + cell.mass * 0.5) - Game.width * 0.5);
How do I factor in a scale factor?
Re: Help with Camera, Mouse, World Coordinates
Posted: Sun Feb 21, 2016 4:34 pm
by kikito
You may want to give a look at my library, gamera.
viewtopic.php?f=5&t=12033
Re: Help with Camera, Mouse, World Coordinates
Posted: Sun Feb 21, 2016 4:36 pm
by BruceTheGoose
How does your library factor in a scale factor?
Nevermind I got it
To translate mouse to world coordinates I did:
And for the camera to follow the player I did:
Code: Select all
x =((player.x + player.width * 0.5) - Game.width/sX * 0.5);
Thank you guys for your time