小行星样式运动,行进方向不与矢量三角形的方向对齐

时间:2017-04-12 00:28:17

标签: c

我目前正在尝试重新创建经典游戏Asteroids。

到目前为止,我已经设法在屏幕上创建了一个矢量三角形,并且我能够使这个三角形围绕原点旋转。我现在正试图让三角形移动的方式与原始游戏中的船一样移动,具有漂浮/冰上的感觉。然而,我的问题是当按下向上按钮时船的行进方向与三角形的方向不一致。即三角形向错误的方向移动,看起来很愚蠢。

我很确定三角形运动的物理特性很好,所以我认为错误可能与三角形的定义有关,但我不确定。我的代码如下:

//handles updating the game state, moving the objects, and handling collisions.

#include "gamestate.h"

struct gameState_t{
        int lives;
} game;

struct ship_t{
    int heading;
    int x, y;
    double vx, vy;
    int speed;
    bool isAcc; //isAccelerating
} ship;

void physics(void)
{
    moveShip();
}


void initGameState(void){

    ship.heading = 0;
    ship.x = 240;
    ship.y = 240;
    ship.vx = 0;
    ship.vy = 0;
    ship.speed = 0;
    ship.isAcc = false; //acceleration

}




//getters

int getShipX(void){
    return ship.x;
}

int getShipY(void){
    return ship.y;
}

int getShipHeading(void){
    return ship.heading;
}

int getShipSpeed(void)
{
    return ship.speed;
}

bool getShipAcc(void)
{
    return ship.isAcc;
}

double getShipVx(void)
{
    return ship.vx;
}

double getShipVy(void)
{
    return ship.vy;
}


void moveShip(void)
{
        ship.x += (ship.vx / 10);
        ship.y += (ship.vy / 10);
}











void incHeading(void)
{

    ship.heading++;

    if(ship.heading < 0)
    {
        ship.heading = 360;
    }

    if(ship.heading > 360)
    {
        ship.heading = 0;
    }
}

void decHeading(void)
{
    //ship.heading = ship.heading - 6;
    ship.heading--;

    if(ship.heading < 0)
    {
        ship.heading = 360;
    }

    if(ship.heading > 360)
    {
        ship.heading = 0;
    }
}

void accelerate(void)
{
    float shipHeadingRads = (ship.heading * (PI / 180));
    //float shipHeadingRads = ship.heading;

    ship.vy += sin(shipHeadingRads) * acceleration;
    ship.vx += cos(shipHeadingRads) * acceleration;

}

下一部分是用于在屏幕上绘制三角形的代码,但是这在另一个模块中使用。

void drawShipActive(void)
{
    int shipX = getShipX();
    int shipY = getShipY();
    int shipHeading = getShipHeading();

    float shipHeadingRads = (shipHeading * (PI / 180));

    int x1, y1, x2, y2, x3, y3;
    int x1r, y1r, x2r, y2r, x3r, y3r;

    /*
    x1 = shipX;
    y1 = shipY - 20;
    x2 = shipX - 10;
    y2 = shipY + 10;
    x3 = shipX + 10;
    y3 = shipY + 10;
    */

    x1 = shipX;
    y1 = shipY + 20;
    x2 = shipX - 10;
    y2 = shipY - 10;
    x3 = shipX + 10;
    y3 = shipY - 10;

    x1r = ((x1 - shipX) * cos(shipHeadingRads)) - ((shipY - y1) * -(sin(shipHeadingRads))) + shipX;
    y1r = ((shipY - y1) * cos(shipHeadingRads)) - ((x1 - shipX) * sin(shipHeadingRads)) + shipY;
    x2r = ((x2 - shipX) * cos(shipHeadingRads)) - ((shipY - y2) * -(sin(shipHeadingRads))) + shipX;
    y2r = ((shipY - y2) * cos(shipHeadingRads)) - ((x2 - shipX) * sin(shipHeadingRads)) + shipY;
    x3r = ((x3 - shipX) * cos(shipHeadingRads)) - ((shipY - y3) * -(sin(shipHeadingRads))) + shipX;
    y3r = ((shipY - y3) * cos(shipHeadingRads)) - ((x3 - shipX) * sin(shipHeadingRads)) + shipY;

    screen->drawTriangle(x1r, y1r, x2r, y2r, x3r, y3r, GREEN);
}

我认为这是一个相对较小的问题,但我不确定问题究竟在哪里。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

当shipHeadingRads为0时,您将在正X方向加速(因为cos(0)为1而sin(0)为0)。

ship.vy += sin(shipHeadingRads) * acceleration;
ship.vx += cos(shipHeadingRads) * acceleration;

然而,三角形&#34;指向&#34;将是正Y方向,因为这是你正在绘制的形状:

x1 = shipX;
y1 = shipY + 20;
x2 = shipX - 10;
y2 = shipY - 10;
x3 = shipX + 10;
y3 = shipY - 10;

如果在纸上绘制点(0,20),( - 10,-10)和(10, - 10),您会看到尖端侧是正Y方向。

您需要更改其中任何一项。