【发布时间】:2013-12-12 01:09:50
【问题描述】:
由于这似乎是“堆栈”上反复出现的主题,因此我将强调我的问题,因为它没有涵盖。已经涵盖的是平台游戏等的 2D 瓷砖碰撞,但是根据我制作游戏的方式,没有瓷砖。我也没有使用额外的库,一切都是我自己写的。
我所拥有的是为游戏中的每个对象绑定 Rect。到目前为止,只有两个对象类在使用,Platform 和 Entity。 Entity 包含玩家移动等的所有东西,而 Platform 是一个坚固的非移动平台。
平台.java:
package com.toongames.game.objects;
import android.graphics.Color;
import android.graphics.Rect;
import com.toongames.framework.Graphics;
public class Platform {
private int x, y;
private int width, height;
public Platform(int par1, int par2, int par3, int par4) {
x = par1;
y = par2;
width = par3;
height = par4;
}
public Rect getBounds() {
return new Rect(x, y, x + width, y + height);
}
}
Entity.java:
package com.toongames.game.entity;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;
import com.toongames.framework.Graphics;
import com.toongames.framework.Image;
public class Entity {
public final float GRAVITY = 0.1F;
private String entityID;
private Point pos;
private int dx;
private float vel;
public Point desiredPos;
public boolean onGround;
public Entity(String par0String, int par1, int par2) {
entityID = par0String;
pos = new Point(par1, par2);
desiredPos = pos;
dx = 0;
vel = 0;
}
public void update(float deltaTime) {
vel = vel + (GRAVITY * deltaTime);
pos.y += (vel * deltaTime);
pos.x += dx;
}
public void setDx(int par1) {
dx = par1;
}
public int getDx() {
return dx;
}
public void setVelocity(int par1) {
vel = par1;
}
public float getVelocity() {
return vel;
}
public void setPos() {
pos = desiredPos;
}
public Rect getBounds() {
return new Rect(desiredPos.x, desiredPos.y, desiredPos.x + 80, desiredPos.y + 80);
}
}
我已经成功地让玩家与上下两面的东西发生碰撞,但我终生无法让玩家左右碰撞。每当我向左或向右移动时与平台发生碰撞时,我都会跳到所碰撞的平台顶部。
我知道这与我的逻辑有关,但我无法弄清楚要使用的正确逻辑。
ScreenGame.java:
package com.toongames.game.screen;
// Imports here...
public class ScreenGame extends Screen {
private Entity player;
private Button left, right, jump;
private Platform floor, p, p2, p3;
private ArrayList<Platform> platforms;
public ScreenGame(Game game) {
super(game);
player = new Entity("PLAYER", 300, 100, Assets.charRight);
left = new Button(Assets.move_left, 10, 790 - Assets.move_left.getHeight(), Assets.move_left.getWidth(), Assets.move_left.getHeight());
right = new Button(Assets.move_right, 20 + Assets.move_left.getWidth(), 790 - Assets.move_right.getHeight(), Assets.move_right.getWidth(), Assets.move_right.getHeight());
jump = new Button(Assets.jump, 1270 - Assets.jump.getWidth(), 790 - Assets.jump.getHeight(), Assets.jump.getWidth(), Assets.jump.getHeight());
floor = new Platform(0, 790, 1280, 80);
p = new Platform(1280 - 500, 500, 400, 80);
p2 = new Platform(0, 200, 400, 80);
p3 = new Platform(400, 120, 200, 80);
platforms = new ArrayList<Platform>();
platforms.add(floor);
platforms.add(p);
platforms.add(p2);
platforms.add(p3);
}
// An update method calls these
public void updateMovement(float deltaTime) {
List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
int len = touchEvents.size();
for (int i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);
if (event.type == TouchEvent.TOUCH_DOWN) {
if (inBounds(left.getBounds(), event)) {
player.setDx((int) -(deltaTime * 1.5F));
} else if (inBounds(right.getBounds(), event)) {
player.setDx((int) deltaTime * 2);
} else if (inBounds(jump.getBounds(), event)) {
if (player.onGround) {
player.setVelocity(-8);
}
}
} else if (event.type == TouchEvent.TOUCH_DRAGGED) {
if (inBounds(left.getBounds(), event)) {
player.setDx((int) -deltaTime * 2);
} else if (inBounds(right.getBounds(), event)) {
player.setDx((int) deltaTime * 2);
} else if (inBounds(jump.getBounds(), event)) {
if (player.onGround) {
player.setVelocity(-8);
}
} else {
player.setDx(0);
player.jumpCounter = 0;
}
} else if (event.type == TouchEvent.TOUCH_UP) {
player.setDx(0);
player.jumpCounter = 0;
}
}
}
// An update method calls these
public void updateGameObjects(float deltaTime) {
for (Platform p : platforms)
p.update();
player.update(deltaTime);
}
// An update method calls these
public void checkCollisions() {
Rect playerRect = player.getBounds();
for (Platform p : platforms) {
Rect pRect = p.getBounds();
if (Rect.intersects(playerRect, pRect)) {
Rect intersection = playerRect;
intersection.intersect(pRect);
if (player.getVelocity() != player.GRAVITY) {
int resolutionHeight;
if (player.getVelocity() < player.GRAVITY)
resolutionHeight = intersection.height();
else {
resolutionHeight = -intersection.height();
player.onGround = true;
}
player.setVelocity(0);
player.desiredPos = new Point(player.desiredPos.x, player.desiredPos.y + resolutionHeight);
}
}
}
player.setPos();
}
}
作为一个额外的说明,我已经删除了一些与实体和实体健康等图像有关的不必要的代码。我还删除了一些空的方法和类似的东西,它们与以往没有任何关系。
[编辑] 删除大部分绘图代码和导入。现在所有绝对必要的东西都在那里了。
【问题讨论】:
-
@portforwardpodcast 我开始研究一些物理引擎,但后来意识到我必须构建一个全新的框架,而且我还必须使用他们的大部分流程等,但我只需要是重力。所以我刚刚制作了重力,现在正在添加碰撞检测。这肯定没有什么问题吗?
-
您查看过 JST 拓扑套件吗? tsusiatsoftware.net/jts/main.html 如果你想要演示它的功能,请查看:bl.ocks.org/christophermanning/4450188
-
@portforwardpodcast 如果您能帮我解决我最初的问题而不是给我其他东西,我将不胜感激......这是好东西,毫无疑问,但它不是我的我想要。我已经给出了我为什么要使用自己的物理学的理由,所以如果你能帮助我,我将不胜感激。谢谢
-
这篇文章的代码太多,我无法有效地给你答案。进一步配对您的代码通常可以找到答案。
-
@portforwardpodcast 我会尝试这样做,谢谢你的提示。
标签: android 2d collision-detection game-physics