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
Parameters
| Name | Type | Default | Meaning |
|---|---|---|---|
| mag | number | 0 | The length or speed of the vector (magnitude). |
| dir | number | 0 | The 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.