MakeCode Games!

A fan-made community library of MakeCode Arcade games. Visit MakeCode Arcade to make a game!

← Back to extensions

Vector Math

Create and calculate 2D vectors to handle angles, distances, and directional velocity for sprites in MakeCode Arcade.

create vector

Problem it solves

You want to launch a fireball or move a spaceship at a specific speed in a chosen angle (such as 50 speed at a 45-degree angle), but you need the separate X and Y speeds to set its velocity.

What this block does

Creates a new vector with a given length (magnitude) and angle (direction in degrees). The vector automatically calculates its X and Y components so you can easily apply them to sprite velocities.

The block

Block ID: vectormath_create_vector

create vector with magnitude %mag and direction %dir block

Parameters

NameTypeDefaultMeaning
magnumber0The length or speed of the vector (magnitude).
dirnumber0The direction of the vector in degrees (0 is right, 90 is down).

Returns

Vector — A new Vector object containing the magnitude, direction, and Cartesian (x, y) coordinates.

Example

let bulletVector = vectorMath.createVector(100, 45)
let projectile = sprites.create(img`.`, SpriteKind.Projectile)
projectile.vx = bulletVector.x
projectile.vy = bulletVector.y

Add the extension

Add github:robo-technical-group/pxt-arcade-vector-math to your project, or open: Import pxt-arcade-vector-math.