Hello and welcome to our community! Is this your first visit?
Register
Results 1 to 3 of 3

Thread: AS3 Trig issue

  1. #1
    Veteran Enthusiast LeadingManNigel's Avatar


    Join Date
    May 2005
    Location
    New York City
    Posts
    6,213

    AS3 Trig issue

    I was writing a rotateToPoint for my math class when I ran into a bit of a bump. Rotationg works fine when the objects spawns facing up or to the right but this is not the case with down.
    Code:
    public static const radiansToDegress:Number = 180 / Math.PI;
    Code:
    public static function rotateToPoint(targetX:int, targetY:int, pointX:int, pointY:int,offSet:int):Number {
    			var tx:int = pointX - targetX;
    			var ty:int = (pointY - targetY);
    			var rad:Number = Math.atan2(ty, tx);
    			var angle:Number = rad * radiansToDegress;
    			var o:int = 0;
    			switch(offSet) {
    				case 0:
    				o = 90;
    				break;
    				case 1:
    				o = 0;
    				break;
    				case 2:
    				o = 0; // Dunno what to put here
    				case 3:
    				o = 0 // Same
    				break;
    				default:
    				o = 0;
    			}
    			angle += o;
    			return angle;
    		}
    Doc incase you have trouble understanding:
    Code:
    /**
    		 * 
    		 * @param	targetX The X value of who you want to rotate.
    		 * @param	targetY The Y value of who you want to rotate.
    		 * @param	pointX	The X value of object you want target to point to.
    		 * @param	pointY	The y value of object you want target to point to.
    		 * @param	offSet	Direction of object when spawned(0 = UP; 1 = RIGHT; 2 = DOWN; 3 = LEFT)
    		 * @return 	angle
    		 */
    Also rather than supporting only the four cardinal directions is there some sort of math wizardry to calculate the angle regardless of position? How do I maths?

  2. #2
    Veteran Enthusiast BEER BEER STRONG's Avatar


    Join Date
    Jun 2005
    Location
    I am the liquor
    Posts
    5,853

    Re: AS3 Trig issue

    I don't really know actionscript, but I would personally try and do it using matrices. Linear transformations are pretty good for rotating stuff, and I think AS has decent matrix capabilities. I kind of have a hard time telling what's going on in your code, so that's all I can really throw out right now. Could you explain a little bit more what exactly you're trying to do?
    [center:16hbm10o]

    He lost all his friends
    He didn't need his friends
    He lived with his mother and repeated himself[/center:16hbm10o]

  3. #3
    Veteran Enthusiast LeadingManNigel's Avatar


    Join Date
    May 2005
    Location
    New York City
    Posts
    6,213

    Re: AS3 Trig issue

    Im trying to rotate to a point regardless of the objects original orientation. Also I have failed pretty much every Math class beyond 4th grade so I have no idea what you're talking about.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •