Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Rotating vector

New Here ,
Apr 17, 2013 Apr 17, 2013

Hi guys!

Well, my problem is not really program-related, it's more math related (math was never my strong side) and I thought maybe someone here could help me out...

Lets says I have 2 points in the X-Y plane: A(180,180) , B(310,310), now I craete a vector from B to A: V=(-180 , -180).

Now, I need to rotate that vector around point B at 1 degree intervals (in an OnEnterFrame event).

I cant seem to figure out the math required...can someone please help me out?

Thank you!

TOPICS
ActionScript
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 17, 2013 Apr 17, 2013

use:

var A:Point = new Point(180,180);

var B:Point = new Point(310,310);

var angle:Number = Math.atan2(-130,-130);

var L:Number = Math.sqrt(130*130*2);

var nextX:Number = B.x+L*Math.cos(angle);

var nextY:Number = B.y+L*Math.sin(angle);

this.addEventListener(Event.ENTER_FRAME,f);

function f(e:Event):void {

    angle+=Math.PI/180;

    nextX = B.x+L*Math.cos(angle);

    nextY = B.y+L*Math.sin(angle);

}

Translate
Community Expert ,
Apr 17, 2013 Apr 17, 2013

what is your vector?  is it a movieclip?  is it a line drawn dynamically (using the graphics class)?  something else?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 17, 2013 Apr 17, 2013

The vector exsits only in the code, its a simple subtraction of 2 points, as detailed above.

point B is fixed, at the tip of point A there is an object (but is a child of the stage, the points exists only in the code, they are not objects), I need to update to location of point A and as a result to move the object.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 17, 2013 Apr 17, 2013

I need to achieve something resembels a clock - move A around B in a fixed radius circle

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 17, 2013 Apr 17, 2013

create an movieclip (like the minute hand of a clock) pointing to the right (0degrees) with registration point at its left middle and instance name arrow_mc:

you can then use:

arrow_mc.x=310;

arrow_mc.y=310;

arrow_mc.rotation=180*Math.atan2(-130,-130)/Math.PI;

arrow_mc.addEventListener(Event.ENTER_FRAME,rotateF);

function rotateF(e:Event):void{

arrow_mc.rotation++;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 17, 2013 Apr 17, 2013

Thanks kglad, but thats not really what I'm looking for, I know how to rotate a movie clip, that's not the case here (can't use rotation)

the only thing I need to do is to updtae to location of A, and I need to do it by updating it's x and y cords.

This is the code I'm currently using, maybe you can tell me what I'm doiing wrong:

first I define UnitVecX UnitVecY and VecNormal outside the Event:

UnitVecX =  A.x - B.x

UnitVecY = A.y - B.y

VecNormal = Math.sqrt(UnitVecX * UnitVecX  + UnitVecY  * UnitVecY );

in the Event itself:

angle = Math.atan2(UnitVecY , UnitVecX )*180/Math.PI +90;

angle = angle +1;

nextX = A.x + UnitVecX *  VecNormal *  Math.cos(angle * Math.PI/180);

nextY = A.y + UnitVecY *  VecNormal *   Math.sin(angle * Math.PI/180);

UnitVecX = (nextX - B.x) / VecNormal;

UnitVecY = (nextY - B.y) / VecNormal

finally (also in the event)

A.x = nextX ;

A.y = nextY;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 17, 2013 Apr 17, 2013

what kind of objects are A and B?

what's the displayobject that looks like a vector or arrow?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 17, 2013 Apr 17, 2013

A and B are basicly points: A(x,y) B(x,y) (they actually define proprties of a custome class in my project).
Think of them as just points in the plane...(there isnt really any arrow....)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 17, 2013 Apr 17, 2013

they are either points (in flash actionscript) or they are not.  if they are, that's fine but, they are not displayobjects.  if they are not points, are they objects? 

in either case, you don't appear to have any displayobject so nothing will be seen on stage.  is that what you want?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 17, 2013 Apr 17, 2013

A defines the location of a blitted object which is on stage (though I don't see how it matters).

Its not a problem of displaying them on the stage, it's a pure math problem; Consider A and B to be invisible points in the plane, and A need to circle B in a fixed radius.

It really is't even complex math, I'm missing something I can't seem to find what 😕

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 17, 2013 Apr 17, 2013

use:

var A:Point = new Point(180,180);

var B:Point = new Point(310,310);

var angle:Number = Math.atan2(-130,-130);

var L:Number = Math.sqrt(130*130*2);

var nextX:Number = B.x+L*Math.cos(angle);

var nextY:Number = B.y+L*Math.sin(angle);

this.addEventListener(Event.ENTER_FRAME,f);

function f(e:Event):void {

    angle+=Math.PI/180;

    nextX = B.x+L*Math.cos(angle);

    nextY = B.y+L*Math.sin(angle);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 19, 2013 Apr 19, 2013

Yay it works!

Thank you very much!!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 19, 2013 Apr 19, 2013
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines