[Solved] Scrolling map when touching edge with mouse (RTS project)

Hey all!

I very recently started working with jME3. About a year ago, I experimented quite a bit with jME2 and was planning on making my own RTS from scratch, but then jME3 came out and (along with being very busy IRL), I decided to stop working on it.

Now that jME3 is quite stable (and I got some time on my hands again :stuck_out_tongue: ), I decided to make the move to jME3.

I found that it is in many ways an improvement on jME2, and with the jMonkeyPlatform I find it very suitable.

Anyway, on to business: I have a small problem. I am trying to make the whole look and feel of a real RTS, and although I managed to accomplish quite a bit on that so far, there is one thing that I just canā€™t find: the edge of the screen.

When working in fullscreen mode, it would be possible for me to detect when the user is at the edge of the screen and roll the map to that side. However, when working in a window, my mouse simply leaves the canvas. I would like to detect when it is at the edge, and also prevent it from leaving the canvas.

I am quite experienced with java and although I donā€™t have that much knowledge concerning jME, I can take care of myself. All I need is a hint or a few lines of code on how to find the edge and stop my mouse from leaving the scene.

Thanks in advance :slight_smile:

Itā€™s a cool subject I think :stuck_out_tongue: , let look into the detail :

I canā€™t give you a real-code but let see how the work done in abstract :



You want you mouse find the edge of the scene , and then by the push of user to the edge , find the speed of scrolling out of current position in the map , if itā€™s true so itā€™s easy to do as a few line of code , more code for tweaking of course!



(I have do the same thing for my old Flash game :p)




  • First, hide your mouse , make a cool icon instead (a jme Picture), then ask the inputManager for the 2d position of mouse and move the Picture.
  • If it near the edge (depend on your criteria , may relate to the UI border too) for example


  • if its position in range of

    (X:0 ->10) :

    scroll left

    (X:maxX -10 ā†’ maxX) :

    scroll right

    (Y:0 ->10) :

    scroll top

    (Y:maxY -10 ā†’ maxY) :

    scroll bottom



    The amount of scrolling is depend on you , ā† implements AnalogListener and in a AnalogEvent you get the ā€œdelta of the mouse motionā€ , so one unit of this you scroll your map (move your camera say ten ) ā€¦



    The scrolling can get faster and faster if in the ā€œcheck in rangeā€ you add an acceleration increased every sec the push to the edgeā€¦



    :stuck_out_tongue: Itā€™s very easy, but further work is to find the edge of your Map, so your view can scroll any further.
1 Like

Your best bet I think is to actually have the mouse hidden, and then replace it with a GUI element that you can control. You can get the delta of the mouse motion through input events, either RawInputListener or AnalogListener, and then move the GUI element appropriately.

When I say GUI element, it could either be a Nifty GUI element, or a jME3 ā€œPictureā€ or some other object that is rendered in ortho mode.

1 Like

Thanks both for your replies, I understand what you mean and Iā€™m sure Iā€™ll be able to work this out on myself now :slight_smile:

Now I can even customize my mouse look when itā€™s at the edge (arrow left when at the left edge etc.).



Cheers!



PS: If Iā€™m finished with this thing, I will probably release the source code incase other RTS-devs wish to use it :wink:

@insdeath: Good to hear that man , RTS is a good choice for a start in JME game ā€¦ waiting for your game in action !