Created
April 30, 2020 00:36
-
-
Save ksasao/df4deb14a437c21031d61932c35004ec to your computer and use it in GitHub Desktop.
toioで追っかけっこするやつ。公式のサンプルの chase/index.js を置き換えてください。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Original code: | |
| * Copyright (c) 2019-present, Sony Interactive Entertainment Inc. | |
| * | |
| * This source code is licensed under the MIT license found in the | |
| * LICENSE file in the root directory of this source tree. | |
| * | |
| * modified by ksasao | |
| */ | |
| const { NearScanner } = require('@toio/scanner') | |
| // calculate chasing cube's motor speed | |
| function chase(x1, y1, x2, y2, y2Angle) { | |
| const diffX = x1 - x2 | |
| const diffY = y1 - y2 | |
| const distance = Math.sqrt(diffX * diffX + diffY * diffY) | |
| if (distance < 50) { | |
| return [0, 0] // stop | |
| } | |
| let relAngle = (Math.atan2(diffY, diffX) * 180) / Math.PI - y2Angle | |
| relAngle = relAngle % 360 | |
| if (relAngle < -180) { | |
| relAngle += 360 | |
| } else if (relAngle > 180) { | |
| relAngle -= 360 | |
| } | |
| const ratio = 1 - Math.abs(relAngle) / 90 / 2 | |
| let speed = 40 | |
| if (relAngle > 0) { | |
| return [speed, speed * ratio] | |
| } else { | |
| return [speed * ratio, speed] | |
| } | |
| } | |
| async function main() { | |
| // start a scanner to find nearest two cubes | |
| const cubes = await new NearScanner(3).start() | |
| // connect two cubes (tom chases jerry) | |
| const jerry = await cubes[0].connect() | |
| const tom = await cubes[1].connect() | |
| const spike = await cubes[2].connect() | |
| // set light color and store position | |
| let jerryX = 0 | |
| let jerryY = 0 | |
| let jerryAngle = 0 | |
| jerry.turnOnLight({ durationMs: 0, red: 255, green: 255, blue: 255 }) | |
| jerry.on('id:position-id', data => { | |
| jerryX = data.x | |
| jerryY = data.y | |
| jerryAngle = data.angle | |
| }) | |
| // set light color and store position | |
| let tomX = 0 | |
| let tomY = 0 | |
| let tomAngle = 0 | |
| tom.turnOnLight({ durationMs: 0, red: 255, green: 255, blue: 255 }) | |
| tom.on('id:position-id', data => { | |
| tomX = data.x | |
| tomY = data.y | |
| tomAngle = data.angle | |
| }) | |
| // set light color and store position | |
| let spikeX = 0 | |
| let spikeY = 0 | |
| let spikeAngle = 0 | |
| spike.turnOnLight({ durationMs: 0, red: 255, green: 255, blue: 255 }) | |
| spike.on('id:position-id', data => { | |
| spikeX = data.x | |
| spikeY = data.y | |
| spikeAngle = data.angle | |
| }) | |
| // loop | |
| setInterval(() => { | |
| spike.move(...chase(tomX, tomY, spikeY, spikeY, spikeAngle), 100) | |
| tom.move(...chase(jerryX, jerryY, tomX, tomY, tomAngle), 100) | |
| jerry.move(...chase(spikeX, spikeY, jerryX, jerryY, jerryAngle), 100) | |
| }, 50) | |
| } | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment