Rotate matrix by mouse position.

Hey guys I have been banging my head against this problem for the past few hours.  I have a rectangle object created, I want the rectangle's rotation to follow my mouse cursor. ( I am easily able to do this).  Where things get hard is that I want the rectangle to rotate around it's bottom right corner, to do this I've found that I need to use a matrix transformation.  At this point I am able to have my rectangle rotate around the bottom right point, but I cannot figure out how to have it follow my mouse cursor as well while rotating.  The reason being that with a matrix you can only tell it a amount of radians to rotate (matrix.rotate) rather than just being able to set its current rotation.... Here is my code
var point:Point=new Point(myRectangle.x+myRectangle.width/2, myRectangle.y+myRectangle.height/2);
function rotateObject( event:Event ):void
          var m:Matrix=myRectangle.transform.matrix;
                              m.tx -= point.x;
                              m.ty -= point.y;
                              m.rotate (45*(Math.PI/180));
                              m.tx += point.x;
                              m.ty += point.y;
                              myRectangle.transform.matrix=m;
addEventListener( Event.ENTER_FRAME, rotateObject);
So with the current code it is constantly rotating around the bottom right corner, I have tried changing the "45" to "Math.atan(mouseX/mouseY), but again it seems that would only work if i could set the rotation of the matrix to that rather than telling it how much to rotate by....
Any thoughts or ideas would be much appreciated, thanks!

can you not calculate the new rotation based on the mouse position and the rotate by the difference with the current rotation on your rectangle?
function rotateObject( event:Event ):void
          var newRot:Number = Math.atan(mouseX/mouseY);
          var rotationDelta:Number = myRectangle.rotation - newRotation
          var m:Matrix=myRectangle.transform.matrix;
                              m.tx -= point.x;
                              m.ty -= point.y;
                              m.rotate (rotationDelta*(Math.PI/180));
                              m.tx += point.x;
                              m.ty += point.y;
                              myRectangle.transform.matrix=m;

