由於這似乎是'堆棧'上反覆出現的話題,我將強化我的問題,因爲沒有涉及到。已經涵蓋的是用於平臺遊戲等的2D瓦片碰撞,但是以我製作遊戲的方式,沒有瓦片。我也沒有使用額外的庫,所有內容都是由我自己親自編寫的。Android - 二維平臺碰撞檢測/ w重力物理
我擁有的是Rect's遊戲中的每一個物體。到目前爲止,只有兩個對象類正在使用,Platform和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我開始研究一些物理引擎,但後來意識到我將不得不構建一個全新的框架,而且我還必須使用大部分物理引擎他們的過程等等,但我需要的只是重力。所以我只是引力而已,現在加入了碰撞檢測。 這當然沒有錯? – Kwibble
你看過JST Topology Suite嗎? http://tsusiatsoftware.net/jts/main.html如果你想演示它可以做什麼檢查了這一點:http://bl.ocks.org/christophermanning/4450188 – portforwardpodcast
@portforwardpodcast我會很感激,如果你可以請幫助我原來的問題,而不是給我這些其他的東西......這是好東西,毫無疑問,但它不是我想要的東西。我已經給出了我爲什麼使用自己的物理學的原因,所以如果你能幫助我,我會很感激。謝謝 – Kwibble