Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package {
- public class MathExtras {
- /* MathExtras
- A bunch of useful/commonly used maths functions that can easily be accessed from this class
- */
- public function MathExtras() {
- }
- // degsToRads(number) - converts a degrees angle to a radian angle
- public function degsToRads(degrees:Number):Number {
- return degrees * Math.PI / 180;
- }
- // radsToDegs(number) - converts a radians angle to a degrees angle
- public function radsToDegs(rads:Number):Number {
- return rads / (Math.PI / 180);
- }
- // normaliseAngle(angle) - normalises an angle so it always falls between 0 and 360
- public function normaliseAngle(angle:Number){
- var temp = angle
- while(temp < 0){
- temp += 360
- }
- while(temp > 360){
- temp -= 360
- }
- return temp
- }
- // getDist(obj1,obj2) - returns the distance between 2 objects
- public function getDist(firstObject:Object,secondObject:Object) {
- var distance:Number = Math.sqrt( ( firstObject.x - secondObject.x ) * ( firstObject.x - secondObject.x ) + ( firstObject.y - secondObject.y ) * ( firstObject.y - secondObject.y ) );
- return distance;
- }
- // angleToObject(obj1,obj2) - returns the angle which object1 needs to rotate to point face object2
- public function angleToObject(obj:Object,obj2:Object):Number {
- var dX:Number = obj2.x - obj.x
- var dY:Number = obj2.y - obj.y
- var angleDeg:Number = radsToDegs(Math.atan2(dY,dX));
- return angleDeg;
- }
- // roundToDecPlaces(number,places) - rounds the number to x decimal places
- public function roundToDecPlaces(number:Number,places:int){
- var val:int = number * Math.pow(10,places)
- return val / Math.pow(10,places)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement