Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //NormalizeAngle(angle).
- //Converts the passed in angle into an angle between 0 and 360, not including 360.
- //Examples: NormalizeAngle(540) = 180, NormalizeAngle(-270) = 90, NormalizeAngle(360) = 0.
- function NormalizeAngle(angle){
- angle %= 360;
- if(angle<0){ angle += 360; }
- return angle;
- }
- //AngularDistance(angle1, angle2).
- //Gets the smallest angle between angle1 and angle2. Will always return a number between -180 and 180, not including //-180. A positive number means the shortest way from angle1 to angle2 goes clockwise, a negative number means the //shortest way goes counterclockwise. Also uses the NormalizeAngle function from above.
- //Examples: AngularDistance(90, 180) = 90, AngularDistance(180, 90) = -90, AngularDistance(1, 359) = -2.
- function AngularDistance(angle1, angle2){
- let distance = NormalizeAngle(angle2 - angle1);
- if(distance>180){ distance-=360; }
- return distance;
- }
- //Function for objects: TurnToAngle.
- //Three Parameters, which are id, speed and angle.
- //Makes an object bullet/laser turn till it's angle is equal to the number input as the parameter angle. Doesn't loop //by itself. Speed is the speed it turns at. Inputting a negative value for speed gives you awkward results. Id is //(obviously) the id of the object.
- function TurnToAngle(id, speed, angle){
- let current_angle = Obj_GetAngle(id);
- let target_angle = angle;
- let adist = AngularDistance(current_angle, target_angle);
- if(absolute(adist) < speed){
- Obj_SetAngle(id, target_angle);
- }
- else {
- if(adist > 0){
- Obj_SetAngle(id, Obj_GetAngle(id)+speed);
- }
- else if(adist < 0){
- Obj_SetAngle(id, Obj_GetAngle(id)-speed);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment