How do I "launch" a FlxSprite from an angle, kind of like angry birds?
-
I'm new to HaxeFlixel and surprisingly, despite having a barebones physics engine, it's pretty functional and simple.
However, I want to figure out how I would launch a FlxSprite from an angle, this would have many uses and I can assure you I'm not making an angry birds clone.. haha .
-
Set
acceleration.y
to gravity value. Andvelocity.x
andvelocity.y
to projections of velocities on axes. To calculate those projections you can useFlxVector
class.
-
FlxVelocity offers some functions like velocityFromAngle. Here's how to use it:
sprite.velocity.copyFrom(FlxVelocity.velocityFromAngle(-45, 200)); // angle, speed
-
You can use:
-
FlxAngle.angleBetweenMouse(Object:FlxObject, AsDegrees:Bool = false);
or -
FlxAngle.angleBetweenPoint(Sprite:FlxSprite, Target:FlxPoint, AsDegrees:Bool = false);
for setting angle of object. -
Then set velocity from angle.
use FlxVelocity.velocityFromAngle();
-
Don't forget to set gravity (so the object can fall)
object.accerelation.y = 100; // gravity
-
-
@eminfedar said in How do I "launch" a FlxSprite from an angle, kind of like angry birds?:
You can use:
-
FlxAngle.angleBetweenMouse(Object:FlxObject, AsDegrees:Bool = false);
or -
FlxAngle.angleBetweenPoint(Sprite:FlxSprite, Target:FlxPoint, AsDegrees:Bool = false);
for setting angle of object. -
Then set velocity from angle.
use FlxVelocity.velocityFromAngle();
-
Don't forget to set gravity (so the object can fall)
object.accerelation.y = 100; // gravity
Now if I had a canon that was launching said FlxSprite in an upward direction, would the velocity be accounted for? I guess what I'm trying to say is, if I launched the object almost completely upward, would the velocity towards the left or right be minimal?
-
-
@Aideon if the user perfectly make 90 degrees between cannon and mouse, there will be no velocity at left or right. (because cos(90) equals 0 and the main velocity you entered will distribute by sin and cos values of the angle.
- sin90 = 1, cos90=0,
- so
velocity.y = sin90*mainvelocity;
=>1*velocity;
andvelocity.y
equalsmainvelocity;
velocity.x = cos90*mainvelocity;
=>0*velocity;
andvelocity.x
equals0;
// so there is no velocity at x`
-