Rotate and move a sprite around a point



  • Hi ! First, sorry if there are English mistakes, I'm French, so, it's not my mother language you know...

    I have some troubles making a portage of my last game never released.

    I'm trying to remake this :
    https://twitter.com/GLOW_Shana/status/750823403196706821

    I've done it by using Phaser, and i'm trying to remake it by using Flixel this time. But, in Phaser, it was very easy to move a sprite around a point and to make it facing the sprite.
    I saw "FlxPoint" and the "rotate" method, so I used it, but it's pretty weird... I'm setting the positions of my sprite on a sprite (a circle basically), so, normally, its top left point should be everytime on the circle, no ? My circle's got a radius of 64, so it's a perfect circle, but when it rotates, It goes beyond the edges...

    I'm using this (On Player extends FlxSprite) :
    ....
    centerPoint = new FlxPoint(640 / 2, 360 / 2);
    .....

    var p : FlxPoint = this.getScreenPosition();
    var np : FlxPoint = p.rotate(this.centerPoint, 2 );
    this.setPosition(np.x, np.y);

    Thanks for your answers.



  • The easiest way I can see is using 2 cameras, one for the game itself and the UI (gamepad) and change the first FlxCamera::angle.
    You will want to make the first camera bigger and center it.

    Here's how to create a new camera:

    var gameCam = new FlxCamera();
    var uiCam = new FlxCamera();
    FlxG.cameras.add(uiCam);
    
    // set default cameras or everything will be rendered twice (on uiCam, too)
    FlxCamera.defaultCameras = [gameCam];
    

    Your way is OK but you can improve it like this:

    // get a FlxPoint from the pool
    // FlxG.width and FlxG.height for the game/screen height
    var centerPoint = FlxPoint.get(FlxG.width / 2, FlxG.height / 2);
    
    // getScreenPosition() ...gets the position on the screen
    // getPosition() gets the position in the world even if the camera moves
    var p : FlxPoint = this.getPosition(); 
    p.rotate(this.centerPoint, 2); // no need to create np
    this.setPosition(p.x, p.y)
    p.put(); // put it back to the pool
    


  • Ok thank you, it finally works. It was a problem about origin point in fact ^^ ...

    Thank you :) !



  • Just curious, with which way did you solve your problem?


Log in to reply
 

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.