Windows View that follow the mouse and the player

M

molcap

Guest
Hello,
I'm trying to set a view that follows the mouse and the player without scaling the view: for example if I move the mouse to the left the view would move to that position

Thank you for read.
 
L

leonfook29

Guest
Hello,
I'm trying to set a view that follows the mouse and the player without scaling the view: for example if I move the mouse to the left the view would move to that position

Thank you for read.
If you're thinking about something like the camera will always have the character and the mouse in the view(like Nuclear Throne), to achieve that you might need to find the middle value, then have the camera follow that point. Would recommend you use an object to control the view.

Code:
//THIS IS THE CODE FOR CAMERA OBJECT
//THE object_follow IS A VARIABLE THAT STORE THE PLAYER CHARACTER ID
var length = point_distance(object_follow.x,object_follow.y,mouse_x,mouse_y)/2);
var dir = point_direction(object_follow.x,object_follow.y,mouse_x,mouse_y);
x = lengthdir_x(length,dir)
y = lengthdir_y(length,dir)
 
M

molcap

Guest
If you're thinking about something like the camera will always have the character and the mouse in the view(like Nuclear Throne), to achieve that you might need to find the middle value, then have the camera follow that point. Would recommend you use an object to control the view.

Code:
//THIS IS THE CODE FOR CAMERA OBJECT
//THE object_follow IS A VARIABLE THAT STORE THE PLAYER CHARACTER ID
var length = point_distance(object_follow.x,object_follow.y,mouse_x,mouse_y)/2);
var dir = point_direction(object_follow.x,object_follow.y,mouse_x,mouse_y);
x = lengthdir_x(length,dir)
y = lengthdir_y(length,dir)
It didn't work but I get the idea
 
A

Aura

Guest
Code:
view_xview[0] = (obj_player.x+((mouse_x-obj_player.x)/2))-view_wview[0]/2;
view_yview[0] = (obj_player.y+((mouse_y-obj_player.y)/2))-view_hview[0]/2;
...in the Step event should do it. Perhaps.
 

TheouAegis

Member
With a centered origin:

view_xview = floor(player.x - (player.sprite_width>>1)*sign(mouse_x - player.x | 1) + mouse_x - view_wview >> 1);
view_yview = floor(player.y - (player.sprite_height>>1)*sign(mouse_y - player.y | 1) + mouse_y - view_hview >> 1);


If the origin is not centered, it'll be trickier. If you have the origin centered horizontally but at the very bottom of the sprite, then change (player.sprite_height>>1) to just player_sprite_height.

Of course if you put all this code in the player object, you don't even need the player. part of it.


Alternative code:

Code:
if abs((mouse_x - view_xview - view_wview/2)) < view_wview>>2 && abs((mouse_y - view_yview - view_hview/2)) < view_hview>>2
{
  var lr = x - view_wview/2 - view_xview;
  var ud = y - view_hview/2 - view_yview;
  view_xview += lr>>2;
  view_yview += ud>>2;
}
else
{
  var lr = sign(mouse_x - x | 1);
  var ud = sign(mouse_y - y | 1);
  if abs(x - mouse_x>>5)
  view_xview = x - (sprite_width>>1)*lr + mouse_x - view_wview >> 1;
  if abs(y - mouse_y>>5)
  view_yview = y - (sprite_height>>1)*ud + mouse_y - view_hview >> 1;
}
 
M

molcap

Guest
Code:
view_xview[0] = (obj_player.x+((mouse_x-obj_player.x)/2))-view_wview[0]/2;
view_yview[0] = (obj_player.y+((mouse_y-obj_player.y)/2))-view_hview[0]/2;
...in the Step event should do it. Perhaps.
With a centered origin:

view_xview = floor(player.x - (player.sprite_width>>1)*sign(mouse_x - player.x | 1) + mouse_x - view_wview >> 1);
view_yview = floor(player.y - (player.sprite_height>>1)*sign(mouse_y - player.y | 1) + mouse_y - view_hview >> 1);


If the origin is not centered, it'll be trickier. If you have the origin centered horizontally but at the very bottom of the sprite, then change (player.sprite_height>>1) to just player_sprite_height.

Of course if you put all this code in the player object, you don't even need the player. part of it.


Alternative code:

Code:
if abs((mouse_x - view_xview - view_wview/2)) < view_wview>>2 && abs((mouse_y - view_yview - view_hview/2)) < view_hview>>2
{
  var lr = x - view_wview/2 - view_xview;
  var ud = y - view_hview/2 - view_yview;
  view_xview += lr>>2;
  view_yview += ud>>2;
}
else
{
  var lr = sign(mouse_x - x | 1);
  var ud = sign(mouse_y - y | 1);
  if abs(x - mouse_x>>5)
  view_xview = x - (sprite_width>>1)*lr + mouse_x - view_wview >> 1;
  if abs(y - mouse_y>>5)
  view_yview = y - (sprite_height>>1)*ud + mouse_y - view_hview >> 1;
}
It works but it shows the outside of the room, I want that only shows the inside, also, if the mouse is so far the ob_player will be out of the view
 

TheouAegis

Member
Um... I know for a fact that my code doesn't move the player outside of the view, and Aura's might clip the player but not actually remove it.

What are your view settings and what is the player's sprite setting? Are you sure you don't have two instances of your player in the room? Check the Instances List in the rooms to make sure.

As for the showing outside the room, put at the very end:

view_xview = median(view_xview,0,room_width - view_wview);
view_yview = median(view_yview,room_height-view_hview);
 
M

molcap

Guest
view_yview = median(view_yview,room_height-view_hview);
you forgot a zero XD, anyway in fullscreen mode when you put the mouse in the black area (the black area when you have a 3:4 format on a 16:9 screen) the player get outside the view, also the movement isn't very smooth and I do not want to move both sides to the point of leaving the player the edge (If I knew how to fix it I would do it)

Thank you very much
 
L

leonfook29

Guest
It seems like you need to clamp the view_xview between 0 and (room_width - view_wview). Also to prevent the player from going outside of view(mouse getting too far) you need to limit the range of that middle point.

Code:
var max_length = 300; //THE MAXIMUM DISTANCE THAT THE CAMERA CAN GO
var length = min(point_distance(player_obj.x,player_obj.y,mouse_x,mouse_y)/2, max_length);
var dir = point_direction(player_obj.x,player_obj.y,mouse_x,mouse_y);
view_xview = lengthdir_x(length,dir)
view_yview = lengthdir_y(length,dir)
 
L

leonfook29

Guest
Ohh, i did have some other code handling the smoothing of everything include the camera movement, this is just something barebone. But yours are definitely shorter.
 
Top