[SOLVED] Trying to snap an object to the screen coordinates

Started by Icey, Mon 03/01/2022 09:55:11

Previous topic - Next topic

Icey

Heya guys, I have a pretty specif thing that I'm trying to do with two objects that I can't seem to wrap my head around doing.

I have an object that acts as an overlay sunlight that sits atop everything on the screen, and sorta another that sits beneath an island on the water surface that gives the illusion of sunlight resting atop the water. In the repeatedly execute, I want to have both the objects always snap to the screen X and Y of (0 & 559). This way whenever the player moves around the screen sunlight/surface light will always be fixed in the same spot.

My problem lies in the fact that I actually can't figure out the correct way to apply this through code?

Code: ags


function room_RepExec()
{
  
//Originally I had this, but then realized it would only snap to the room x,y
Water_light.X = 0
Water_light.Y = 559;
sunlight.X = 0;
sunlight.Y = 559;

//Then I tried this before realizing it can only get, not set
Water_light = Screen.Viewport.X = 0;
 Water_light.Y = Screen.Viewport.Y = 559;

//Now I'm at a loss, as I can't figure out anything else that could help at the moment.


}



Hopefully this something that's actually possible to do? I don't think I've had to work with the screen functions before, so there might be some setting up that I could be missing?

Crimson Wizard

#1
Screen.Viewport.X/Y is where the room is displayed on screen (0,0 by default) and is defined in screen coordinates.
Game.Camera.X/Y is which part of the room is currently visible (can scroll around the room) and is defined in room coordinates.

If Water_light is a Room Object, then it's position is set in room coordinates.

So likely, you need to write
Code: ags

Water_light.X = Game.Camera.X;
Water_light.Y = Game.Camera.Y + 559;


EDIT: the eri0o's solution from below might also work.

eri0o

Icey is back!!!

Hey, so you want to make it fixed to that Screen coordinate and not the Room coordinate, is that it?

There's Screen to Room point: https://adventuregamestudio.github.io/ags-manual/Viewport.html#viewportscreentoroompoint

Maybe something like the following:

Code: ags
Point* pt = Screen.Viewport.ScreenToRoomPoint(0, 559);
Water_light.X = pt.X;
Water_light.Y = pt.Y;


Other than this you could also use an Overlay too, I think they were created exactly for this.

Icey

Thank you both for the help!

Both of those solutions look to work for what I need. Crimson's suggestion also made me realize how silly I feel that it didn't occur to my to try something like that.
But alas, in my plight of trial and error I just couldn't think of something as simple either of those answers haha.

Thanks again for the help!

(Also, howdy eri0o :D )

SMF spam blocked by CleanTalk