Help with Camera, Mouse, World Coordinates

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
BruceTheGoose
Citizen
Posts: 76
Joined: Sat Sep 20, 2014 2:54 pm

Help with Camera, Mouse, World Coordinates

Post 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.
"I don't know."
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Help with Camera, Mouse, World Coordinates

Post 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.
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
User avatar
BruceTheGoose
Citizen
Posts: 76
Joined: Sat Sep 20, 2014 2:54 pm

Re: Help with Camera, Mouse, World Coordinates

Post 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.
"I don't know."
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Help with Camera, Mouse, World Coordinates

Post 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?
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
User avatar
BruceTheGoose
Citizen
Posts: 76
Joined: Sat Sep 20, 2014 2:54 pm

Re: Help with Camera, Mouse, World Coordinates

Post 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?
"I don't know."
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Help with Camera, Mouse, World Coordinates

Post by kikito »

You may want to give a look at my library, gamera. viewtopic.php?f=5&t=12033
When I write def I mean function.
User avatar
BruceTheGoose
Citizen
Posts: 76
Joined: Sat Sep 20, 2014 2:54 pm

Re: Help with Camera, Mouse, World Coordinates

Post by BruceTheGoose »

kikito wrote:You may want to give a look at my library, gamera. viewtopic.php?f=5&t=12033
How does your library factor in a scale factor?

Nevermind I got it

To translate mouse to world coordinates I did:

Code: Select all

mouseX/cam.sY+cam.y
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
"I don't know."
Post Reply

Who is online

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