프로젝트

나도 메이커! 메이커스 여러분들의 작품/프로젝트를 공유하는 공간입니다.

에스플로라와 프로세싱을 이용한 미로탈출 게임 만들기

2016-01-27 00:24:02

프로젝트 개요

 

아두이노 에스플로라(Esplora)에는 가속도 센서가 내장되어 있습니다.

가속도 센서는 어떤 방향의 움직임으로 가속도가 있어도 감지할 수 있지만, 지구의 중력도 가속도로서 감지할 수 있습니다.

이 기능을 이용하면 기울기 센서로도 쓸 수 있습니다.

 

HW: 아두이노 에스플로라

SW: 프로세싱3.0.1

 

최종 결과

 

센서의 작동 유무를 테스트하는 중간 과정

 

 

미로 설계

센서의 작동을 테스트하는 과정 및 미로 설계는 여러분이 해야 할 일은 아닙니다. 지금은 이런 과정을 거치는구나 하고 넘어가시기 바랍니다.

 

연결 방법

 

에스플로라 보드를 PC에 연결하는 것 외에 다른 특별한 연결 방법은 없습니다.

 

 

아두이노 보드 소스 (인용시 출처를 명시해 주세요.)

 

#include <Esplora.h>

void setup()
{
  Serial.begin(9600);        // initialize serial communications with your computer
}

void loop()
{
  int xAxis = (Esplora.readAccelerometer(X_AXIS) - 30)/2;    // read the X axis
  int yAxis = (Esplora.readAccelerometer(Y_AXIS) - 35)/2;    // read the Y axis
  int zAxis = (Esplora.readAccelerometer(Z_AXIS) -  0)/2;    // read the Z axis

  Serial.write(xAxis);
  Serial.print(" ");
  Serial.write(yAxis);
  Serial.print(" ");
  Serial.write(zAxis);
  Serial.println(" ");

  delay(100);              // wait half a second (500 milliseconds)
}

 

프로세싱 소스 (인용시 출처를 명시해 주세요.)

 

PC쪽 프로그램은 프로세싱으로 작성하였습니다.

프로세싱 소스를 작동시키려면 프로세싱 최신 버전을 다운 받아 설치하세요. https://processing.org/download/?processing

 

 

