Log In  


How do you get the mouse x and y based on world space ?

P#114570 2022-07-20 12:41

Without more context, I guess you just keep track of the camera position and add it to the mouse position that the engine says.

So, mouse x in world space would be mouse x in screen space plus the x position of the camera. You just have to store the x and y position of the camera whenever you set it.

Edit: whoops, I put "subtract" at first, because I was working with sprites right before answering, and thus doing the opposite..

P#114572 2022-07-20 13:40 ( Edited 2022-07-20 13:42)

@kimoyoribaka can you give a code example?

P#114575 2022-07-20 14:38

Sure. Here's minimal code to show a dot for the cursor while outputting a manual camera position that moves when the cursor is near an edge of the screen, as well as the two versions of the mouse position. I also put in a use of the built-in camera() function to show how to still use that even despite keeping the manual version around. It's really up to preference whether to use that function though.

function _init()
  cam_x, cam_y = 0, 0

function _update()
  mx, my = stat(32), stat(33)
  if (mx > 126) then cam_x = cam_x + 2 end
  if (mx < 2) then cam_x = cam_x - 2 end
  if (my > 126) then cam_y = cam_y + 2 end
  if (my < 2) then cam_y = cam_y - 2 end
  camera(cam_x, cam_y)

function _draw()
  print("raw mouse: "..mx..","..my)
  print("camera pos:"..cam_x..","..cam_y)
  print("in-game:   "..mx+cam_x..","..my+cam_y)
P#114576 2022-07-20 15:08

Wow i thought it was harder , thanks dude

P#114577 2022-07-20 15:16

[Please log in to post a comment]