# Orbiting Controls [SOLVED]

Discussion in 'Programming' started by Karrlem, Sep 27, 2016.

Tags:
1. ### KarrlemMember

Joined:
Sep 27, 2016
Posts:
28
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.

2. ### obsceneMember

Joined:
Jun 21, 2016
Posts:
2,445
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.

3. ### KarrlemMember

Joined:
Sep 27, 2016
Posts:
28
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?

4. ### NightFrostMember

Joined:
Jun 24, 2016
Posts:
1,977
You need an item to orbit clockwise when a button is pressed, and counterclockwise when another button is pressed? you can read keyboard with keyboard_check.

5. ### KarrlemMember

Joined:
Sep 27, 2016
Posts:
28
yeah I just don't know what values to put.

6. ### zircherMember

Joined:
Jun 21, 2016
Posts:
380
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;

7. ### NuxMember

Joined:
Jul 7, 2016
Posts:
372
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;

```

Karrlem likes this.
8. ### KarrlemMember

Joined:
Sep 27, 2016
Posts:
28
Ok cool, now i'm just trying to figure out how to get it to move using lengthdir_x/y

9. ### KarrlemMember

Joined:
Sep 27, 2016
Posts:
28
Thank you! I will definitely use this on one of my future projects!

Nux likes this.
10. ### NuxMember

Joined:
Jul 7, 2016
Posts:
372
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:
Awesome! it'll be cool to see what you come up with. ;o;

11. ### NightFrostMember

Joined:
Jun 24, 2016
Posts:
1,977
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.

Karrlem likes this.

Joined:
Sep 27, 2016
Posts:
28
13. ### KarrlemMember

Joined:
Sep 27, 2016
Posts:
28
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!!