Skip to content

Instantly share code, notes, and snippets.

@wizardofrobots
Created January 18, 2018 17:23
Show Gist options
  • Select an option

  • Save wizardofrobots/ca25935525dc44c7a3c422fc02b8f485 to your computer and use it in GitHub Desktop.

Select an option

Save wizardofrobots/ca25935525dc44c7a3c422fc02b8f485 to your computer and use it in GitHub Desktop.

Revisions

  1. RobotWizard created this gist Jan 18, 2018.
    64 changes: 64 additions & 0 deletions zigzag.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    const { vector, comm } = require("bytearena-sdk");
    const Vector2 = vector.Vector2;
    const agent = comm.connect();
    let count = 1
    const countLimit = 55
    let countDirection = 1
    let first = true
    let angle = 0
    agent.on("perception", perception => {

    const actions = []
    let p = 1
    let x = 0
    let y = 1

    let steering

    if (count === countLimit) {
    if (first === true) {
    if (angle != 0 ){
    angle = 0
    // console.log("zero")
    }
    else {
    // console.log("pi")
    angle = Math.PI
    }
    first=false
    }
    steering = navigate(perception, angle)
    // console.log(steering)
    if (steering.x == 0) {
    count = 1
    first = true
    }
    }
    else {
    count += countDirection
    steering = new Vector2(0, 1)
    }
    // console.log(count)
    actions.push({ method: "steer", arguments: steering.toArray() });
    agent.do(actions);
    });

    const navigate = (perception, angle) => {

    let steering = null
    let x, y
    let p = 1
    let error=angle-perception.azimuth
    if (Math.abs(error) > .01) {
    x =p * (angle - perception.azimuth)
    y = 1

    }
    else {
    x = 0
    }

    steering = new Vector2(x, y)
    return steering

    }