How to make images move based on mouse position? Looking for a Parallax style effect.

Hey, I'm currently developing a game using RPG Maker MZ.

The game has some visual novel elements, so I was looking for a way to display images and then have them move based on mouse position.


Something like this effect from a RenPy tutorial.

The images would be larger than the game screen, and then the player can move their mouse around to see more parts of the image. It doesn't have to be anything too drastic, I'm just looking for a subtle effect so the player isn't just stuck on a static image for a while.
So... you want to know how to capture the mouse position...?
So... you want to know how to capture the mouse position...?
I want an onscreen image to move based on the mouse position.

Here's an example of it from a Javascript tutorial
I understand your goal, I just don't understand what is keeping you from getting there.

Please help me (or other viewers for that matter) understand what you need. What is it that doesn't go well? I mean there's nothing, not even a hint, of where you are in your creative effort.

It seems to me that it's just a matter of making your pictures move relative to the position of the mouse on the canvas, hence my asking "you want to know how to capture the mouse position...?"
I understand your goal, I just don't understand what is keeping you from getting there.

Please help me (or other viewers for that matter) understand what you need. What is it that doesn't go well? I mean there's nothing, not even a hint, of where you are in your creative effort.

It seems to me that it's just a matter of making your pictures move relative to the position of the mouse on the canvas, hence my asking "you want to know how to capture the mouse position...?"

This is my first-time using RPG Maker, so I'm still getting the hang of how things work. I know the basics of displaying and moving an image, but I don't know where to start when it comes to having the image move in relation to mouse position.


Right now I'm experimenting with the engine and seeing if it does everything I need to make this game. The parallax effect would be a really nice addition as I wanna avoid static images, I just don't know how to implement it.

The effect would primarily be used during dialogue scenes, so the image would be in the background with a textbox over the top.

