[SOLVED] Vector to Coordinate?

Is there a way to turn the vector into a coordinate? For example, I’m doing Cloudrip Siege, and I want to be able to build a decoy toward a specific side. Here’s my code now, but it doesn’t work:

function pos() {
    let build = null;
    if (sideLength() == "l" && hero.pos.y < 41) build = new Vector(23, 41);
    if (sideLength() == "r" && hero.pos.y > 41) build = new Vector(77, 41);
    return build;
}
...
if (hero.gold >= hero.costOf("decoy")) hero.buildXY("decoy", pos());

So more or less, I want to build at a point about three meters from my hero, but I don’t want to go all the way to the point.

So it would be smth like:

hero.buildXY("decoy", self.pos.x-3, self.pos.y)

or

hero.buildXY("decoy", self.pos.x, self.pos.y+3)

or

hero.buildXY("decoy", self.pos.x-3, self.pos.y+3)

and so on.

If I get you right)

Something like that. Actually, I already tried that, and I couldn’t pass the level.

I don’t understand… also, can you please show the sideLength function?

Here’s my sideLength() function:

function sideLength() {
    let leftLength = 0;
    let rightLength = 0;
    for (let enemy of hero.findEnemies()) {
        if (enemy.pos.x > 50) {rightLength += 1;}
        else {leftLength += 1}
    }
    if (leftLength > rightLength) {return "l";}
    else {return "r";}
}

If you go to the level, you’ll find that there’s a section in the middle of the arena where coins spawn. On either side of the arena, enemies spawn. There are also fences that separate the two areas except for a small portion in the center. I’m trying to build the decoys so that they move toward that opening (on the side with the most enemies).

Don’t see anything wrong ._.
Also, this looks better I think:

function sideLength () {
    let leftLength = 0;
    let rightLength = 0;
    for (let enemy of hero.findEnemies()) {
        if (enemy.pos.x > 50) rightLength++;
        else leftLength++;
    }
    if (leftLength > rightLength) return 'l';
    else return 'r';
}
1 Like

I don’t think anything is wrong with the code, I can’t use the buildXY() function with a vector, though, so I was wondering if there was a way to find an XY coordinate along that vector, though. I don’t know if it’s even possible, but I uploaded an illustration of what I am thinking.

wdym? like get a point along the line between 2 vectors, or what?

Well, maybe it’s better to define two points and direct your decoys to the point your function sideLength choose?

Why?

Exactly! (20 characters)

Maybe.

The buildXY() function requires three inputs: "type", an x coordinate and a y coordinate. I’m trying to get those x and y coordinates from a vector, but I can’t directly input a vector there.

Try

const d = (v1, v2) => ((Math.atan2(v2.x - v1.x, v2.y - v1.y) + Math.PI * 2) % (Math.PI * 2));
const c = (v, a, r) => (new Vector(v.x + Math.sin(a) * r, v.y + Math.cos(a) * r));
const point = (l, n) => (c(l[0], d(...l), n));

To get a point, just do something like

const point1 = new Vector(40, 35);
const point2 = new Vector(50, 35);
point([point1, point2], 5); // x: 45, y: 35

Edit: just tested it, works as intended

3 Likes

I can’t pretend to understand how you worked that, but it’s all I wanted it to be! Thanks!

(post deleted by author)

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.