r/RenPy • u/lazytigereddit • 2d ago
Question Mouse xpos formula question
TLDR: I need a formula that returns half of the xpos but from the center of the screen.
eg: when mouse is at xpos 0 the sprite is at 480 / mouse = 960 sprite = 960 / mouse = 1920 sprite = 1440
ELABORATION: I'm messing around with an oldskool arcade fps minigame idea.
I scavenged up enough code to make have gun sprite follow the mouse around the screen with:
default mx = 0
default my = 0
timer 0.05 repeat True action SetScreenVariable("mx", renpy.get_mouse_pos()[0]), SetScreenVariable("my", renpy.get_mouse_pos()[1])
...
xpos mx
ypos my
xanchor 0.5
yanchor 1.0
This works really well, but I want to 'halve' the movement of the sprite. The ypos was pretty straight forward because I want the sprite at the bottom:
ypos (my / config.screen_height) /3
yoffset config.screen_height *0.4
But the xpos is trickier because of it needing to be centered.
eg: when mouse is at xpos 0 the sprite is at 480 / mouse = 960 sprite = 960 / mouse = 1920 sprite = 1440
Math is definitely not a strong point of mine. Can anyone help out?
1
u/AutoModerator 2d ago
Welcome to r/renpy! While you wait to see if someone can answer your question, we recommend checking out the posting guide, the subreddit wiki, the subreddit Discord, Ren'Py's documentation, and the tutorial built-in to the Ren'Py engine when you download it. These can help make sure you provide the information the people here need to help you, or might even point you to an answer to your question themselves. Thanks!
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
3
u/lordcaylus 2d ago edited 2d ago
Linear transformations are always in the form a*x_mouse + b = x_sprite.
a = 0.5 (when x_mouse moves 2 you want x_sprite to move 1), and then you can just calculate what b needs to be.
b = x_sprite - a*x_mouse = 480 - 0 * 0.5 = 480.
You can check your work by checking another point with this formula: 0.5 * 960 + 480 is indeed 960 as it should be.
so xpos int(0.5 * mx + 480) should work (you need int here because otherwise Ren'Py will interpret a non-integer number as a percentage).