RTS drag camera style
-
Hi guys, im new here, so, hi! :)
i have a problem with the camera RTS, i know that there is a post with pseudo code, but i don't get the point.
I set the initial variables for the camera position and mouse position when the right button is just pressed, and that's ok i think.
The problem is that i calculate then the distance from the mouse to the camera, to keep that offset (when the user press the right button and hold it down), but when i drag, it jump from 0,0 to 3000 something.. lol.. somebody can tell me how can i solve this?
thank you.
-
Guys i solve it!, btw thank you! here the code if someone need it.
if(FlxG.mouse.justPressedRight){ initialMousePos = new FlxPoint(FlxG.mouse.x, FlxG.mouse.y); initialCamPos = new FlxPoint(camera.scroll.x, camera.scroll.y); trace("Mouse just pressed: " + initialMousePos); trace("Cam just pressed: " + initialCamPos); } if(FlxG.mouse.pressedRight){ newMousePos = new FlxPoint(FlxG.mouse.x, FlxG.mouse.y); mousePosChanged = newMousePos.subtract(initialMousePos.x, initialMousePos.y); camera.scroll.copyFrom(initialCamPos.subtract(mousePosChanged.x, mousePosChanged.y)); }
-
the only thing that im afraid is, im creating new flxPoint every time i press the button, is better to declare it on top and in the update only set it right?
-
Yes, or using FlxPoint.get() instead of new(), and then put() when you don't need the point.
-
@starry-abyss ok, Thank you mate, i didn't Know about those methods.
-
Hi guys, so i'm in love with haxeflixel and how everything works, and thank you for your suggestions, now i have my box selection and my camera that works! the only thing that i found is that setting my bounds if i drag over the map width, height or under 0 x and 0 y, my camera scroll and then snap back to world bounds. i know that i can check the position of the scroll and set it back to 0 or map size, and could work, but because im using the zoom, if i zoom in that case it cut off a bit of the map and don't scroll to the bounds.
so, i have this class
package; import flixel.FlxCamera; import flixel.math.FlxPoint; import flixel.FlxSprite; import flixel.FlxG; import flixel.util.FlxColor; import flixel.math.FlxMath; class RtsCamera extends FlxCamera { private var initialMousePos:FlxPoint; private var initialCamPos:FlxPoint; private var newMousePos:FlxPoint; public var zoomSpeed:Float = 5; public function new(x:Int,y:Int,w:Int,h:Int,z:Float) { super(x,y,w,h,z); initialMousePos = new FlxPoint(); initialCamPos = new FlxPoint(); newMousePos = new FlxPoint(); //setScrollBounds(0,GlobalVariables.WORLD_WIDTH * GlobalVariables.TILE_WIDTH, 0, GlobalVariables.WORLD_HEIGHT * GlobalVariables.TILE_HEIGHT); setScrollBoundsRect(0,0,GlobalVariables.WORLD_WIDTH * GlobalVariables.TILE_WIDTH, GlobalVariables.WORLD_HEIGHT * GlobalVariables.TILE_HEIGHT, true); } override public function update(elapsed:Float):Void { super.update(elapsed); if(FlxG.keys.pressed.ONE){ // Update camera zoom zoom += zoom / 2 * elapsed * zoomSpeed; } if(FlxG.keys.pressed.TWO){ // Update camera zoom zoom -= zoom / 2 * elapsed * zoomSpeed; } if(FlxG.mouse.justPressedRight){ initialMousePos.copyFrom(FlxG.mouse.getWorldPosition()); initialCamPos.set(camera.scroll.x,camera.scroll.y); } if(FlxG.mouse.pressedRight){ newMousePos.copyFrom(FlxG.mouse.getWorldPosition()); var mousePosChanged = newMousePos.subtract(initialMousePos.x, initialMousePos.y); camera.scroll.copyFrom(initialCamPos.subtract(mousePosChanged.x, mousePosChanged.y)); } } override public function destroy():Void { super.destroy(); } }
how can i lock the camera in a better way to the bounds?
thanks.
-
Guys, i solve it, i forgot to call updateScroll()
btw, thank you,
here the code
package; import flixel.FlxCamera; import flixel.math.FlxPoint; import flixel.FlxSprite; import flixel.FlxG; import flixel.util.FlxColor; import flixel.math.FlxMath; class RtsCamera extends FlxCamera { //Points to handle the drag private var initialMousePos:FlxPoint; private var initialCamPos:FlxPoint; private var newMousePos:FlxPoint; //World width in pixel private var worldWidth:Int; //World height in pixel private var worldHeight:Int; //Max zoom of the camera; private var maxZoom:Int; public function new(x:Int,y:Int, startingZoom:Int, maxZoom:Int,mw:Int,mh:Int) { super(x,y,FlxG.width,FlxG.height,1); //Set the starting zoom zoom = startingZoom; //store size of the world for the bounds worldWidth = mw; worldHeight = mh; //Set the max zoom of the camera this.maxZoom = maxZoom; //Init points to handle drag initialMousePos = new FlxPoint(); initialCamPos = new FlxPoint(); newMousePos = new FlxPoint(); //Set the bounds of the camera setScrollBoundsRect(0,0,worldWidth,worldHeight, true); } override public function update(elapsed:Float):Void { super.update(elapsed); //If right mouse button is pressed the first time if(FlxG.mouse.justPressedRight){ //Set initial mouse and cam position relative to the world initialMousePos.copyFrom(FlxG.mouse.getWorldPosition()); initialCamPos.set(scroll.x,scroll.y); } //If mouse still pressed if(FlxG.mouse.pressedRight){ //Get the mouse position relative to the world newMousePos.copyFrom(FlxG.mouse.getWorldPosition()); //Find how much the mouse moved and update it's position var mousePosChanged = newMousePos.subtract(initialMousePos.x, initialMousePos.y); //Change the position of the camera scroll.copyFrom(initialCamPos.subtract(mousePosChanged.x, mousePosChanged.y)); //Update the initial mouse position to the new position relative to the world initialMousePos.copyFrom(FlxG.mouse.getWorldPosition()); } //If i'm scrolling if (FlxG.mouse.wheel != 0) { //zoom in or out zoom += (FlxG.mouse.wheel / 10); } //Clamp the zoom zoom = clamp(zoom, 1, maxZoom); //Update the camera to clamp it on world bounds based on the zoom updateScroll(); } //function to destroy the object override public function destroy():Void { super.destroy(); } //function to clamp a value private function clamp(value:Float, min:Float, max:Float):Float { if(value < min) return min; if(value > max) return max; return value; } }