***SOLVED*** Adding rotational movement

Hey guys. Taking another crack at programming a different project. I always hit a brick wall when it comes to applying trig to my projects. The similar movement to A Link to the Past with a mouse and keyboard. 'W' moves the player toward whatever target is locked on to, and 'S' moves them backward away from them. I want to use 'A' and 'D' to orbit around the target (whether it be an enemy or the mouse) clockwise and counter-clockwise respectively, but and unsure as to where or how to implement that logic. Thanks for your consideration.

Code:
//switch targets between mouse and actor
    var targx,targy;
    if(global.target<>noone){
        targx=global.target.x;
        targy=global.target.y;
    }else{
        targx=mouse_x;
        targy=mouse_y;
    }
    
    //set direction
    var pdir = point_direction(x,y,targx,targy);
    
    // apply walk movement
    if( walk_dir == 1 || walk_dir == -1){
        //get distance/length
        var len = move_spd;   
            
        //get xspd and yspd
        xspd = lengthdir_x(len, pdir) ;
        yspd = lengthdir_y(len, pdir) ;
        
    }
        
    //move player
    x += xspd*walk_dir;
    y += yspd*walk_dir;
 
T

trentallain

Guest
Point directon + 90 and - 90 using your pdir? Then just include that when you change the X/Y like you're doing already. Not sure how to do this on top of moving forwards or backwards though.
 
No, I don't think that work. It may just snap or subtract a rigid 90 degrees to the vector. I want to add or subtract a gradual amount. It's frustrating because I can make an object orbit another, and i can make one move away or towards another, I'm just not knowledgeable as to how to combine them to create an arc between the two.
 
Point directon + 90 and - 90 using your pdir? Then just include that when you change the X/Y like you're doing already. Not sure how to do this on top of moving forwards or backwards though.
Adding 90 to the angle is starting to get somewhere. Thing is, however, it seems to negate the forward or backward movement; the instance just rotates around at the same distance.
 
T

trentallain

Guest
Adding 90 to the angle is starting to get somewhere. Thing is, however, it seems to negate the forward or backward movement; the instance just rotates around at the same distance.
Maybe try adding another point direction between the moving forwards/backwards Xspd/Yspd and the orbiting Xspd/Yspd and then using lengthdir again to find the final trig? Not sure how well that would work haha.
 
T

trentallain

Guest
Adding 90 to the angle is starting to get somewhere. Thing is, however, it seems to negate the forward or backward movement; the instance just rotates around at the same distance.
Oh also for the length argument in lengthdir for the orbit, that should be point distance of you and the target I think
 
This will move them forwards and backwards, and rotate them around. Not sure if that's totally what you want - as I can't recall how you move in A Link To The Past.

create event:
Code:
spd = whatever;
step event:
Code:
var targx,targy;
if(global.target<>noone)
{
targx=global.target.x;
targy=global.target.y;
}
else
{
targx=mouse_x;
targy=mouse_y;
}
 

var pdir = point_direction(targx, targy, x, y);
var pdist = point_distance(x, y, targx, targy);
var forward = keyboard_check(ord("W"))
var back = keyboard_check(ord("S"))
 
if forward
{
walkdir = -1
}
 
if back
{
walkdir = 1;
}

if ((!forward) && (!back))
{
walkdir = 0;
}
 
if walkdir != 0
{
pdist += walkdir * spd; 
}
    
if keyboard_check(ord("A"))
{
pdir += -1
}
 
if keyboard_check(ord("D"))
{
pdir += 1;
}
 
x = targx + lengthdir_x(pdist, pdir);
y = targy + lengthdir_y(pdist, pdir);
image_angle = pdir + 180;
 
T

trentallain

Guest
In a link to the past you use a d-pad to move. This is more like ocarina of time 2D I think?
 
Well, the rotation works, but there is an unforseen issue. Basically, the further away the player instance is from the target, the greater the change in position. I know it's because to rotate 1degree, the change is greater the further away from the origin of the vector you are. So, I want to alter the number I add to 'pDir' so that the rotational change feels the same no matter how close or far away you are. I'm guessing this is called "normalizing" the value, but I'm not sure that's exactly correct.
 
If I understand correctly what you're trying to do, I think this will work well enough for you:

var _mx = move_speed * (keyboard_check(vk_up) - keyboard_check(vk_down));
var _my = move_speed * (keyboard_check(vk_right) - keyboard_check(vk_left));
var _dir = point_direction(x,y,mouse_x,mouse_y);
var _c = dcos(_dir);
var _s = dsin(_dir);
hspeed = _mx * _c + _my * _s;
vspeed = _mx * -_s + _my * _c;

Presuming the mouse is the target point. You can have seperate "move_speed" variables for _mx and _my if you'd like.
 
If I understand correctly what you're trying to do, I think this will work well enough for you:

var _mx = move_speed * (keyboard_check(vk_up) - keyboard_check(vk_down));
var _my = move_speed * (keyboard_check(vk_right) - keyboard_check(vk_left));
var _dir = point_direction(x,y,mouse_x,mouse_y);
var _c = dcos(_dir);
var _s = dsin(_dir);
hspeed = _mx * _c + _my * _s;
vspeed = _mx * -_s + _my * _c;

Presuming the mouse is the target point. You can have seperate "move_speed" variables for _mx and _my if you'd like.
You guys are the greatest! Thank you so much. Seriously, almost every project that I abandoned I did so because of my lack of knowledge of trig. You wouldn't happen to know of any tutorials or youtube demonstrations of trigonometry for games for dumb dumbs would ya?
 
You guys are the greatest! Thank you so much. Seriously, almost every project that I abandoned I did so because of my lack of knowledge of trig. You wouldn't happen to know of any tutorials or youtube demonstrations of trigonometry for games for dumb dumbs would ya?
If you have a pretty firm grasp of basic algebra, learning basic trig in 2d is actually pretty straightforward. You could probably get it done in a couple of afternoons. A math textbook is where I would personally look. But I'm sure there must be tons of videos on youtube and other resources online.

Now, the huge majority of questions I see that involve some kind of trig basically revolve around rotating a vector. Which is all that is going on above, just rotating the vector (_mx,_my) by the angle _dir. And if you want to understand why what I posted works, I drew up this diagram a while back that I hope makes it somewhat clear. Although here, a vector (x_offset,y_offset) is being rotated and then added to another vector (parent_x,parent_y), but the rotation is done in the same way.



Notice that the y axis points downward here, and that is just the convention that we're using in gamemaker's default orthographic projection. In textbooks, you will see the y axis point upwards, and positive angles of rotation still going anti-clockwise. In which case the sign before the sine functions will be reversed in most calculations.
 
Last edited:
Top