• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Problem with tweening (using the lerp function)

Rivo

7014
I currently have code that makes the view follow my player.
Code:
// View smoothly follows player
view_xview[0] = lerp(view_xview[0], x-view_wview[0]/2, 0.1);
view_yview[0] = lerp(view_yview[0], y-view_hview[0]/2, 0.1);
That works perfectly fine. But, I'm also trying to make the camera tween a bit toward the mouse too, so the player can see a bit more of the screen in the direction he/she points the mouse. (But not too much, so the player is still in screen)
Here is my attempt.
Code:
// mouse effect
target_x = point_direction(x, y, mouse_x/2, mouse_y/2);
target_y = point_direction(x, y, mouse_x/2, mouse_y/2);
view_xview[0] = lerp(view_xview[0], mouse_x-target_x, 0.1);
view_yview[0] = lerp(view_yview[0], mouse_y-target_y, 0.1);
Biggest problem is, I'm having trouble picturing how it works. I understand that the code moves toward a point and slows down whilst moving toward it. But I don't really understand much more than that. ^ I took the player movement code above from somewhere else and tried to make sense of it but, i'm not quite understanding. So, for me to attempt to create the effect i want with the mouse, I would need to understand better the lerp fucntion and how people use tweening in gamemaker, since all I can find online is crappy videos from people who barely explain it.

So, if anyone could help me with this code and/or help my understand better, that would be great! Thank you.
 

jo-thijs

Member
lerp(x, y, p) = x * (1 - p) + y * p = x + (y - x) * p
You can think of p as percentage (but where 100% = 1)

If you do lerp(start_x, end_x, 0.25), and you would visualize start_x and end_x at a line: s--------e
then the lerp would be 25% of that line segment, closest to start_x: s-l------e

This place will always lie between the points you have given if 0 < p < 1 (causing the view to move towards the end point)
and the distance from the start will decrease as the distance between the start and end decreases,
which result in decreasing velocity of the view.

There is 1 problem with working with tweening based on mouse coordinates.
As the view changes coordinates, the mouse coordinates in the room will change and the end point of the mouse coordinates will change,
so you won't have a nice tweening towards the mouse.

You should think: what should the end point of the view be?
 
E

elementbound

Guest
You are calculating your target point really, really wrong. What you do is take the direction and (mouse_x,mouse_y)/2 and using it as both of your coordinates. I assume you'd like to have a point towards the mouse, but not totally there. You could
a.) Have a point halfway between your player and your mouse:
Code:
target_x = (x+mouse_x)/2;
target_y = (y+mouse_y)/2;
Or ( both of these do the same ):
Code:
x = lerp(x, mouse_x, 0.5);
y = lerp(y, mouse_y, 0.5);
The higher the number, the more it leans towards the mouse.

b.) Have a point towards the mouse, in a fixed distance:
Code:
var dst = 64;
var dir = point_direction(x, y, mouse_x, mouse_y);

target_x = x + lengthdir_x(dst, dir);
target_y = y + lengthdir_y(dst, dir);
 

Rivo

7014
Thank you so much guys! I put some thought into it and referred to both of your help and I managed to figure it out! (sorry it took me so long to reply i was off gamemaker for a bit)
Code:
// follow mouse
view_xview[0] = lerp(view_xview[0], mouse_x-view_wview[0]/2, 0.05);
view_yview[0] = lerp(view_yview[0], mouse_y-view_hview[0]/2, 0.05);
I've also got code that follows the player which is why the above code works^
 
Top