0

プレイヤーが左右からプラットホームを通り抜けられないようにし、プラットホームの上でプレイヤーを停止させようとしています。これまでのところ、衝突のために持っているのは左右だけですが、右は機能せず、左はバグっています。プラットフォーム ゲームの基本的なコリジョンのやり方がわかりません。

プレーヤー クラス コード:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public class Player implements KeyListener
{
int x, y, width, height;

boolean jump, left, right;

PlayerThread playerThread;

int maxHeight = 40;
double heightC = 0;
boolean onPlatform = false;
boolean landed = true;
int prevY;
int prevX;
Rectangle playRect = new Rectangle();
boolean leftCollision = false;
boolean rightCollision = false;
Platform p;

public Player()
{
    x = 500;
    y = 350;
    width = 40;
    height = 50;
    playerThread = new PlayerThread();
    playerThread.start();
}

public void paint(Graphics g)
{
    String str = String.valueOf(x);
    String str2 = String.valueOf(y);

    g.setColor(Color.RED);
    playRect.setBounds(x, y, width, height);
    g.fillRect(x, y, width, height);

    g.drawString("X: " + str + ", Y: " + str2, 100, 100);
}

public void update(Platform p)
{
    this.p = p;
    CheckForCollision(p);

}

public void CheckForCollision(Platform p)
{
    int pX = p.getX();
    int pY = p.getY();
    int pWidth = p.getWidth();
    int pHeight = p.getHeight();

    //COLLISION WITH PLATFORM CODE
    if (playRect.intersects(p.plat) && left == true && !jump && !right && landed)
    {
        System.out.println("LEFT");
        x = prevX;
        leftCollision = true;
    }
    else
    {
        leftCollision = false;
    }

    if (playRect.intersects(p.plat) && right == true && !jump && !right && landed)
    {
        System.out.println("RIGHT");
        x = prevX;
        rightCollision = true;
    }
    else
    {
        rightCollision = false;
    }
}

public class PlayerThread extends Thread implements Runnable
{
    public void run()
    {
        while (true)
        {
            if (left && !leftCollision)
            {

                prevX = x;
                x -= 2;

            }

            if (right && !rightCollision)
            {
                prevX = x;
                x += 2;
            }

            if (jump)
            {
                if (heightC >= maxHeight)
                {
                    System.out.println(heightC);
                    jump = false;
                    heightC = 0;
                }
                else
                {
                    heightC += 1.5;
                    prevY = y;
                    y -= 5;
                    landed = false;
                }
            }

            //GRAVITY CODE
            if (!jump && !landed)
            {
                if (y < 400 - height)
                {
                    prevY = y;
                    y += 5;
                    //landed = false;
                }
            }

            if (y >= 400 - height && !landed)
            {
                y = 400 - height;
                landed = true;
            }

            try
            {
                sleep(17);
            } 
            catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

@Override
public void keyPressed(KeyEvent e)
{
    switch (e.getKeyCode())
    {
    case KeyEvent.VK_LEFT:

        left = true;
        break;
    case KeyEvent.VK_RIGHT:
        right = true;
        break;
    case KeyEvent.VK_SPACE:
        if (landed)
        {
        jump = true;
        }
        break;
    }
}

@Override
public void keyReleased(KeyEvent e)
{
    switch (e.getKeyCode())
    {
    case KeyEvent.VK_LEFT:
        left = false;
        break;
    case KeyEvent.VK_RIGHT:
        right = false;
        break;
    case KeyEvent.VK_SPACE:
        break;
    }
}



public int getX()
{
    return x;
}

public void setX(int x)
{
    this.x = x;
}

public int getY()
{
    return y;
}

public void setY(int y)
{
    this.y = y;
}

public int getWidth()
{
    return width;
}

public void setWidth(int width)
{
    this.width = width;
}

public int getHeight()
{
    return height;
}

public void setHeight(int height)
{
    this.height = height;
}

@Override
public void keyTyped(KeyEvent e)
{
    // TODO Auto-generated method stub

}
}
4

1 に答える 1

0

これは私が長い間苦労してきたことであり、これには多くの解決策があることを知っています.明らかに、衝突が発生したプラットフォームの側面に基づいてプレーヤーの位置をリセットする必要があります.

だから私がすることは、最初に長方形が交差しているかどうかを確認することです。次に、プラットフォームのどちら側に衝突しているかを把握しようとします。これは、プレーヤーの四角形の各辺の外側に四角形を作成することによって行います。次に、プラットフォームの長方形と各側面の長方形の交点を取得します。次に、交差点の面積を計算します。交差点の面積が最も大きい側が、衝突が発生した側です。次に、衝突したプラットフォームの側面に基づいて、プレーヤーの位置をリセットできます。これを自分のゲーム エンジンで非常に使いやすくする Mask クラスを作成しました。

複雑な部分はこんな感じ。マスクは、四角形を表すために使用するクラスです

このメソッドはブール値の配列を返します。true は衝突が発生した側です。

public static int left=2,top=3,right=0,bottom=1;
public boolean[] collisionSide(Mask m){

    boolean[] leftUp = new boolean[4];

    Rectangle[] boxes = new Rectangle[4];
    boxes[0] = new Rectangle(this.getRect().x-1, this.getRect().y, 1, this.getRect().height);
    boxes[1] = new Rectangle(this.getRect().x, this.getRect().y-1, this.getRect().width, 1);
    boxes[2] = new Rectangle(this.getRect().x + this.getRect().width, this.getRect().y, 1, this.getRect().height);
    boxes[3] = new Rectangle(this.getRect().x, this.getRect().y+this.getRect().height, this.getRect().width, 1);

    double greatestArea = 0;
    int greatest = 0;

    for( int bbb = 0; bbb<4; bbb++){
        if( Calc.getArea( boxes[bbb].createIntersection(m.getRect() ) ) > greatestArea){
            greatestArea = Calc.getArea( boxes[bbb].createIntersection(m.getRect() ) );
            greatest = bbb;
        }
    }

    for(int b=0; b<4; b++)
        leftUp[b] = false;

    leftUp[greatest] = true;

    return leftUp;
}
于 2014-07-21T00:00:26.067 に答える