GameMaker Help with angles maths

Petrik33

Member
Hello everybody. I need help in making my cursor (which is an aim of the player) move randomly around the real cursor(mouse x and y). The problem is I don't know how to create the function of the x and y coordinates of the cursor (which is actually just a point with sprite) the way that it moves smoothly and randomly. Being more concrete the point should be waving around the cursor and its not neccesarly that it crosses the center of this circle, and I also need this function to contain arguments defining the size of this circle and the speed of the point movement. Would be very pleased even if you share some literature or articles in google which can help me because I personally haven't found anything usefull searching the web.
Yeah and to be clear I need this function to create aim in the game not standing on one place to make aiming really harder(the game itself is turn-based, so I have to make shooting more interesting anyway).
 

Yal

🐧 *penguin noises*
GMC Elder
It's easiest if you have two separate points: the target coordinates, which changes randomly and erratically all the time, and the actual coordinates, which you change smoothly using linear interpolation:
GML:
tx = mouse_x + random_range(-64,64);
ty = mouse_y + random_range(-64,64);
cursor_x = lerp(cursor_x,tx,0.10);
cursor_y = lerp(cursor_y,ty,0.10);
 

Petrik33

Member
It's easiest if you have two separate points: the target coordinates, which changes randomly and erratically all the time, and the actual coordinates, which you change smoothly using linear interpolation:
GML:
tx = mouse_x + random_range(-64,64);
ty = mouse_y + random_range(-64,64);
cursor_x = lerp(cursor_x,tx,0.10);
cursor_y = lerp(cursor_y,ty,0.10);
thanks a lot but it makes the cursor move very chaotic.
 

Petrik33

Member
Ok so now the problem has changed slightly. It seems like I have found the solution but the porblem is that I need help with angles maths. Here is the current code of the step event for my aim:
GML:
var _distance_toMouse=point_distance( 0,0,aim_x,aim_y);
var _direction_toMouse=point_direction(aim_x,aim_y,0,0);
if(Chance(_distance_toMouse/Radius))  //The chance gets higher when you get closer to the edge of the circle
{
    destination=_direction_toMouse;
    destination=WrapAngle(destination);
}


//The problem is HERE !!!
if(direction-destination<0)
{
    direction=min(direction+0.02,destination);
}
else
{
    direction=max(direction-0.02,destination);
}
//The Problem is Below ^^^^^


var Vx=lengthdir_x(spd,direction);
var Vy=lengthdir_y(spd,direction);
aim_x+=Vx;
aim_y+=Vy;

x=mouse_x+aim_x;
y=mouse_y+aim_y;
So the problem is that I don't know how to make the maths to make smooth direction changing. Actually I need the direction to smoothly become destination so it looks smoothly, and min and max functions would do great in many cases but not here: the problem is that direction wraps over 360 and below 0 , so when for example direction is 359 and destination is 1, the direction will decrese from 359 to 1, but It could just go reverse and increse from 359 to 1 as it is the angle? So how can I do in this case????
 
Last edited:

Petrik33

Member
Try changing the 0.1 to a lower number (e.g. 0.01).
Doesn't help, still I have chaged direction to other variable so it doesn't wrap, and the result is pretty good, here is tht code, but still it ain't perfect because the cursor isn't always beutifully waving:
GML:
if(Chance(0.033))
{
    spd+=random_range(-spd/10,spd/10);
    spd=clamp(spd,min_spd,max_spd)
}

var _distance_toMouse=point_distance( 0,0,aim_x,aim_y);
var _direction_toMouse=point_direction(aim_x,aim_y,0,0);
if(Chance( (_distance_toMouse/Radius)*(_distance_toMouse/Radius) ) )
{
    var _choose=irandom_range(-1,1);
    if(_choose!=0) clockWise=_choose;
    destination=_direction_toMouse+clockWise*random(30)
}
if(_distance_toMouse>Radius)
{
    destination=_direction_toMouse;   
}

if(clockWise) current_direction=min(current_direction+12,destination);
else current_direction=max(current_direction-12,destination);


var Vx=lengthdir_x(spd,current_direction);
var Vy=lengthdir_y(spd,current_direction);
aim_x+=Vx;
aim_y+=Vy;

x=mouse_x+aim_x;
y=mouse_y+aim_y;
Anyway thanks a lot, and good luck if you trake part in current Jam
 
Top