Page 1 of 1

RTS style game, clicking with translate camara movement

Posted: Sat Jun 22, 2024 12:15 pm
by A_Fast_Cat
Hi all :3 ,

I come from a beginner python background and recently picked up Lua. Im not the best but I want to keep going.

I've so far being using forums on here and sheepolutions guide to understand.

I am trying to make RTS like camera. My problem is where I click, the user does not follow.

This is because of love.graphics.translate. I saw I must reverse the translation but its been a few days and I am really stuck :/

I have found a way to track the offset by measuring the distance of the camera currently from where it started. My problem is changing the click x,y to actually go there. I think it is as simple as adding this offset number to mouse.x, mouse.y but idk... Without the translate it does move to where I want it.

Any help would be awesome! The code is in one file for the sake of learning. I plan to recode it later into seperate files when it works!!

Currently there is a user and a camera. The translation follows the camara. If you press Y the camera will follow the user. If the camera is not following the user and the mouse is within the white boundary area, the camera will move to the mouse. If you are familiar with league of legends, I am trying to make an unlocked camera...

My current goal would be to have a large playable map where the user can move around. I am hoping the playable area can be roughly 3000*3000 but first I must make the movement work....

Press F9 to let your cursor be free of the window.

Please let me know if I can provide any further info :))

Re: RTS style game, clicking with translate camara movement

Posted: Sat Jun 22, 2024 4:23 pm
by dusoft
Maybe try existing lib like: https://github.com/a327ex/STALKER-X (or study the code)

Re: RTS style game, clicking with translate camara movement

Posted: Sun Jun 23, 2024 2:52 am
by RNavega
A_Fast_Cat wrote: Sat Jun 22, 2024 12:15 pm I have found a way to track the offset by measuring the distance of the camera currently from where it started. My problem is changing the click x,y to actually go there. I think it is as simple as adding this offset number to mouse.x, mouse.y but idk... Without the translate it does move to where I want it
I also think you should try to solve this without using love.graphics.translate(), as it's like a global transform on all drawing commands, if you ever use it even once then every coordinate you use needs to take it into consideration, at least until you reset the global graphics transform with love.graphics.origin().

But anyway, I think you're on the right track, you need two mapping functions: one that maps screen-pixel to game-world-pixel, and another that does the reverse. When the camera hasn't moved, the screen pixels map directly to (some of) the game world pixels.
When you offset the camera, then a certain screen pixel points to the game world pixel being 'viewed' by that offset camera, so it's (screenPixelX + cameraX) as you've predicted.
This only works at a 100% scale though. If you want your RTS camera to be able to zoom in or out, then those pixel coordinate mapping functions will need to incorporate scale into the calculations -- a zoomed out camera is a large camera that sees a lot of the map, so the offset to be added to the screen pixel was both translated and scaled by the camera.
You might find Transform objects from love.math helpful with this. In fact, as I recall they have a mapping function already: https://love2d.org/wiki/Transform:transformPoint

PS this thread is not about the same thing, but might be interesting to read anyway: https://love2d.org/forums/viewtopic.php?t=90109

Re: RTS style game, clicking with translate camara movement

Posted: Sun Jun 23, 2024 9:07 am
by pgimeno
Using Transform objects can be very useful indeed, because they provide two key functions: transformPoint and inverseTransformPoint. Indeed my cam11 library is entirely based on a Transform object.

This thread may be useful in understanding how to use Transform objects and the transformPoint and inverseTransformPoint functions: https://love2d.org/forums/viewtopic.php?f=4&t=89704

Re: RTS style game, clicking with translate camara movement

Posted: Sun Jun 23, 2024 9:24 am
by dusoft
pgimeno wrote: Sun Jun 23, 2024 9:07 am Using Transform objects can be very useful indeed, because they provide two key functions: transformPoint and inverseTransformPoint. Indeed my cam11 library is entirely based on a Transform object.

This thread may be useful in understanding how to use Transform objects and the transformPoint and inverseTransformPoint functions: https://love2d.org/forums/viewtopic.php?f=4&t=89704
This is good, I haven't known about these Transform objects as when I investigated this it was well before v 11. 0 release.

Re: RTS style game, clicking with translate camara movement

Posted: Tue Jun 25, 2024 12:38 pm
by A_Fast_Cat
Thank you very much for the help! I have taken on the suggestions. Unfortunetly, looking through the CAM11 library is above my skill level but I believe I understand how transform works and I will utilise the object to give it another go. My new issue is when i move the camera the user also moves....

I think I will need 2 transform objects to fix this. Notheless this is only a basic game. I hope the idea can inpsire someone better than me haha!

I will do what I can and update

Re: RTS style game, clicking with translate camara movement

Posted: Mon Aug 12, 2024 4:20 pm
by A_Fast_Cat
Phew its been some time...

I decided to just get it working. In the end i used the stlker-x camera as it had the best documentation (and im still a noob)

Anyways heres the league of legends type camera demo thing. Hopefully helps anyone trying to make an RTS

The camera is seperate from the player movement. Where you click a blue circle will apear and the white user (ik what it sounds like but no) will move there

white is the user, red is the camera, green is some background terrain, blue lines are the orgional screen borders and white lines are for unlocked cam movement

y is to lock and unlock cam. Whislt unlocked if you move the mouse past the white border the camera will follow
F9 to leave the window

Think thats everything...