# Direction angle to an object?

3 replies to this topic

### #1 jabbyjim

jabbyjim

GMC Member

• GMC Member
• 134 posts
• Version:Unknown

Posted 11 July 2012 - 08:30 PM

I basically have a space game to where there is a wormhole that has gravity in it. So ... I basically want the ship also on the screen to slowly gravitate to the object.

I tried "step torwards point" and even "move torwards point", and it works but ... the ships thrusting in other directions (to get away) do not work anymore. I want the wormhole to be pulling the ship constantly but not that hard (its not a tractor beam!!)

So its obvious I need to add gravity to the wormhole for the ship object. How on earth do I do that?

I assume I need to use set the gravity, but how can I tell the direction? How can I set the gravity of one object to the direction of another? Is there a find_direction script, or some quick formula I can use?

Thanks for any help!

Cheers!
• 0

### #2 TsukaYuriko

TsukaYuriko

GMC Member

• GMC Member
• 510 posts
• Version:GM:Studio

Posted 11 July 2012 - 08:46 PM

No need for gravity, and there is indeed a "find_direction"-like function. It is called point_direction. By using the functions lengthdir_x and lengthdir_y in combination with it, achieving the desired effect is possible:
```var dir, dist;
dir = point_direction(x, y, OBJECT.x, OBJECT.y);
dist = point_distance(x, y, OBJECT.x, OBJECT.y);
if (dist < FORCE) {
x = OBJECT.x;
y = OBJECT.y;
}
else {
x += lengthdir_x(FORCE, dir);
y += lengthdir_y(FORCE, dir);
}```
Replace OBJECT with the wormhole object's name (is there only one of these? If no, the above code will need slight adjustments, tell us if that's the case), and FORCE with the gravitation speed (in pixels per frame).

Edited by TsukaYuriko, 11 July 2012 - 08:46 PM.

• 1

GMC Member

• GMC Member
• 383 posts
• Version:GM8

Posted 11 July 2012 - 08:57 PM

You probably have some variables like "xVel" and "yVel" (horizontal and vertical veloctity) for the ship, right? I don't know how you're currently, moving
the ship, but here's one way:

```xVel += lengthdir_x(shipSpeed, shipDirection)
yVel += lengthdir_y(shipSpeed, shipDirection)```

shipDirection is the direction the ship is facing in (0-360) and shipSpeed is how fast the ship can be moved (for example, 2 if you go slow, 4 if you use full thrust, and so on).

Now, before you actually update the ship's position, calculate how much the wormhole is pulling you towards it. Suppose the wormhole starts to pull you in if you're closer than 200 pixels to it:

```if distance_to_object(objWormHole) < 200
{
pullDir = point_direction(x,y,objWormHole.x,objWormHole.y)
xVel += lengthdir_x(200-distance_to_object(objWormHole), pullDir)*multiplier
yVel += lengthdir_y(200-distance_to_object(objWormHole), pullDir)*multiplier
}
```

Here, you will be pulled harder the closer you are to the wormhole. I suggest you play around with the "multiplier" variable to get the pull strength
to what you want it to be (try small values like 0.5 first).

NOTE: This code will give you an error if there are no objWormHole objects in the room. In this case, just add the following check in the beginning:
`if instance_exists(objWormHole)`

NOTE 2: If there are multiple wormholes active that could affect you at the same time, you must do this check with all of them by using a structure like this:
In the ship's code:

```totalXVelChange = 0
totalYVelChange = 0
with objWormHole
{
if distance_to_object(objShip) < 200
{
pullDir = point_direction(objShip.x,objShip.y,x,y)
totalXVelChange += lengthdir_x(200-distance_to_object(objShip), pullDir)*multiplier
totalYVelChange += lengthdir_y(200-distance_to_object(objShip), pullDir)*multiplier
}
xVel += totalXVelChange
yVel += totalYVelChange
}```

And now, after you've calculated the effect of both the ship's own movement and the pull by the wormhole, we update the position of the ship:

```x += xVel
y += yVel```

I hope you get this to work! If not, don't hesitate to ask for more help! =)

Edited by Koaske, 11 July 2012 - 08:58 PM.

• 1

### #4 jabbyjim

jabbyjim

GMC Member

• GMC Member
• 134 posts
• Version:Unknown

Posted 12 July 2012 - 02:25 PM

thank you so much, all this information helped and I have it working really well. The point_direction is HUGE .. I didn't even know that existed. Now I can do even more crazy stuff.

Cheers!
• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users