Capturing the mouse position would be useful info, but then I'd still need to know how to make the image move based on that position.
(Those examples you've been checking out might have used the term "event" to mean a DOM event. Just to be clear, an "event" in RPG Maker is a higher-level abstraction specific to the engine: there is no built-in "mouse move" event, but you can make something that updates every frame.)

The current touch/mouse coordinates on the game canvas can be fetched using a script call:
  • X: TouchInput.x;
  • Y: TouchInput.y.
To update a picture's position every frame, you can:
  1. Use Show Picture to show the picture whenever it should first appear. If you want it to appear when arriving on the map, you can make a very simple event with Parallel trigger that erases itself after processing once, e.g.

    ◆Show Picture:#1, MyPicture, Center (408,312), (100%,100%), 255, Normal ◆Erase Event
    Erase Event lasts until the player leaves that map.

  2. Make an event to handle picture movement. You can use the Parallel trigger to let it process without interrupting normal player controls. Note that a Parallel event will trigger any frame it is not already running, effectively making it a "looped event".

  3. In that event, you can use:
    • Control Variables -> Script to store the X/Y coordinates in a couple of variables;
    • Move Picture -> Designation with variables to move your picture to those coordinates.
    E.g.

    ◆Control Variables:#0001 Aim X Screen = TouchInput.x ◆Control Variables:#0002 Aim Y Screen = TouchInput.y ◆Move Picture:#1, Linear, Center ({Aim X Screen},{Aim Y Screen}), (100%,100%), 255, Normal, 1 frame
The current touch/mouse coordinates on the game canvas can be fetched using a script call:
  • X: TouchInput.x;
  • Y: TouchInput.y.

I assumed you needed
JavaScript:
Graphics.pageToCanvasX(event.pageX)
so I read this and I was like WHAT? Learned a new thing again! Thanks!
@werzque - in case you're interested: that's how the engine converts the HTML/DOM event coordinates to game canvas coordinates. The result is passed to one of the relevant TouchInput methods, e.g. _onTrigger (cf rmmz_core.js), which updates these x and y values for later reference. 👋
(Those examples you've been checking out might have used the term "event" to mean a DOM event. Just to be clear, an "event" in RPG Maker is a higher-level abstraction specific to the engine: there is no built-in "mouse move" event, but you can make something that updates every frame.)

The current touch/mouse coordinates on the game canvas can be fetched using a script call:
  • X: TouchInput.x;
  • Y: TouchInput.y.
To update a picture's position every frame, you can:
  1. Use Show Picture to show the picture whenever it should first appear. If you want it to appear when arriving on the map, you can make a very simple event with Parallel trigger that erases itself after processing once, e.g.

    ◆Show Picture:#1, MyPicture, Center (408,312), (100%,100%), 255, Normal ◆Erase Event

    Erase Event lasts until the player leaves that map.

  2. Make an event to handle picture movement. You can use the Parallel trigger to let it process without interrupting normal player controls. Note that a Parallel event will trigger any frame it is not already running, effectively making it a "looped event".

  3. In that event, you can use:
    • Control Variables -> Script to store the X/Y coordinates in a couple of variables;
    • Move Picture -> Designation with variables to move your picture to those coordinates.
    E.g.

    ◆Control Variables:#0001 Aim X Screen = TouchInput.x ◆Control Variables:#0002 Aim Y Screen = TouchInput.y ◆Move Picture:#1, Linear, Center ({Aim X Screen},{Aim Y Screen}), (100%,100%), 255, Normal, 1 frame


I tried following the instructions, but I'm pretty sure I'm getting it wrong. 😳

I started by showing the image and then adding the erase event so it would stay on screen.

1.jpg


Then I did the control variables.

2.jpg


And then I tried to add those variables to the move picture event.

3.jpg
@GWL_Art - yes, you have a few small mistakes:
  1. A map event can only have 1 page active at a time. To decide which page is active, the engine checks their conditions, starting with the highest page number: the first page found with either no conditions, or all conditions met, becomes active. You have 2 pages in your event, but the second page has no conditions, which means the event will always use page 2 instead of page 1.

    However, that's irrelevant in this case, because...

  2. Erase Event basically sets the map event to page -1 (i.e. "nothing") until the player leaves the map. So "show picture" and "control picture position" should be 2 different events, not just 2 different pages.

  3. The "control picture position" event should have Parallel trigger. (In your screenshots it is set to Action Button.)
@GWL_Art - yes, you have a few small mistakes:
  1. A map event can only have 1 page active at a time. To decide which page is active, the engine checks their conditions, starting with the highest page number: the first page found with either no conditions, or all conditions met, becomes active. You have 2 pages in your event, but the second page has no conditions, which means the event will always use page 2 instead of page 1.

    However, that's irrelevant in this case, because...

  2. Erase Event basically sets the map event to page -1 (i.e. "nothing") until the player leaves the map. So "show picture" and "control picture position" should be 2 different events, not just 2 different pages.

  3. The "control picture position" event should have Parallel trigger. (In your screenshots it is set to Action Button.)
It worked! Thank you so much, this is exactly the kind of effect I was looking for. I checked it with a dialogue box too and it works. 😀

@GWL_Art - yes, you have a few small mistakes:
  1. A map event can only have 1 page active at a time. To decide which page is active, the engine checks their conditions, starting with the highest page number: the first page found with either no conditions, or all conditions met, becomes active. You have 2 pages in your event, but the second page has no conditions, which means the event will always use page 2 instead of page 1.

    However, that's irrelevant in this case, because...

  2. Erase Event basically sets the map event to page -1 (i.e. "nothing") until the player leaves the map. So "show picture" and "control picture position" should be 2 different events, not just 2 different pages.

  3. The "control picture position" event should have Parallel trigger. (In your screenshots it is set to Action Button.)

Just a little followup to this.

I played around with the duration time, and increasing it to around 90 gives it a really nice delay effect.

Is there anyway to stop the image moving too far in a certain direction? Right now if you move the mouse all the way to the edge of the screen then the end of the image starts to show.

Or is there a way to make the image movement more subtle, so it's not 1:1 with the mouse position?
Is there anyway to stop the image moving too far in a certain direction? Right now if you move the mouse all the way to the edge of the screen then the end of the image starts to show.

Or is there a way to make the image movement more subtle, so it's not 1:1 with the mouse position?
Yes. The target coordinates are stored in variables: you can change the values of those variables before passing them into the Move Picture command. E.g.

◆Comment:Get in-game touch coordinate X. ◆Control Variables:#0001 Aim X Screen = TouchInput.x ◆Comment:Scale to 7/10 of current value. ◆Control Variables:#0001 Aim X Screen *= 7 ◆Control Variables:#0001 Aim X Screen /= 10 ◆Comment:Force minimum value. ◆If:Aim X Screen < 50 ◆Control Variables:#0001 Aim X Screen = 50 ◆ :End
Yes. The target coordinates are stored in variables: you can change the values of those variables before passing them into the Move Picture command. E.g.

◆Comment:Get in-game touch coordinate X. ◆Control Variables:#0001 Aim X Screen = TouchInput.x ◆Comment:Scale to 7/10 of current value. ◆Control Variables:#0001 Aim X Screen *= 7 ◆Control Variables:#0001 Aim X Screen /= 10 ◆Comment:Force minimum value. ◆If:Aim X Screen < 50 ◆Control Variables:#0001 Aim X Screen = 50 ◆ :End


This works!

But now the only issue is that it displays the image off center 😳 It kinda pushes the image more towards the top left. Is there anyway to center the image?



The grey box is my test image, it's meant to be centered on the screen. The brightest grey part is the screen size, and then as the greys get darker, those are meant to be the cutoff points.
Sounds like you just need to figure out an appropriate scaling~

A general approach (per-axis) could be:
  1. Get in-game touch coordinate.
  2. Apply touch area min/max if appropriate, e.g. clamp input within 48 px of screen edges.
  3. Get difference between touch and screen centre.
  4. Multiply by -1 if you like, to make the picture move in the opposite direction.
  5. Scale from the size of the touch area to the size of your picture's "dark" margins.
  6. Add screen centre.
E.g. for the X coordinate, with screen width 816 px, touch margin 48 px, picture margin 96 px:
Spoiler: event example
◆Comment:1) Get in-game touch coordinate X. ◆Control Variables:#0001 Aim X Screen = TouchInput.x ◆Comment:2) Apply 48 px "touch area" margin. ◆If:Aim X Screen < 48 ◆Control Variables:#0001 Aim X Screen = 48 ◆ :End ◆If:Aim X Screen > 768 ◆Control Variables:#0001 Aim X Screen = 768 ◆ :End ◆Comment:3) Get difference between touch coordinate and screen centre. ◆Control Variables:#0001 Aim X Screen -= 408 ◆Comment:4) Invert move direction. ◆Control Variables:#0001 Aim X Screen *= -1 ◆Comment:5) Scale down from touch area (half screen width - touch margin) to picture margin width. ◆Control Variables:#0001 Aim X Screen *= 96 ◆Control Variables:#0001 Aim X Screen /= 360 ◆Comment:6) "Start at screen centre". ◆Control Variables:#0001 Aim X Screen += 408
(Untested, hopefully the maths is correct.) Similarly for the Y coordinate. 👋
Sounds like you just need to figure out an appropriate scaling~

A general approach (per-axis) could be:
  1. Get in-game touch coordinate.
  2. Apply touch area min/max if appropriate, e.g. clamp input within 48 px of screen edges.
  3. Get difference between touch and screen centre.
  4. Multiply by -1 if you like, to make the picture move in the opposite direction.
  5. Scale from the size of the touch area to the size of your picture's "dark" margins.
  6. Add screen centre.
E.g. for the X coordinate, with screen width 816 px, touch margin 48 px, picture margin 96 px:
Spoiler: event example
◆Comment:1) Get in-game touch coordinate X. ◆Control Variables:#0001 Aim X Screen = TouchInput.x ◆Comment:2) Apply 48 px "touch area" margin. ◆If:Aim X Screen < 48 ◆Control Variables:#0001 Aim X Screen = 48 ◆ :End ◆If:Aim X Screen > 768 ◆Control Variables:#0001 Aim X Screen = 768 ◆ :End ◆Comment:3) Get difference between touch coordinate and screen centre. ◆Control Variables:#0001 Aim X Screen -= 408 ◆Comment:4) Invert move direction. ◆Control Variables:#0001 Aim X Screen *= -1 ◆Comment:5) Scale down from touch area (half screen width - touch margin) to picture margin width. ◆Control Variables:#0001 Aim X Screen *= 96 ◆Control Variables:#0001 Aim X Screen /= 360 ◆Comment:6) "Start at screen centre". ◆Control Variables:#0001 Aim X Screen += 408
(Untested, hopefully the maths is correct.) Similarly for the Y coordinate. 👋

Thanks! I found a little workaround that gives me the added benefit of changing the amount of x or y movement depending on the image.

I made a numbered grid that was big enough to fill the entire screen, then I can map the furthest point of up/down/left/right by the numbers. After that it's just a case of making sure my image fills that section of the grid and saving the entire thing as a PNG.

Thank you for all the help with this, the parallax effect was my biggest worry when it came to making this game, and it looks like this will be the perfect solution! 😀
Great, you're welcome! Happy RPG Making~ 😂

Original thread: https://forums.rpgmakerweb.com/threads/how-to-make-images-move-based-on-mouse-position-looking-for-a-parallax-style-effect.158228/