SRL Archive - Jr. Member General Help and Discussion

Almost there! need help linking functions together


ogustuce

2019-03-19 22:03:54

ok so i've been working on a lil project lately: to mimic a joystick behavior with the mouse and have it follow a path(made with SPS).

to mimic a joystick, imagine a circle. when your mouse moves to a point on the circonference, it would be like pushing your joystick that way. for example, moving the mouse to 90 degree would be like pushing your joystick upward.

so far i've suceeded:
-have the mouse follow a tpa(path) on a circle (by following angles between the points in the path). however it moves from point to point on the circle, not following the circonference(aka in a line not an arc).
-move the mouse between points following an arc of a circle.


what i want to do:
merge the two above principles together, to move from point to point of a path, on a circle, following the circonference of that circle.

[CODE]
program new;
{$i srl-6/srl.simba}
{$i ETL_Lape.simba}
//{$i mKEx.simba}
var
x,y,I,H,D,Q,W:integer;
path:tpoint;
center:tpoint:=[841,483];// center of the joystick
origin:tpoint:=[901,483];// x and y at angle 0 of
console : ETL_TDebugBox;
a,a1,a2,SD,ED:extended;

const
FirstMap1: TpointArray :=[[840, 482], [787, 425], [752, 394], [764, 370], [742, 350], [759, 309], [732, 284], [748, 256], [729, 238], [753, 216], [759, 179], [731, 154], [708, 136], [698, 144], [682, 136], [646, 170], [633, 164]];

function tpoint.gitAngleTo(pt2: TPoint): extended; //gives the angle between 2 Tpoints on the circle centered at 841,483
begin
Result := -((ArcTan2(((pt2.Y) - center.Y), (pt2.X - center.X)) * (180.0 / Pi)));
if (Result < 0.0) then
Result := (Result+360);
writeln(floattostr(result));
end;

function DirectionTarget(a:extended):Tpoint; //gives the Tpoint at angle a on the circle centered at 841,483 with radius of 65
Var
r:integer;
point:tpoint
begin
r:=65;
point.x:=841 + round(r*cos(-a*( PI / 180)));
point.y:=483 + round(r*sin(-a*( PI / 180)));
result:= [point.x,point.y];
end

function directiontargets(p1,p2:tpoint):tpointarray; //gives the TPA shaped as and arc of a circle between two points on a circle centered 841,483 with radius of 65
var
r,a4:integer;
s,arc:extended;
begin
r:=65;
a1:= (origin.gitangleto(p1));
a2:= (origin.gitangleto(p2));

if a1> a2 then
swapE(a1,a2);

h:=round(a2);
setlength(result,round(a2-a1));
result[0]:=p1;

for i:= round(a1)+1 to h-1 do
begin
result[i]:=directiontarget(i); // sets a tpoint at every 1 degree between the angle of p1 and p2
end
result[h]:=p2;

end

procedure StopMoving;// brings the mouse to the center of the circle, like an idle joystick
begin
mouse(841,483);
end

procedure adjustCenterAndOrigin(p:tpoint);
begin
center:= [p.x,p.y];
origin:=[(p.x + 60),p.y];
end


procedure RadialMouse(p:Tpointarray); // used with directiontargets. p[0] will always be closest point to the mouse
var
d1,d2:extended;
m1,m2:integer;
m:tpoint;
begin
h:= high(p);
getmousepos(m1,m2);
m:=[m1,m2];
d1:= distance(m,p[1]);
d2:= distance(m,p[h]);

if d1>d2 then
for i:= h downto 0 do
mouse(p[i],mouse_move);

if d2>d1 then
for i:= 0 to h do
mouse(p[i],mouse_move);
end



function WalkPath(Path: TPointArray): boolean;

begin
a:=origin.gitAngleTo(path[0]);
mouse(directionTarget(a),mouse_move);
H := high(Path);
for I := 1 to h do
begin
a1:=origin.gitAngleTo(path[i]);
a2:=origin.gitAngleTo(path[i+1]);

mousespeed:=5;
mouse(directiontarget(a1),mouse_move);

// radialmouse(directiontargets(directiontarget(a1),directiontarget(a2))); //<-- this is what i cant get to work. the radialmouse function inside the walkpath


begin

etl_lineto(center.x,center.y,path[i].x,path[i].y,clYellow); //draws the path
end
adjustcenterAndOrigin(path[i]);// makes center and origin the tpoint to get the correct angle
end

end



procedure cartesian; // draw a square at 0,90,180,270 degrees on a circle centered at 841,483
var
q,w,e,r:Tpoint;
begin
q:= directiontarget(0);
w:= directiontarget(90);
e:= directiontarget(180);
r:= directiontarget(270);
begin
etl_rectangle(q.x+10,q.y+10,q.x-10,q.y-10,clYellow,true);//yellow=0
etl_rectangle(w.x+10,w.y+10,w.x-10,w.y-10,clred,true);//red=90
etl_rectangle(e.x+10,e.y+10,e.x-10,e.y-10,14914580,true);//blue=180
etl_rectangle(r.x+10,r.y+10,r.x-10,r.y-10,clgreen,true);//green=270
end
end

procedure joystick; //draws a circle AKA the joystick
var
joystick:Tpointarray;
begin
joystick:= TPAFromCircle(841,483,65);
ETL_DrawTPA(joystick,clred,false);
end

begin
ETL_Start();
ETL_Clearlayer;
ClearDebug;
cartesian;
joystick;

//walkpath(firstmap1);

//radialmouse(directiontargets(directiontarget(0),directiontarget(90)));

end.



[/CODE]

-to use the functions in the main loop try:
  walkpath(firstmap1);  
it will draw the path, draw the circle ''joystick'' and move the mouse to points on that circle following the path.


radialmouse(directiontargets(directiontarget(0),directiontarget(90));
this will move your mouse from 0 to 90 degrees following the cicle if your mouse is closer to 0 degrees, from 90 to 0 if your mouse is closer to 90.


i highly suggest you have ETL by @bg5. makes everything much clearer and is such a cool tool :)
ETL by bg5: https://villavu.com/forum/showthread.php?t=106263

i've been headbutting on this for a few days now. sometimes an outside look really helps.

thank you