HaxeFlixel Logo HaxeFlixel Snippets
  • About
  • Back to HaxeFlixel.com
    • Screen
    • Adding
    • Alive
    • Health
    • Making Sprites
    • Loading Sprites
    • Animation
    • Alpha
    • Color
    • Facing
    • Scale
    • Baked Rotations
    • FlxText
    • FlxBitmapText
    • Velocity
    • Acceleration
    • MaxVelocity
    • Gravity
    • Jumping
    • Angle
    • Angular Velocity
    • Angular Acceleration
    • Basic Group
    • Typed Group
    • Recycling
    • Sorting
    • Tileblock
    • Tilemap
    • Ray
    • tileProperties
    • Autotiles
    • Simple Overlap
    • Overlap Callbacks
    • 1-to-1 Collision
    • Immovable
    • Tilemap Collision
    • Moving Platforms
    • Flash
    • Fade
    • Shake
    • Follow
    • scrollFactor
    • Basics
    • Keyboard
    • Gamepad
    • Mouse
    • Button
    • FlxSound
    • FlxState
    • FlxSubState
    • Tween
    • Angle
    • Color
    • Motion
    • Num
    • Callbacks
    • FlxTimer
    • FlxSignal
    • moveTowards
    • Distance
    • velocityFromAngle
    • timeScale
    • drawLine

velocityFromAngle

velocityFromAngle will return a FlxPoint with the x and y velocity based on an angle and a speed.

Set an object’s velocity to these values to move that object (or use it for something else)

// determine the velocity based on angle and speed
velocity = FlxVelocity.velocityFromAngle(angle, speed);
Note: There is a simlar accelerateFromAngle function that will actually move an FlxSprite based on an angle, acceleration, and maxSpeed.

Demonstration

Source

View Source on GitHub

package;

import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.math.FlxMath;
import flixel.math.FlxVelocity;
import flixel.text.FlxText;
import flixel.tweens.FlxTween;
import flixel.util.FlxColor;

class PlayState extends FlxState
{
	private var statusText:FlxText;
	private var angle:Float = 30;
	private var sprite:FlxSprite;

	override public function create()
	{
		bgColor = 0;

		sprite = new FlxSprite("assets/sprite.png");
		sprite.screenCenter();
		add(sprite);

		statusText = new FlxText();
		statusText.size = 16;
		statusText.text = "angle: 0";
		statusText.autoSize = false;
		statusText.wordWrap = false;
		statusText.fieldWidth = FlxG.width;
		statusText.color = FlxColor.BLACK;
		statusText.setBorderStyle(FlxTextBorderStyle.SHADOW, FlxColor.GRAY, 2, 1);
		statusText.alignment = FlxTextAlign.LEFT;
		statusText.x = statusText.y = 10;
		add(statusText);

		moveSprite();

		super.create();
	}

	private function moveSprite():Void
	{
		if (angle < 0)
		{
			angle += 360;
		}
		else if (angle > 360)
		{
			angle -= 360;
		}
		sprite.velocity.copyFrom(FlxVelocity.velocityFromAngle(angle, 200));
		statusText.text = "angle: " + Std.string(FlxMath.roundDecimal(angle, 2));
	}

	override public function update(elapsed:Float):Void
	{
		super.update(elapsed);

		if (sprite.x < 0)
		{
			sprite.x = 0;
			angle = 2 * 90 - angle;
			moveSprite();
		}
		else if (sprite.x > FlxG.width - sprite.width)
		{
			sprite.x = FlxG.width - sprite.width;
			angle = 2 * 270 - angle;
			moveSprite();
		}

		if (sprite.y < 0)
		{
			sprite.y = 0;
			angle = 2 * 180 - angle;
			moveSprite();
		}
		else if (sprite.y > FlxG.height - sprite.height)
		{
			sprite.y = FlxG.height - sprite.height;
			angle = 2 * 0 - angle;
			moveSprite();
		}
	}
}

Tags

movement math utilities
Powered By HaxeFlixel Logo HaxeFlixel