2013-12-12 2 views
2

'스택'에 대해 반복되는 주제 인 것처럼 보이므로 문제를 보완하지 않겠습니다. 커버 된 것은 플랫폼 게임 등을위한 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(); 
    } 
} 

, 나는 엔티티와 엔티티 건강 등을위한 이미지와 함께 할 불필요한 코드의 일부를 잘라가 ... 또한 내가 비어있는 방법을 절단하고 그와 같은 것들은 그다지 관련성이 없습니다.

[편집] 대부분의 도면 코드를 잘라내어 가져옵니다. 모든 절대적으로 필요한 것들이 지금 있습니다.

+0

@portforwardpodcast 필자는 일부 물리 엔진을 살펴보기 시작했지만 완전히 새로운 프레임 워크를 구축해야한다는 것을 깨달았습니다. 그들의 과정 등. 그러나 내가 필요로하는 것은 모두 중력이었다. 그래서 방금 중력을 만들었고 충돌 감지 기능을 추가했습니다. 분명히이 사실이 잘못되었습니다. – Kwibble

+0

JST Topology Suite를 살펴 보셨습니까? http://tsusiatsoftware.net/jts/main.html 만약 당신이 그것을 할 수있는 데모를 원한다면 : http://bl.ocks.org/christophermanning/4450188 – portforwardpodcast

+0

@portforwardpodcast 만약 당신이 저에게이 다른 물건을주는 대신에 원래의 질문으로 나를 도울 수 있겠습니까? 그것은 좋은 물건입니다. 의심의 여지가 없지만, 제가 원하는 것은 아닙니다. 나는 내 자신의 물리학을 왜 사용하고 있는지에 대한 이유를 설명해 주었고, 당신이 그것을 도와 주면 고맙겠습니다. 감사합니다 – Kwibble

답변

0
player.desiredPos = new Point(player.desiredPos.x, player.desiredPos.y + resolutionHeight); 

"이 위의 이동, 오른쪽/왼쪽이 아닙니까?"

당신의 Rect.intersects 메서드는 어느 방향으로 충돌이 발생했는지를 나타내는 { NONE, LEFT, RIGHT, UP, DOWN } 중 하나를 반환해야한다고 생각합니다. 그래서 올바른 방향으로 충돌에 반응 할 수 있습니다 ...

+0

나는 이처럼 충돌 시스템을 가지고 있었지만 중력을 추가하기 전에는 그랬습니다. 중력을 추가하면 모든 충돌은 두 충돌, 즉 충돌 및 충돌이있었습니다. 그래서 충돌은 의도 한대로 작동하지 않았습니다. 또한 타일 맵을 사용하는 자습서를 읽었으며 타일 맵 이었지만 여전히 올바른 충돌 아이디어를 가지고있었습니다.당신이 떨어지는 경우, 당신은 여전히 ​​떨어지기를 원하기 때문에 왼쪽 오른쪽의 충돌 수정을하고 싶습니다. 플랫폼에서 왼쪽/오른쪽으로 이동하는 것과 동일하게 위로 이동하여 수정하십시오. (계속) – Kwibble

+0

(계속) 그러나 그것은 여전히 ​​떨어지지 않는 동안 벽면으로 움직이는 것을 고치는 길은주지 않습니다. 내가 가지고있는 문제의 그게 전부 야 ... 답변 주셔서 감사 : D 조 – Kwibble