'스택'에 대해 반복되는 주제 인 것처럼 보이므로 문제를 보완하지 않겠습니다. 커버 된 것은 플랫폼 게임 등을위한 2D 타일 충돌입니다. 그러나 제 게임을 만든 방식으로는 타일이 없습니다. 나는 또한 여분의 라이브러리를 사용하지 않고 모든 것은 내 손으로 쓴다.안드로이드 - 2D 플랫폼 충돌 감지/중력 물리학
내가 가지고있는 것은 게임의 모든 개체에 대해 Rect의 경계선입니다. 지금까지 사용중인 두 개의 객체 클래스 인 플랫폼 및 엔티티 만 있습니다. 엔티티에는 플레이어 이동 등을위한 모든 요소가 포함되어 있지만 플랫폼은 견고한 움직이지 않는 플랫폼을위한 것입니다.
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