2017-05-22 4 views
0

Java Processing 3을 사용하고 2 인용 탱크 게임을 만들고 있습니다. 아래는 포탑에 대한 내 코드입니다. 현재 나는 tDir에서 마우스를 따라 포탑을 목표로하고 있는데, 위아래 화살표를 사용하여 목표를 0도에서 90도까지 위 아래로 움직일 수 있기를 원했습니다.Java Processing 목적으로 화살표 키 사용

어떻게하면됩니까? 감사.

/* 
Uzair 
*/ 

PVector mPos; //mouse position 
PVector tPos; //position of turret 
PVector tDir; //the firing direction of the turret 
int gravMult = 3; 

void setup() { 
    size(1200, 600); 
    init(); 
} 

void init() { 
    //PVector initializations 
    mPos = new PVector(); //zero til mouse x and y exist 
    tPos = new PVector(width/8, height); 
    tDir = new PVector(); // 
} 
void draw() { 
    //clear last frame 
    background(100,100,140); 

    //check keys to see if there is new key input for turret 
    if (keyPressed){ 
    if (key == 'w'){ 
     tDir.y -= 10; 
    } 
    else if (key == 's'){ 
     tDir.y += 10; 
    } 
    } 

    mPos.set(mouseX, mouseY); 
    tDir = PVector.sub(mPos, tPos); 
    tDir.normalize(); 
    tDir.mult(50); 

    //draw 
    fill(255); 
    ellipse(tPos.x, tPos.y, 40, 40); 
    strokeWeight(5); 
    line(tPos.x, tPos.y, tPos.x + tDir.x, tPos.y + tDir.y); 
    fill(255, 0, 0); 
    ellipse(tPos.x + tDir.x, tPos.y + tDir.y, 10, 10); 
} 
+1

Java가 처리되지 않습니다. Processing-mode가 아닌 Java-mode로 될 사람들이 많으므로 Java에 대한 참조를 제거하도록 권장 할 것입니다. Java 관련 문제가 아니기 때문입니다. – Makoto

답변

1
이것은 각도/회전을 증가시키는 대신 현재만큼 y 위치를 증가 같은데 0 내지 90

에서 상하 목표

.

극좌표 (각도/반경)를 직교 좌표 (x, y)로 변환해야합니다.

수식을 사용하여 수행 할 수 있습니다

x = cos(angle) * radius 
y = sin(angle) * radius 

귀하의 경우

tDir.x = cos(angle) * 50; 
tDir.y = sin(angle) * 50; 

에 ...이 마음 PVector에두고 가치가 있지만 이미 fromAngle()를 통해이 기능을 제공합니다.

PVector mPos; //mouse position 
PVector tPos; //position of turret 
PVector tDir; //the firing direction of the turret 
int gravMult = 3; 
//angle in radians 
float angle = 0; 

void setup() { 
    size(1200, 600); 
    init(); 
} 

void init() { 
    //PVector initializations 
    mPos = new PVector(); //zero til mouse x and y exist 
    tPos = new PVector(width/8, height * .75); 
    tDir = new PVector(); // 
} 
void draw() { 
    //clear last frame 
    background(100,100,140); 

    //check keys to see if there is new key input for turret 
    if (keyPressed){ 
    if (key == 'w'){ 
     angle -= 0.1; 
     tDir = PVector.mult(PVector.fromAngle(angle),50); 
    } 
    else if (key == 's'){ 
     angle += 0.1; 
     tDir = PVector.mult(PVector.fromAngle(angle),50); 
    } 
    }else if(mousePressed){ 
    mPos.set(mouseX, mouseY); 
    tDir = PVector.sub(mPos, tPos); 
    tDir.normalize(); 
    tDir.mult(50); 
    } 
    //draw 
    fill(255); 
    ellipse(tPos.x, tPos.y, 40, 40); 
    strokeWeight(5); 
    line(tPos.x, tPos.y, tPos.x + tDir.x, tPos.y + tDir.y); 
    fill(255, 0, 0); 
    ellipse(tPos.x + tDir.x, tPos.y + tDir.y, 10, 10); 
} 

또한 도움이 this answer을 찾을 수 있습니다

다음은이 아이디어를 사용하는 불통 코드입니다.

1

스택 오버플로가 정말 형의 질문에 "나는 이것을 어떻게"일반적으로 설계되지 않았습니다. 구체적으로 "나는 X를 시도했는데 예상 Y이지만 Z를 대신 입력"했습니다. 즉, 나는 일반적인 의미로 돕기 위해 노력할 것입니다.

작은 단계로 문제를 해결하고 한 번에 하나씩 단계를 수행해야합니다. 화살표 키를 누를 때 콘솔에 무언가를 인쇄하는 별도의 독립형 스케치를 만들 수 있습니까?

스케치와는 별도로 스케치 상단의 위치 또는 각도를 변수에 저장하는 다른 스케치를 생성 할 수 있습니까? 이러한 변수를 사용하여 장면을 그리고 다시 그 자체로 작업하십시오.

사용자가 직접 작업 할 때 사용자가 화살표 키를 누를 때 스케치 수준 변수를 변경하여이를 결합하는 방법을 생각할 수 있습니다.

특정 단계에서 어려움을 겪는다면 MCVE을 새 질문으로 게시 해주세요. 행운을 빕니다.