Game Development Community

Moving the camera with the mouse

by Matt Brasier · in Torque Game Builder · 04/16/2009 (1:32 pm) · 10 replies

I have a top down game I am developing, and want to be able to scroll the camera by moving the the edges of the screen.

What are my options for doing this? I have a SceneObject as the mouse cursor, so was thinking of setting world limits based on the camera position, and changing them as the camera moved (based on the callback when the object reaches its world limits). However this sounds quite slow and inefficient, so I thought there must be a better way of doing it.

Matt

#1
04/21/2009 (11:43 am)
I came up with the following, which seems hacky to me still
function selectionCursor::onWorldLimit(%this, %limitMode, %limitDescription)
{
   if (%limitDescription $= "LEFT")
   {
      %pos = sceneWindow2D.getCurrentCameraPosition();
      %X = getWord(%pos,0);
      %Y = getWord(%pos,1);
      %X = %X - %this.CameraSpeed;
      %newPos = (%X SPC %Y);
      sceneWindow2D.setCurrentCameraPosition(%newPos);
      %limits = %this.getWorldLimit();
      %Xmin = getWord(%limits,1);
      %Ymin = getWord(%limits,2);
      %Xmax = getWord(%limits,3);
      %Ymax = getWord(%limits,4);
      %Xmin = %Xmin - %this.CameraSpeed;
      %Xmax = %Xmax - %this.CameraSpeed;
      %this.setWorldLimit("NULL",%Xmin,%Ymin,%Xmax,%Ymax,true);
      
   }
   if (%limitDescription $= "RIGHT")
   {
      %pos = sceneWindow2D.getCurrentCameraPosition();
      %X = getWord(%pos,0);
      %Y = getWord(%pos,1);
      %X = %X + %this.CameraSpeed;
      %newPos = (%X SPC %Y);
      sceneWindow2D.setCurrentCameraPosition(%newPos);
      %limits = %this.getWorldLimit();
      %Xmin = getWord(%limits,1);
      %Ymin = getWord(%limits,2);
      %Xmax = getWord(%limits,3);
      %Ymax = getWord(%limits,4);
      %Xmin = %Xmin + %this.CameraSpeed;
      %Xmax = %Xmax + %this.CameraSpeed;
      %this.setWorldLimit("NULL",%Xmin,%Ymin,%Xmax,%Ymax,true);
   }
   if (%limitDescription $= "TOP")
   {
      %pos = sceneWindow2D.getCurrentCameraPosition();
      %X = getWord(%pos,0);
      %Y = getWord(%pos,1);
      %Y = %Y - %this.CameraSpeed;
      %newPos = (%X SPC %Y);
      sceneWindow2D.setCurrentCameraPosition(%newPos);
      %limits = %this.getWorldLimit();
      %Xmin = getWord(%limits,1);
      %Ymin = getWord(%limits,2);
      %Xmax = getWord(%limits,3);
      %Ymax = getWord(%limits,4);
      %Ymin = %Ymin - %this.CameraSpeed;
      %Ymax = %Ymax - %this.CameraSpeed;
      %this.setWorldLimit("NULL",%Xmin,%Ymin,%Xmax,%Ymax,true);
   }
   if (%limitDescription $= "BOTTOM")
   {
      %pos = sceneWindow2D.getCurrentCameraPosition();
      %X = getWord(%pos,0);
      %Y = getWord(%pos,1);
      %Y = %Y + %this.CameraSpeed;
      %newPos = (%X SPC %Y);
      sceneWindow2D.setCurrentCameraPosition(%newPos);
      %limits = %this.getWorldLimit();
      %Xmin = getWord(%limits,1);
      %Ymin = getWord(%limits,2);
      %Xmax = getWord(%limits,3);
      %Ymax = getWord(%limits,4);
      %Ymin = %Ymin + %this.CameraSpeed;
      %Ymax = %Ymax + %this.CameraSpeed;
      %this.setWorldLimit("NULL",%Xmin,%Ymin,%Xmax,%Ymax,true);
   }
}

Which lets me scroll. I will tidy it up later. I now need to stop the camera scrolling off the edge of the tilemap that forms the map. Is there an easy way of doing this? or do I name the tilemap and then calculate the co-ordinates of the edges and put something in the above function to deal with them?
#2
04/21/2009 (7:52 pm)
have u got a working example of this?
#3
04/22/2009 (12:56 am)
Of moving the camera with the mouse? Yes, but it allows you to scroll infinitely in all directions at the moment.

I downloaded the MouseSelector behaviour from TDN, and created a simple selection cursor graphic. I then imported this into TGB, created an instance of the sprite, gave it the MouseSelector behaviour (and a selection box graphic, which I also created) and the name selectionCursor. I also gave it an attribute CameraSpeed (set to 1).

I defined world limits for the selectionCursor object of just inside the camera box using TGB.

The above script is in a file called mouse.cs, which I exec from game.cs, although i could have put it in the MouseSelection behaviour script.

If you need more detail I can put together a step-by-step tutorial, although it will have to wait until the weekend, as I am away from my TGB machine for a few days.
#4
04/22/2009 (12:57 am)
Oh yea, you set the world limit type to NULL and enable world limit callbacks for the selectionCursor object.
#5
04/22/2009 (1:09 am)
i have an idea of how i might be able to stop it indefinetly scrolling gimmie a few ticks
#6
04/22/2009 (1:22 am)
hmm couldnt get its to work from your small refrence however
#7
04/22/2009 (1:24 am)
I will put together step-by-step instructions at the weekend.
#8
04/26/2009 (2:18 pm)
I put together a tutorial for mouse scrolling with TGB games here:
www.xp-dev.com/wiki/9737/MouseScrolling

If anyone has any ideas how to set limits for its scrolling based on the size of a tilemap then let me know, otherwise I will work it out when I get some time.

Matt
#9
03/28/2010 (2:29 pm)
I'm not seeing the Mouse Selector behavior on TDN. Did the name change? Or can somebody post its code in the forums? Thank you.
#10
03/29/2010 (7:01 am)
I think it's called MakeSelectable now.