我在游戏中添加了一个玩家,并且我有一个圆圈作为地图。我希望这个玩家的碰撞响应指向基于圆圈斜率的当前角度。以下是...
我在游戏中添加了一个玩家,并且我有一个圆圈作为地图。我希望这个玩家的碰撞响应指向基于圆圈斜率的当前角度。
以下是圆圈的代码
public void handleCollision(Entity entity) {
double widthScale = (width / (double) image.getWidth());
double heightScale = (height / (double) image.getHeight());
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
int bottomLeftX = entity.getX();
int bottomLeftY = entity.getY() + entity.getHeight();
int bottomRightX = entity.getX() + entity.getWidth();
int bottomRightY = entity.getY() + entity.getHeight();
int transLeftX = doubleToInt((bottomLeftX - x) / widthScale);
int transLeftY = doubleToInt((bottomLeftY - y) / heightScale);
int transRightX = doubleToInt((bottomRightX - x) / widthScale);
int transRightY = doubleToInt((bottomRightY - y) / heightScale);
boolean leftOut = transLeftX < 0 || transLeftY < 0;
boolean rightOut = transRightX < 0 || transRightY < 0;
if(!leftOut) {
if(transparencyMap[transLeftX][transLeftY] == 1) {
int x1 = 0;
int y1 = 0;
if(transLeftY > imageHeight / 2) {
y1 = imageHeight;
}
if(transLeftX > imageWidth / 2) {
x1 = imageWidth;
}
int x2 = transLeftX;
int y2 = heightMap[transLeftX];
System.out.println(x1 + " " + y1 + ", " + x2 + " " + y2);
double rotation = Math.toDegrees(Math.atan2(y1 - y2, x1 - x2));
entity.setRotation((float) Math.toRadians(rotation + 90));
}
}
// TODO: Add !rightOut logic
}
我尝试过根据玩家所在的象限将玩家指向角落。如果玩家位于右上象限,则指向右上角,依此类推。但这种方法不起作用。我怎样才能自动找出斜率并相应地变换玩家