[英]Java Platform Game Collision
我试图使播放器无法从左侧和右侧穿过平台,并使播放器停在平台顶部。 到目前为止,我所发生的所有碰撞都是左右移动,但是右边没有工作,而左边则是bug。 我不知道如何进行平台游戏的基本碰撞,有人可以给我一个可行的解决方案吗?
播放器类代码:
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
}
}
这是我长期以来一直在努力的事情,并且我知道有很多解决方案。显然,您需要根据发生碰撞的平台的任一侧来重置玩家的位置。
所以我要做的是检查矩形是否先相交。 然后,我尝试找出该平台与哪一侧发生碰撞。 我是通过在播放器矩形每一侧的外部创建一个矩形来实现的。 然后我得到平台矩形与每个侧面矩形的交集。 然后我计算出相交的面积。相交面积最大的一侧是发生碰撞的一侧。 然后,您可以根据与他相撞的平台的侧面重置玩家的位置。 我创建了一个Mask类,使它在游戏引擎中非常易于使用
复杂的部分看起来像这样。 遮罩是我用来表示矩形的类
此方法返回一个布尔数组,正确的是发生碰撞的那一侧
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.