Set max distance an object can go

C

Catasong

Guest


I currently have my player and another object (blue ball). The blue ball follows my mouse via:
Code:
x = mouse_x;
y = mouse_y;
However, I don't want it to travel too far. Basically this blue ball is my camera so I can move the view slightly around the player to see more.

I want it to stay within a certain radius of my players x and y position, so basically it would travel in a circle if it goes too far. How do I accomplish this?
 
M

maartenvt

Guest
You could calculate the vector from the player to the mouse. Then check if this vector is too long (longer than the radius of your red circle), and if it is too long, resize it to be the same length as your radius. Then use this vector to gain the coordinates of the ball.
Are you familiair with vectors?
 
T

tserek

Guest
Something like that..
Code:
// init position, between mouse and player
dir = point_direction(obj_player.x,obj_player.y,mouse_x,mouse_y);
len = point_distance(obj_player.x,obj_player.y,mouse_x,mouse_y);

x = obj_player.x +lengthdir_x(len/2,dir);
y = obj_player.y +lengthdir_y(len/2,dir);

// keeping circular distance to player
dir = point_direction(obj_player.x,obj_player.y,x,y);
len = point_distance(obj_player.x,obj_player.y,x,y);
maxdist = 80;

if len > maxdist
{
x = obj_player.x +lengthdir_x(maxdist,dir);
y = obj_player.y +lengthdir_y(maxdist,dir);
}

// view centering, focus between both objects
view_xview[0] = (x+obj_player.x)/2 -(view_wview[0]/2);
view_yview[0] = (y+obj_player.y)/2 -(view_hview[0]/2);
 
Last edited by a moderator:
C

Catasong

Guest
Something like that..
Code:
// init position, between mouse and player
dir = point_direction(obj_player.x,obj_player.y,mouse_x,mouse_y);
len = point_distance(obj_player.x,obj_player.y,mouse_x,mouse_y);

x = obj_player.x +lengthdir_x(len/2,dir);
y = obj_player.y +lengthdir_y(len/2,dir);

// keeping circular distance to player
dir = point_direction(obj_player.x,obj_player.y,x,y);
len = point_distance(obj_player.x,obj_player.y,x,y);
maxdist = 80;

if len > maxdist
{
x = obj_player.x +lengthdir_x(maxdist,dir);
y = obj_player.y +lengthdir_y(maxdist,dir);
}

// view centering, focus between both objects
view_xview[0] = (x+obj_player.x)/2 -(view_wview[0]/2);
view_yview[0] = (y+obj_player.y)/2 -(view_hview[0]/2);
Wow this works perfectly!! Thank you!!
 
Top