[SOLVED] Units smooth turning when about to leave screen

D

Duck

Guest
Hello guys, I need help again!

I'm trying to make a space game, where a unit circling back when they are about to leave to screen.
First off all here is what I got for the code:
------------------------------------------------------------------------------------
EnteringAngle = image_angle

if y <= 100 && EnteringAngle >= 90 //top
{
motion_add(EnteringAngle + 20, speed/10)
}
else if y <= 100 && EnteringAngle < 90
{
motion_add(EnteringAngle - 20 , speed/10)
}


if y >= room_height - 100 && EnteringAngle >= 270 //bottom
{
motion_add(EnteringAngle + 20, speed/10)
}
else if y >= room_height - 100 && EnteringAngle < 270
{
motion_add(EnteringAngle - 20 , speed/10)
}



if x <= 100 && EnteringAngle >= 180 //left
{
motion_add(EnteringAngle + 20, speed/10)
}
else if x <= 100 && EnteringAngle < 180
{
motion_add(EnteringAngle - 20 , speed/10)
}

if x >= room_width - 100 && EnteringAngle >= 0 //right
{
motion_add(EnteringAngle + 20, speed/10)
}
else if x >= room_width - 100 && EnteringAngle < 359
{
motion_add(EnteringAngle - 20 , speed/10)
}

------------------------------------------------------------------------------------

So I try to tell them if they are coming from the right towards the border, they should circle to the left,
if they are coming from the left side towards the border, they should circle to the right.

Mostly it works fine, except for the top and the bottom border, everytime when they should turn to the right side of the screen ( turning right from the top border or turning left from the bottom border), they just turn straight to the right and go on until they left the screen.
At the top border I never see them again, at the bottom border I can see them circling in and out of the screen and sometimes one of them just come back after I don't know how many rounds.
Occasionally they also having troubles at the edges and just leave.

I guess maybe something is wrong with my borders at 100 from each side?
So is my code somewhere at the right direction or completely wrong for this case?

Thank you in advance for the answers! :)

-Duck
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
I would do something completely different, tbh... First check to see if the instance is within the "border zone", then get the angle difference between the current direction and the center of the room... then rotate the instance until it is no longer inside the border zone. So:
Code:
var _left = 100;
var _right = room_width - 100;
var _top = 100;
var _bottom = room_height - 100;

if x < _left || x > _right || y < _top || y > _bottom
{
var _angto = point_direction(x, y, room_width / 2; room_height / 2);
var _dif = median(angle_difference(direction, _angto), -10, 10); /// Change -10/10 to the min/max turning speed
motion_add(direction + _dif, speed / 10);
}
Note that you may need to swap the angle_difference values around (as I always get the two mixed up!)...
 
D

Duck

Guest
Omg Nocturne, this works perfect!
But since I'm pretty new to all this, could you/ or someone else explain the following line to me?
--------------------------------------------------------------------------------
var _dif = median(angle_difference(direction, _angto), -10, 10);
--------------------------------------------------------------------------------
I quite don't understand it 100%



Thank you so much again! :)

-Duck
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Yay! Glad it works! Okay, so that line... first of all you are getting the difference in angles between the current direction and the direction towards the middle of the room. This will be a value between 0 and 180 or 0 and -180... BUT we don't want the ship to turn towards the middle of the room, we only want it to turn enough to exit the border zone, so we then use the MEDIAN function to clamp the value that the ship will turn to. So, if the angle difference is (for example) -16, then the the median(-10, -16, 10) will be (-10)... so we add -10 onto the direction then repeat the check the next step, which would be median(-10, -6, 10), so this time we add (-6) on, etc.... until the ship is out of the border zone and will perform the check no longer.

This method is great for making ANYTHING slowly rotate towards a point, btw, and can be used for all sorts of things like turrets on tanks, or AI, etc... :)
 
D

Duck

Guest
Thank you again for explaining it to me, I can understand it now! :)
 
Top