import processing.serial.*;
Serial myPort;
float xRot = 0;
float yRot = 0;
int hWall =  30;
int wall[][] = 
    {
    {-400, -400, 800, 0},     {-400, -400, 0, 800},     {-400, -200, 100, 0},     {-300, -300, 0, 100},     {-300, -300, 100, 0}, 
    {-200, -300, 0, 100},     {-100, -300, 0, 200},     {0, -300, 0, 100},     {100, -300, 0, 400},     {100, -300, 100, 0}, 
    {300, -300, 0, 200},     {300, -300, 100, 0},     {0, -200, 100, 0},     {200, -200, 100, 0},     {400, -400, 0, 800}, 
    {-300, -100, 200, 0},     {100, -100, 100, 0},     {-300, -100, 0, 200},     {-200, -100, 0, 100},     {0, -100, 0, 100}, 
    {0, 0, 100, 0},     {-100, 0, 0, 100},     {200, 0, 0, 100},     {300, 0, 0, 100},     {-400, 100, 100, 0}, 
    {-100, 100, 400, 0},     {-200, 100, 0, 200},     {-300, 100, 100, 0},     {200, 200, 200, 0},     {-300, 200, 0, 200}, 
    {-100, 200, 0, 100},     {0, 200, 0, 200},     {100, 200, 0, 100},     {-200, 300, 100, 0},     {100, 300, 300, 0}, 
    {-400, 400, 700, 0}
};
float xBall = 50;
float yBall = -350;
float vxBall = 0;
float vyBall = 0;
void setup() 
{
    size(1280, 768, P3D);
    myPort = new Serial(this, Serial.list()[1], 9600);
    setup2();
}
void setup2() 
{
    xBall = 50;
    yBall = -350;
    vxBall = 0;
    vyBall = 0;
}
void draw() 
{
    background(255);
    float fov = PI/6.0;
    float cameraZ = (height/2.0) / tan(fov/2.0); 
    perspective(fov, float(width)/float(height), cameraZ/2.0, cameraZ*2.0); 
    camera(width/2, height/2, cameraZ, width/2, height/2, 0, 0, 1, 0);
    lights();
    translate(width/2, height/2);
    rotateX(1.3);
    pushMatrix();
    int nBuffer = 0;
    byte[] inBuffer = new byte[1024];
    if (myPort.available() > 0) 
    {
        nBuffer = myPort.readBytes(inBuffer);
        myPort.readBytes(inBuffer);
        if (nBuffer == 8) 
        {
            int x = -inBuffer[2];
            if (x > 0) x = max(x - 1, 0);
            if (x < 0) x = min(x + 1, 0);           
            xRot = x/200.0;
            xRot = constrain(xRot, -PI/8, PI/8);
            int y = -inBuffer[0];
            if (y > 0) y = max(y - 1, 0);
            if (y < 0) y = min(y + 1, 0);           
            yRot = y/200.0;
            yRot = constrain(yRot, -PI/8, PI/8);
        }
    }
    noFill();
    stroke(0, 255, 0);
    {
        pushMatrix();
        translate(0, 0, 0);
        box(1024, 1024, 1);
        popMatrix();
    }
    rotateX(xRot);
    rotateY(yRot);
    fill(255);
    stroke(64);
    {
        pushMatrix();
        translate(0, 0, -1);
        box(800, 800, 1);
        popMatrix();
    }
    for (int n = 0; n < wall.length; n++)
    {
        pushMatrix();
        translate(wall[n][0] + wall[n][2]/2 - 2, wall[n][1] + wall[n][3]/2 - 2, hWall/2);
        box(wall[n][2] + 5, wall[n][3] + 5, hWall);
        popMatrix();
    }

    vxBall += yRot;
    vyBall -= xRot;
    if (abs(vxBall) > 10) vxBall = vxBall*10/abs(vxBall);
    if (abs(vyBall) > 10) vyBall = vyBall*10/abs(vyBall);
    xBall += vxBall;
    yBall += vyBall;

    float tollerence = 23;
    for (int n = 0; n < wall.length; n++)
    {
        if ((wall[n][0] - tollerence*0.9) < xBall && (wall[n][0] + wall[n][2] + tollerence*0.9) > xBall && wall[n][2] > 0)
        {
            if (abs(wall[n][1] - yBall) < tollerence)
            {
                if (wall[n][1] < yBall)
                {
                    yBall++;
                    vyBall = 0;
                } else
                {
                    yBall--;
                    vyBall = 0;
                }
            }
        }
        if ((wall[n][1] - tollerence*0.9) < yBall && (wall[n][1] + wall[n][3] + tollerence*0.9) > yBall && wall[n][3] > 0)
        {
            if (abs(wall[n][0] - xBall) < tollerence)
            {
                if (wall[n][0] < xBall)
                {
                    xBall++;
                    vxBall = 0;
                } else
                {
                    xBall--;
                    vxBall = 0;
                }
            }
        }
    }
    {
        fill(255, 0, 0);
        noStroke();
        pushMatrix();
        translate(xBall, yBall, 20);
        sphere(20);
        popMatrix();
    }
    if(yBall > 420) setup2();
    popMatrix();
}

 

 

자바실험실

profile

Klant 2016-01-27 08:31:22

재미난 프로젝트네요~ 프로세싱은 웹에서도 동작 가능해서 저 같은 경우 예전에 휴대폰의 가속도 센서 값으로 프로세싱 작업을 했던 기억이 나네요~ 스마트폰에 내장되어 있는 센서들을 활용하면 더 재미난 것들을 할 수도 있을 거 같아요! ;)

profile

원기옥 2016-01-27 08:44:33

이전 Klant 님의 "미로 탈출 게임기 만들기"와 비슷한듯 하면서도 접근 방법을 달리하니 매우 새롭네요.^^

profile

수박쨈 2016-01-27 11:52:45

프로세싱은 저도 잘 못쓰는데 프로세싱으로 미로를 만드는거 조차 신기하네요...ㅋㅋㅋ
저도 프로세싱은 인터넷에서 가져와서 조금씩 수정하는 정도라 저런건 아직 어렵네요...........

profile

모모 2016-01-31 21:05:14

우와 대단하네요!

profile

제임스짱 2016-02-01 19:01:19

오~ 신박한데요~ ^^

profile

김정우 2016-02-20 01:31:42

이야 멋진 프로젝트하셨네요.

profile

구본휘 2016-02-20 02:32:39

재미있는 프로젝트였어요 좋은 정보 감사합니다.

profile

장세현 2016-03-15 19:37:22

좋은정보감사합니다.

profile

정상훈 2016-10-11 15:14:04

멋진 프로젝트입니다 !!!!!