Similar Messages

  • Bitmap Rotation According to Mouse Position?

    Hi,
    I am working on a 2d computer graphics project, and I need a good function to rotate a Bitmap 360 degree according to my mouse position.
    for example: 

    Hi,
    I am working on a 2d computer graphics project, and I need a good function to rotate a Bitmap 360 degree according to my mouse position.
    for example: 
    Hello,
    To rotate that image, we need to deal with the following tips.
    1. The image size.
    If the area for that image rotated is not big enough, it will just lose some parts of that image.
    Here is a nice code shared in this thread
    http://stackoverflow.com/questions/5199205/how-do-i-rotate-image-then-move-to-the-top-left-0-0-without-cutting-off-the-imag/5200280#5200280.
    private Bitmap RotateImage(Bitmap b, float Angle)
    // The original bitmap needs to be drawn onto a new bitmap which will probably be bigger
    // because the corners of the original will move outside the original rectangle.
    // An easy way (OK slightly 'brute force') is to calculate the new bounding box is to calculate the positions of the
    // corners after rotation and get the difference between the maximum and minimum x and y coordinates.
    float wOver2 = b.Width / 2.0f;
    float hOver2 = b.Height / 2.0f;
    float radians = -(float)(Angle / 180.0 * Math.PI);
    // Get the coordinates of the corners, taking the origin to be the centre of the bitmap.
    PointF[] corners = new PointF[]{
    new PointF(-wOver2, -hOver2),
    new PointF(+wOver2, -hOver2),
    new PointF(+wOver2, +hOver2),
    new PointF(-wOver2, +hOver2)
    for (int i = 0; i < 4; i++)
    PointF p = corners[i];
    PointF newP = new PointF((float)(p.X * Math.Cos(radians) - p.Y * Math.Sin(radians)), (float)(p.X * Math.Sin(radians) + p.Y * Math.Cos(radians)));
    corners[i] = newP;
    // Find the min and max x and y coordinates.
    float minX = corners[0].X;
    float maxX = minX;
    float minY = corners[0].Y;
    float maxY = minY;
    for (int i = 1; i < 4; i++)
    PointF p = corners[i];
    minX = Math.Min(minX, p.X);
    maxX = Math.Max(maxX, p.X);
    minY = Math.Min(minY, p.Y);
    maxY = Math.Max(maxY, p.Y);
    // Get the size of the new bitmap.
    SizeF newSize = new SizeF(maxX - minX, maxY - minY);
    // ...and create it.
    Bitmap returnBitmap = new Bitmap((int)Math.Ceiling(newSize.Width), (int)Math.Ceiling(newSize.Height));
    // Now draw the old bitmap on it.
    using (Graphics g = Graphics.FromImage(returnBitmap))
    g.TranslateTransform(newSize.Width / 2.0f, newSize.Height / 2.0f);
    g.RotateTransform(Angle);
    g.TranslateTransform(-b.Width / 2.0f, -b.Height / 2.0f);
    g.DrawImage(b, 0, 0);
    return returnBitmap;
    2. The location of that control which displays that image.
    If we use a picturebox, and set its sizemode to autosize like the following line, then if you just want to rotate that image to show, and you don't want to that affects the original image, then we need to keep its center point.
    this.pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
    We could add resize event handler after we set image for that picturebox.
            Point pOrign;
            Size sOrign;private void Form1_Load(object sender, EventArgs e)
    this.pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
    Image img = Image.FromFile(@"D:\Documents\New folder\New folder\TestImage.PNG");
    this.pictureBox1.Image = img;
    this.pictureBox1.InitialImage = img;
    pOrign = new Point(this.pictureBox1.Left , this.pictureBox1.Top );
    sOrign = new Size(this.pictureBox1.Width, this.pictureBox1.Height);
    this.pictureBox1.BorderStyle = BorderStyle.FixedSingle;
    this.pictureBox1.Resize += pictureBox1_Resize;
    private void pictureBox1_Resize(object sender, EventArgs e)
    this.pictureBox1.Left = this.pOrign.X + (this.sOrign.Width - this.pictureBox1.Width) / 2;
    this.pictureBox1.Top = this.pOrign.Y + (this.sOrign.Height - this.pictureBox1.Height) / 2;
    3. The angle between that center point and your mouse postion.
    We could get that value inside the picturebox's container's mouse_move event.
    Double angleNew ; private void pictureBoxContainer_MouseMove(object sender, MouseEventArgs e)
    angleNew = Math.Atan2(this.pOrign.Y + this.sOrign.Height / 2 - e.Y, this.pOrign.X + this.sOrign.Width/2 - e.X) * 180.0 / Math.PI;
    But when to start rotate that image, it should be your chooice, and you could decide when rotate that image with the following line.
    this.pictureBox1.Image = (Image)RotateImage(new Bitmap(this.pictureBox1.InitialImage), (float)angleNew);
    If you just want to save that change to that image, then you could save that bitmap to file directly.
    Happy new year.
    Regards.
    Carl
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • How to get an Image to rotate according to the position of the mouse in Muse?

    Hey guys,
             I'm designing a home page that is centered around a logo. I need the logo to rotate around 360 degrees as the mouse explores the area around it. I'm attaching a quick sketch explaining the type of motion I need, a mockup of what the page might look like, and a .png file I'll be using for the logo. Please give detailed instructions, as I am somewhat new to Muse. Thanks in advance, any help is MUCH appreciated!

    UPDATE: The movement can be made very simply in Adobe Edge Animate through the following process:
    (and then published as an Animate Deployment Package and inserted into Muse as a .oam file.)
    "   Based on this stack exchange response Edit fiddle - JSFiddle
    Place the provided script in compositionReady and change the var img reference to the name of your element.
    example: var img = sym.$("WirelosLogoTight");     "
    - Heathrowe
      Hope this helps anyone else with the same question. Here is a link to that post: How to create an animation that rotates in response to the mouse position?

  • Does rotation matrix keeps perpendicularity of two vectors?

    I know my question is quite silly but let me explain my problem...
    During the execution of my program, I wrote my own "lookAt" function wich takes into parameters two 3D points (eye position and view position) and a up vector. Everything is working great but, at one time, when I need to rotate my whole thing, I get a strange mistake I don't know how to solve :
    I compute my two vectors (up vector and the view one (view - eye)), there I check this two vectors are perpendicular (v1.dot(v2) == 0). Then I multiply my two vectors by my rotation matrix and I get my two new vectors. But, when I check if the two vectors are perpendicular, v1.dot(v2) now equals to something like -x.xxxxxxxxxxxxxxxE-XX, XX like 16 or 17. I know the problem is coming from the precision of my numbers, but does someone knows how I can solve it to conserve the perpendicularity?
    I really need this perpendicularity because I need to compute a new transform Matrix and with my problem, this one is not congruent, so I get an error.
    Thanks!!

    i did the same and had an error too, post you calculation code and i will try to help if i can

  • Imagesnapshot.capturebitmapdata with rotation matrix

    Hi all,
    I am not able to use imagesnapshot with rotation matrix.
    I am using imagesnapshot to cpature bitmapdata from image.
    My image is rotated so I want to rotate bitmapdata as per rotation.
    Here is the code.
    var matrix:Matrix = new Matrix();
    matrix.rotate(degreesToRadians(90));
    var bitmapData:BitmapData = ImageSnapshot.captureBitmapData(image, matrix);
    This code is not working it gives error.
    any idea why it is giving error?

    Kglad,
    While the parent isn't being rotated, it used to:
    Group all of the shapes together
    Acts as a basis for a bitmap object
    The problem occurs when I align the parentMC to the bottom and right edges of the stage. If left where the true boundary of the parentMC exists, there's a huge gap between the visible part of the shapes and the edge of the screen. If I manually adjust the parenMC, the screen size of the background is enlarged, and any object that rely on the right edge for alignment slide off screen.
    The breakdown of the display objects looks like this:
    ScreenContainer
    Background Image Container
    Shapes Container (holds all of the puzzle shapes)
    Individual Shape Container (a movieclip that can be dragged or rotated--X and Y offset to rotate around center)
    Individual Shape Subcontainer (a movieclip holds the drag handles and the shape--X and Y offset)
    Handle at each vertex
    Shape
    Tool Area
    I want to rotate the Individual Shape Container (not the subcontainer) without offsetting the X and Y positions of it or the Subcontainer using a transform. My problem seems to be in assigning the center point during the transform.
    Does that make sense?

  • Zooming image from mouse position(like in  windows vista photo gallery)

    hello all;
    here's my situation, hope someone can help..
    i wanna Zoom an image, which zoom from my mouse position
    like in windows photo gallery in windows vista
    so i do this..
    g2.translate(iMoux,iMouy);       
            g2.scale(zoom, zoom);       
            g2.translate(-iMoux,-iMouy);       
            g.drawImage(icon.getImage(), iSposx, iSposx, d.width/2-iValue, d.height-iBawah, null);
            g.drawImage(icon2.getImage(), d.width/2, iSposy, d.width/2-iValue, d.height-iBawah, null);the problem come when i move my mouse to the different location (like from top right to bottom left)
    the zoom image displayed in the screen like jump from latest location to new location
    anybody can help me...a clue how to do that?
    thx appreciate your help guys
    mao
    Edited by: KingMao on 31 Mei 08 14:27

    Hi Frank.
    Thanks for the response.
    Agreed, the pertinent question is why can't my colleague edit the JPG exported by Aperture. It's probably also worth pointing out, the same problem occurs with JPGs exported from iPhoto.
    The Windows software usually plays nicely with JPGs by all acounts, just not the ones I send - which I do via eMail or my public space on Mobile Me incidently.
    So, another key question is: all settings being equal (color profile, quality, etc.) are the JPGs as produced by iPhoto and Aperture indistinguishable from those produced by other apps on other platforms - i.e. does the use of JPG enforce a common standard?
    If that is the case, I suspect ours might be a permissions issue.
    According to the Microsoft support page on editing in Windows Live Photo Gallery, the inability to edit a picture is commonly caused by unsupported file type, or read-only attribute set on the file.
    Unfortunately, he and I are not in the same place, and he's not particularly au-fait with this type of problem solving. Hence, before involving him, I'd like to know:
    1. it's possible (i.e. someone else does it), and,
    2. what's involved (at my end and/or his).
    Thanks again,
    PB

  • System not sensing mouse position

    Starting today, my computer (iMac G5 PPC) no longer senses the position of my mouse pointer. For instance:
    - The dock doesn't magnify when the mouse is in it
    - None of the corners activate things like screensaver, expose, desktop
    - When I select a menu in the menu bar, moving the mouse over another menu item doesn't open that menu.
    In other words, OSX simply has no clue where my mouse pointer is located as I move it for all those neat automatic things. Yet, I can still use the mouse to click on items to make them work, then OSX knows where the mouse is.
    I've rebooted several times to no effect, but did notice something. As apps start up, the instant a window opens, OSX will sense the mouse position for that brief moment. For example, putting the mouse over the dock doesn't magnify, but when an app window appears, the dock will magnify that very instance. Moving the mouse out of the dock area doesn't unmagnify the dock, it's stuck in that state until either another window opens, causing OSX to sense the new position, or I click any mouse button which all gets OSX's attention and senses the mouse position.
    The pointer never changes when I'm over things like Links.
    Strange stuff. The mouse moves fine, it's just not being detected as it moves.
    Now, yesterday I installed a new ScanSnap scanner with the ScanSnap Manager software and Acrobat Standard ver. 7. But the mouse didn't have this behavior yesterday, or even this morning. This strange problem just appeared out of the blue mid-day today.
    I've since used Disk Utility and Onyx, and re-installed the 10.4.9 update. I switched to another user account, no difference, so it's definitely system wide.
    This doesn't stop me from using my iMac, but I just don't know how to fix this mouse issue besides a OSX re-install.

    Ok everybody, I finally got proper behavior, it's just too bad I'm not sure of the procedure.
    When I go to bed I usually run the mouse pointer to the upper left corner to activate the screensaver. Of course it didn't work. But I also knew that clicking a button causes the mouse to be sensed, so I was pushing some of the buttons while in the corner to see if I could get the screensaver going.
    Well, after doing this, everything is back to normal operation again. So, I fixed it totally by accident. Makes me wonder if there's some features that are turned on and off somehow by doing whatever it was I did up in that corner.
    Just so you know, I use a Logitech Cordless Optical Trackman. I swapped to the original Apple corded mouse but that made no difference.
    I never had a chance to try the suggestions since I just read them before posting this.
    Anyway, I hope I don't do again whatever it was that caused the behavior, but I am going to store this info in DEVONthink just in case something like this happens again.
    Anyway, it would still be interesting if someone could shed some light on this fix I accidently found. Perhaps some hidden capabilities/features? Or just dumb luck?

  • I created a PDF of my book in Preview and encrypted it. When opened in Adobe Reader, it is rotated to the vertical position rather than the original landscape position. How do I fix this in Preview? I don't want my buyers to have to hassle with rotating.

    I created a PDF of my book in Preview and encrypted it. When opened in Adobe Reader, it is rotated to the vertical position rather than the original landscape position. How do I fix this in Preview? I don’t want to hassle my buyers with rotating. There was no option in Preview to account for this automatic and unwanted rotation.

    Then you rotate it and SAVE or SAVE AS and it will remain that way.

  • MAC OS X + stage.fullScreenSourceRect + renderMode set to GPU = problem with mouse position

    When setting stage.fullScreenSourceRect, stage.displayState to StageDisplayState.FULL_SCREEN_INTERACTIVE; and renderMode to GPU, mouse position is read incorrectly. This is not only for mouseX and mouseY position, but also all the mouse interactions with Sprites, Buttons etc are not working correctly.
    Anybody know a solution for this issue?
    Here is the bug reported, but there is no response yet.
    https://bugbase.adobe.com/index.cfm?event=bug&id=3486120
    Greg.

    Bump up.
    Anybody has the same problem and have an idea how to fix it? Or please just check if you have the same problem.. I'm going to submit my game "Amelia and Terror of the Night" (successfully added to iOS store) to MAC App Store but can't do it while this problem appears.
    I am disappointed nobody  even verified the bugs I submitted at  the bugbase.adobe.com for AIR 3.5 and 3.6
    thanks
    Greg

  • Hwo to "track the mouse position"

    I m a java newbie, and need to write a small program to track the mouse position.
    basically, it opens a small window, and tell you the current mouse x, y coordinate. when mouse moves, hte x, y changes...
    by the way, how can i compile it to .exe file, after i finished coding.
    i try this way, but so many errors.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class mouseWin {
    public static void main(String[] args)
              protected int last_x=0, last_y=0;
              mouseMoved(MouseEvent e);
    JFrame frame = new JFrame("Mike's Mouse");
    JLabel X = new JLabel("x: " + last_x + "y: " + last_y);
    frame.getContentPane().add(X);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
         public mouseMoved(mouseEvent e)
              last_x = e.getx();
              last_y = e.gety();

    please help.
    i rewrite the code, but still cannot get it work
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.EventListener;
    public class mouseWin implements MouseMotionListener
    public static void main(String[] args)
                   int last_x;
                   int last_y;
                   last_x = 0;
                   last_y = 0;
                   JFrame frame = new JFrame("Mike's Mouse");
                   JLabel X = new JLabel("x: " + last_x + "y: " + last_y);
              frame.getContentPane().add(X);
                   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   frame.pack();
                   frame.setVisible(true);
         public void mouseMoved(MouseEvent e)
              last_x = e.getx();
              last_y = e.gety();
              JLabel X = new JLabel("x: " + last_x + "y: " + last_y);
    }

  • Mouse position on screen

    is there any way to get the mouses position on the screen (not a window)?
    Stephen

    Component c = (Component)e.getSource();
    Point p = e.getPoint();
    SwingUtilities.convertPointToScreen(p, c);
    System.out.println(p);

  • Mouse position capturing

    I was wondering if there is a way to capture the mouse position w/o the use of a frame or jframe? I already know how to use the MouseListener on the JFrame/Frame, but that is no help to me since I'm not using a frame at all. Any suggestions????

    What is your goal. This will work -- (the window is 1 pixel!) -- because the robot starts a "drag" and
    swing has mouse capture during drag, but as soon as the use clicks, it's over...
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class X {
        public static void main(String[] args) throws AWTException {
            JWindow w = new JWindow();
            Container cp = w.getContentPane();
            cp.addMouseMotionListener(new MouseMotionListener() {
                public void mouseDragged(MouseEvent e) {
                       System.out.println(e.getPoint());
                public void mouseMoved(MouseEvent e) {
            w.setBounds(new Rectangle(0,0,1,1));
            w.setVisible(true);
            Robot rob = new Robot();
            rob.mouseMove(0,0);
            rob.mousePress(InputEvent.BUTTON1_MASK);
            rob.mouseMove(100,100);
    [/code[

  • Detect Mouse Position?

    I can't (within actionscript 3) determine where the mouse is
    relative to the flash player window.
    I can get stage.mouseX and stage.mouseY but those coordinates
    are in relation to the size of the entire ORIGINAL stage at compile
    time.
    I need to know where the mouse is in relation to the window
    playing the swf, which is significantly smaller than the original.
    That is the play screen has been resized.
    If I zoom way in and scroll to the right edge of the stage
    and mouseover a spot on the right edge, then trace the mouse
    position (in a mouseover event proc) it shows me a number that is
    the width of the ORIGINAL stage. It seems to be completely
    oblivious to the play window.
    Is there any way at all to determine where the mouse is
    relative to the play window?
    For example,
    if the original stage is 1350 and the play window is 540 and
    I'm zoomed WAY in and scrolled all the way to the right and
    I put the mouse directly in the middle of the window,
    I need something that tells me (in actionscript 3) that the
    mouse's x coordinate is at 270, NOT 12XX (somewhere near the end of
    the orginal size of the stage).
    Is this possible in Flash C3??

    Hi there!
    I don´t know if it´s right but have you tried
    localX
    localY
    from the occuring MouseEvent?
    Hope that helps :)
    CU
    Ingo

  • Convert mouse position to image position

    Using a zoomed IMAQ image in an image control, I want to recover the position of the mouse in the co-ords of the underlying image. LastMousePositionX (and Y) give the info I want but they are one click in the past and not the current position. Amazingly, there doesn't appear to be an equivalent CurrentMousePositionX. I can't seem to find the info I need to do the conversion manually. The mousedown event returns the current mouse position and I can get the position in the image container by subtracting off its left and top position. But to convert this to the image position I need to know both the zoom and where the images origin is relative to what is displayed. I can't seem to find that origin position in the list of property nodes. The only thing I can seem to come up with is to extract the info from teh image information string, but this seems like an incredibly poor way to extract such obvious information - espeically since the info must be calculated to put in the string anyway. Can anyone help with this?

    Hi,
    I am confused why you get the mouse position one click in the past because it seems to work ok for me. I've had to use four of the image properties to recreate the effect using the mousemove event, but this seems to give the same values as the Last Mouse Position property so I think it does what you want. I couldn't see an origin property so I did the conversion using the position given at the image center. I wanted to post a snippet here but I'm new to posting and couldn't seem to get it to work for the whole block diagram, so I'm using a screenshot instead. I hope this helps in some way.
    Will
    Attachments:
    mouse position test.vi ‏58 KB

  • Get mouse position during Custom UI Draw event?

    I am building a Custom UI for one of my plugin effect.
    I want to get the mouse position during the Custom UI Draw event (PF_Event_DRAW), but cannot manage to get it.
    From what I understand, it seems we can only get the mouse position during the events PF_Event_DRAG, PF_Event_DO_CLICK, and PF_Event_ADJUST_CURSOR.
    I tried to use PFAppSuite4::PF_GetMouse() too, but this resulted in the error message "After Effects error: internal verification failure, sorry! {PF_GetMouse can only be called during valid events.}".
    Therefore I want to store the mouse position during PF_Event_ADJUST_CURSOR, and use it later during PF_Event_DRAW.
    Where can I store those values in a "clean" way (ensuring there will not be conflicts if two instances of the same effect are running, etc)?
    Or is there an easier method to get the mouse position during a Custom UI Draw event?

    //SequenceDataVars is the name of your data structure
    //this is how you create the sequence data
    out_data->sequence_data = PF_NEW_HANDLE(sizeof(SequenceDataVars));
    SequenceDataVars *seqData =
    (SequenceDataVars*)PF_LOCK_HANDLE(out_data->sequence_data);
    //and now you can store stuff in it.
    seqData->var1 = 7;
    //after it's created, on other calls, you just do a simple cast
    SequenceDataVars *seqData = *(SequenceDataVars **)out_data->sequence_data;
    seqData->var1 = 100;
    //this is how you delete it. (if you don't delete it, it will be saved with
    the project, and loaded on the next session. (if you want)
    PF_DISPOSE_HANDLE(out_data->sequence_data);
    there's more to know, so look up SequenceSetup, SequenceSetdown, ect in the
    sample project.

Maybe you are looking for

  • How do I display a list of all emails without the thread indetations?

    I must have accidentally clicked the wrong thing. Now one of my email folder listings has the emails with the same subject line all indented in what I suppose is the THREAD format. I want to keep it simple and just list them in date/time sequence. Ho

  • Asset capitalization date not appropriate

    Hi,, Please tell me while I run Asset settlement through KO88 in period 7 ie, 22October 2010 asset capitalization and first acquisition date become 31.10.2010 instead of 22.10.2010. Because I executed asset settlement through KO88 on 22.10.2010 which

  • Sony handycam video camera upload trouble

    I recently got a sony handycam, but mac doesn't support the program it comes with to upload videos, does anyone know what I can do so I can upload my videos onto my macbook pro?

  • Sychronisation of DAQ on multiple cards

    I am operating with Labview 5.1.1 and Flexmotion on Windows 98 and I am an intermediate user. Using PCI-6023E and PCI-7344 motion controller card, connected by an RTSI cable. Want a process whereby after running a subvi, DAQ begins at the same time o

  • Simple Video Management App

    Hi all I have a ton on family videos taken on my HD camera, they are most all of them short, 2-3 min .AVI files. I don't have time to edit them all and make full length movies (I have in the past with iMovie), now I just want a simple application tha