Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

My game is a classic RTS where the user can move around the landscape by moving the mouse to the edge of the screen or using keys. Eventually I'd want to think about multi-touch alternatives too.

Is there a library for this, or a simple tutorial that shows clearly how best to do it?

share|improve this question
2  
I seriously doubt it. This is such simple code (assuming you can arbitrarily position the screen) that nobody bothered to make an actual library for it. You detect that the mouse's X/Y position is close to the side (however you want to define "close") and move the screen accordingly. Also, RTS's on touch screens aren't going to work the same way as for a mouse, so you will have to develop entirely different gameplay to make them work. –  Nicol Bolas Nov 26 '11 at 1:49
1  
Integration of any 'library code' would probably be more complex than the code itself. –  stephelton Nov 26 '11 at 20:23
    
Having written the movement code several times now it occurs to me that actually good navigation code with bells and whistles like spinning around when drive off the map and scaling back so you don't fly through high buildings and such is not as small as you'd like, but it is generic enough to be a useful library. –  Will Nov 27 '11 at 7:50
add comment

1 Answer

up vote 3 down vote accepted

I don't think there is a library, but as for how to do it, it depends on whether the game is 3d or 2d. For 3d, you should check if the mouse position is touching one of the edges of the screen and then change the position of the camera, or world matrix if you dont use one. Something like this:

public int tolerance = 1;
public int speed = 5;
private Camera camera;

public void CheckCameraMoves(int mouseX, int mouseY, int screenWidth, int screenHeight)
{
    if (mouseX < tolerance)
    {
        camera.X -= speed;
    }
    else if (mouseX > screenWidth - tolerance)
    {
        camera.X += speed;
    }
    if (mouseY < tolerance)
    {
        camera.Y -= speed;
    }
    else if (mouseY > screenHeight - tolerance)
    {
        camera.Y += speed;
    }
}

You could also make it nicer looking by smoothing the speed out like this:

    if (mouseX < tolerance)
    {
        camera.X -= (int)(speed * (tolerance-mouseX)/tolerance);
    }
    else if (mouseX > screenWidth - tolerance)
    {
        camera.X += (int)(speed * (mouseX-screenWidth+tolerance)/tolerance);
    }
    if (mouseY < tolerance)
    {
        camera.Y -= (int)(speed * (tolerance-mouseY)/tolerance);
    }
    else if (mouseY > screenHeight - tolerance)
    {
        (int)(speed * (mouseX-screenWidth+tolerance)/tolerance);
    }

This way as the mouse gets closer to the edge of the screen it starts to accelerate towards the speed given.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.