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 from sprites

Problem it solves

You have an enemy boss that needs to aim and shoot a missile directly at the player, but you don't know the exact angle or distance between them.

What this block does

Creates a vector pointing from the first sprite directly to the second sprite. The magnitude is set to the distance in pixels between them, and the direction is the angle in degrees pointing at the target.

The block

Block ID: vectormath_create_vector_from_sprites

create vector from sprite %spriteFrom to sprite %spriteTo block

Parameters

NameTypeDefaultMeaning
spriteFromSpritenullThe starting sprite where the vector begins.
spriteToSpritenullThe target sprite where the vector points.

Returns

Vector — A Vector object pointing from the source sprite toward the target sprite, with magnitude equal to the distance between them.

Example

let enemy = sprites.create(img`.`, SpriteKind.Enemy)
let player = sprites.create(img`.`, SpriteKind.Player)
let aim = vectorMath.createVectorFromSprites(enemy, player)
let bullet = sprites.createProjectileFromSprite(img`.`, enemy, aim.x, aim.y)

Add the extension

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