Orbiting Controls [SOLVED]

K

Karrlem

Guest
Hello all, so I'm working on a game and I would like to know how to use two keys(A+D, Left + Right arrow) as controls to orbit an object.
 

obscene

Member
Lets say you have a point, x,y.

You want to find another point a distance of 100 pixels away at exactly 45 degrees...

xx=x+lengthdir_x(100,45);
yy=y+lengthdir_y(100,45);

So if instead of 45 you used a variable, you could increase or decrease it therefore changing the value off xx,yy around the point.
 
K

Karrlem

Guest
Lets say you have a point, x,y.

You want to find another point a distance of 100 pixels away at exactly 45 degrees...

xx=x+lengthdir_x(100,45);
yy=y+lengthdir_y(100,45);

So if instead of 45 you used a variable, you could increase or decrease it therefore changing the value off xx,yy around the point.
Ok so how would I go about doing this? what do I set xx/yy as and what variable to put instead of 45 degrees?
 

zircher

Member
Just create a global var like player_angle since you will be referencing it a lot. You probably will want some wrap-around checks...
if player_angle > 360 then player_angle -= 360;
if player_angle < 0 then player_angle += 360;
 

Nux

Member
I'll just dump this code here since it's obsolete to me, maybe you can find use of it.
here it is in use
Code:
# create event:
/// define variables

// gravity
g        = 1;
g_angle  = 270; // gravity direction (down)

// acceleration
a        = 1;
a_angle  = 180; // acceleration direction (normal to grav)

// flags
dir      = 0;
hv       = 0;
vv       = 0;
Code:
# step event
// get inputs
key_left  =-keyboard_check( ord("A") );
key_right = keyboard_check( ord("D") );

// calculate the direction the for player inputs
dir = key_left + key_right;

g_angle = point_direction(mouse_x,mouse_y,x,y)-180; // fall towards mouse
a_angle = g_angle + 90;
image_angle = a_angle;

// move left/right
if ( dir == 0 )
{
    if ( hv != 0 )
    {
        // slow down
        var pre_hv = hv;
        hv -= a * sign(hv);
        if ( sign(pre_hv) != sign(hv) ) hv = 0;
    }
}
else
    hv += a * dir;

// gravity
if ( vv < 20 )
        vv += g;

// change depending on angle
var ha, va; // horiz acc angle, and verti acc angle
ha = dcos( 360 - a_angle) * hv + dcos( 360 - g_angle) * vv;
va = dsin( 360 - a_angle) * hv + dsin( 360 - g_angle) * vv;

x += ha;
y += va;
 
K

Karrlem

Guest
Just create a global var like player_angle since you will be referencing it a lot. You probably will want some wrap-around checks...
if player_angle > 360 then player_angle -= 360;
if player_angle < 0 then player_angle += 360;
Ok cool, now i'm just trying to figure out how to get it to move using lengthdir_x/y
 
K

Karrlem

Guest
I'll just dump this code here since it's obsolete to me, maybe you can find use of it.
here it is in use
Code:
# create event:
/// define variables

// gravity
g        = 1;
g_angle  = 270; // gravity direction (down)

// acceleration
a        = 1;
a_angle  = 180; // acceleration direction (normal to grav)

// flags
dir      = 0;
hv       = 0;
vv       = 0;
Code:
# step event
// get inputs
key_left  =-keyboard_check( ord("A") );
key_right = keyboard_check( ord("D") );

// calculate the direction the for player inputs
dir = key_left + key_right;

g_angle = point_direction(mouse_x,mouse_y,x,y)-180; // fall towards mouse
a_angle = g_angle + 90;
image_angle = a_angle;

// move left/right
if ( dir == 0 )
{
    if ( hv != 0 )
    {
        // slow down
        var pre_hv = hv;
        hv -= a * sign(hv);
        if ( sign(pre_hv) != sign(hv) ) hv = 0;
    }
}
else
    hv += a * dir;

// gravity
if ( vv < 20 )
        vv += g;

// change depending on angle
var ha, va; // horiz acc angle, and verti acc angle
ha = dcos( 360 - a_angle) * hv + dcos( 360 - g_angle) * vv;
va = dsin( 360 - a_angle) * hv + dsin( 360 - g_angle) * vv;

x += ha;
y += va;
Thank you! I will definitely use this on one of my future projects!
 
  • Like
Reactions: Nux

Nux

Member
I may have misunderstood the question, if you want the object to rotate around a pivot (e.g. a player holding a gun) you can do it simply like this:
Code:
var angle = point_direction(player.x,player.y,mouse_x,mouse_y);
var radius = 10; // change this to the distance you want the object to orbit
var xx = dcos(angle) * radius;
var yy = dsin(angle) * radius;
gun.x = player.x+xx; // change gun.[] and player.[] to any variable you want to store the "orbiting"
gun.y = player.y+yy;
EDIT:
Thank you! I will definitely use this on one of my future projects!
Awesome! it'll be cool to see what you come up with. ;o;
 

NightFrost

Member
Well I answered a question about orbital movement in this thread, but to adapt the code to keyboard controls:
Code:
CREATE:
Orbit = 200; // Orbit distance
Angle = 0; // Current orbital angle
Speed = 1; // Orbital speed
Center_X = room_width / 2; // x of orbital center
Center_Y = room_height / 2; // y of orbital center

STEP:
// Orbital motion
if(keyboard_check(vk_left)){
    Angle += Speed; // Orbit counterclockwise on left arrow
    if(Angle >= 360) Angle -= 360; // Keep angle below 360
}
else if(keyboard_check(vk_right)){
    Angle -= Speed; // Orbit clockwise on right arrow
    if(Angle < 0) Angle += 360; // Keep angle at 0 or above
}

// Update position
x = lengthdir_x(Orbit, Angle) + Center_X;
y = lengthdir_y(Orbit, Angle) + Center_Y;
EDIT - this code goes, obviously, to the orbiting object.
 
K

Karrlem

Guest
Well I answered a question about orbital movement in this thread, but to adapt the code to keyboard controls:
Code:
CREATE:
Orbit = 200; // Orbit distance
Angle = 0; // Current orbital angle
Speed = 1; // Orbital speed
Center_X = room_width / 2; // x of orbital center
Center_Y = room_height / 2; // y of orbital center

STEP:
// Orbital motion
if(keyboard_check(vk_left)){
    Angle += Speed; // Orbit counterclockwise on left arrow
    if(Angle >= 360) Angle -= 360; // Keep angle below 360
}
else if(keyboard_check(vk_right)){
    Angle -= Speed; // Orbit clockwise on right arrow
    if(Angle < 0) Angle += 360; // Keep angle at 0 or above
}

// Update position
x = lengthdir_x(Orbit, Angle) + Center_X;
y = lengthdir_y(Orbit, Angle) + Center_Y;
You got it!!! thanks, I'll mess around with it little and let you know if I run into anything else, Thank you soo much!!
 
Top