<legend id='h7ZbU'><style id='h7ZbU'><dir id='h7ZbU'><q id='h7ZbU'></q></dir></style></legend>
  • <i id='h7ZbU'><tr id='h7ZbU'><dt id='h7ZbU'><q id='h7ZbU'><span id='h7ZbU'><b id='h7ZbU'><form id='h7ZbU'><ins id='h7ZbU'></ins><ul id='h7ZbU'></ul><sub id='h7ZbU'></sub></form><legend id='h7ZbU'></legend><bdo id='h7ZbU'><pre id='h7ZbU'><center id='h7ZbU'></center></pre></bdo></b><th id='h7ZbU'></th></span></q></dt></tr></i><div id='h7ZbU'><tfoot id='h7ZbU'></tfoot><dl id='h7ZbU'><fieldset id='h7ZbU'></fieldset></dl></div>

  • <tfoot id='h7ZbU'></tfoot>

  • <small id='h7ZbU'></small><noframes id='h7ZbU'>

        <bdo id='h7ZbU'></bdo><ul id='h7ZbU'></ul>

      1. 矩形内的 libgdx 动画 - 碰撞检测 - 矩形

        时间:2023-05-18

        1. <tfoot id='X1K8J'></tfoot>
            <tbody id='X1K8J'></tbody>
            • <legend id='X1K8J'><style id='X1K8J'><dir id='X1K8J'><q id='X1K8J'></q></dir></style></legend>

                <bdo id='X1K8J'></bdo><ul id='X1K8J'></ul>

                  <i id='X1K8J'><tr id='X1K8J'><dt id='X1K8J'><q id='X1K8J'><span id='X1K8J'><b id='X1K8J'><form id='X1K8J'><ins id='X1K8J'></ins><ul id='X1K8J'></ul><sub id='X1K8J'></sub></form><legend id='X1K8J'></legend><bdo id='X1K8J'><pre id='X1K8J'><center id='X1K8J'></center></pre></bdo></b><th id='X1K8J'></th></span></q></dt></tr></i><div id='X1K8J'><tfoot id='X1K8J'></tfoot><dl id='X1K8J'><fieldset id='X1K8J'></fieldset></dl></div>

                  <small id='X1K8J'></small><noframes id='X1K8J'>

                1. 本文介绍了矩形内的 libgdx 动画 - 碰撞检测 - 矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在编写一个类似于 Pokemon 风格的 RPG 游戏(俯视图).我现在正在研究碰撞检测的问题.我想创建基于矩形的碰撞检测.问题是我很难在我之前设置的动画周围绘制矩形.我在 Google 和 YouTube 上搜索了有关如何处理此问题的答案/教程,但一无所获.

                  I am writing a RPG game similar to the style of Pokemon (top down view). I am now working on the issue of collision detection. I am wanting to create the collision detection based on rectangles. The problem is that i am having difficulty drawing the rectangle around the animation that i have previously set. I have searched Google and YouTube for a answer/tutorial of how to handle this problem yet found nothing.

                  播放器类

                  public class Player {
                  
                  public Vector2 position; 
                  private float moveSpeed;
                  private SpriteBatch batch;
                  
                  //animation
                  public Animation an;
                  private Texture tex;
                  public TextureRegion currentFrame;
                  private TextureRegion[][] frames;
                  private float frameTime;
                  
                  
                  public Player(Vector2 position){
                      this.position = position;
                      moveSpeed = 5f;
                      createPlayer();
                  }
                  
                  public void createPlayer(){
                      tex = new Texture("Sprites/image.png");
                      frames = TextureRegion.split(tex, tex.getWidth()/3, tex.getHeight()/4);
                      an = new Animation(0.10f, frames[0]);
                  }
                  
                  public void render(float delta){
                      handleInput();
                      frameTime += delta;
                      currentFrame = an.getKeyFrame(frameTime, true);
                  }
                  
                  public void handleInput(){
                      if(Gdx.input.isKeyPressed(Keys.UP)){
                          an = new Animation(0.10f, frames[3]);
                          position.y += moveSpeed;
                      }
                      if(Gdx.input.isKeyPressed(Keys.DOWN)){
                          an = new Animation(0.10f, frames[0]);
                          position.y -= moveSpeed;
                      }
                      if(Gdx.input.isKeyPressed(Keys.LEFT)){
                          an = new Animation(0.10f, frames[1]);
                          position.x -= moveSpeed;
                      }
                      if(Gdx.input.isKeyPressed(Keys.RIGHT)){
                          an = new Animation(0.10f, frames[2]);
                          position.x += moveSpeed;    
                      }
                      if(!Gdx.input.isKeyPressed(Keys.ANY_KEY)){
                          an = new Animation(0.10f, frames[0]);
                      }
                  }
                  
                  public void dispose(){
                      batch.dispose();
                  }
                  
                  public float getX(){
                      return position.x;
                  }
                  
                  public float getY(){
                      return position.y;
                  }
                  
                  public int getWidth(){
                      return 32;
                  }
                  
                  public int getHeight(){
                      return 32;
                  }
                  
                  }
                  

                  WorldRenderer.class

                  WorldRenderer.class

                  public class WorldRenderer {
                  
                  private OrthogonalTiledMapRenderer renderer;
                  public OrthographicCamera camera;
                  private Player player;
                  
                  //Tilemap
                  private TiledMapTileLayer collision;
                  private TiledMap map;
                  
                  
                  public WorldRenderer() {
                      map = new TmxMapLoader().load("maps/testMap.tmx");
                      renderer = new OrthogonalTiledMapRenderer(map);
                  
                      //Tiled layers
                      collision = (TiledMapTileLayer) map.getLayers().get("collision");
                  
                      player = new Player(new Vector2(100, 100));
                      camera = new OrthographicCamera();
                      camera.viewportWidth = Gdx.graphics.getWidth()/2;
                      camera.viewportHeight = Gdx.graphics.getHeight()/2;
                  
                  }
                  public void render (float delta) {      
                      camera.update();
                      renderer.setView(camera);
                      renderer.render();
                  
                      player.render(delta);
                  
                  
                      // Calculate tile size in pixels
                      MapProperties prop = renderer.getMap().getProperties();
                      int mapWidth = prop.get("width", Integer.class); //how many tiles in map
                      int mapHeight = prop.get("height", Integer.class);
                      int tilePixelWidth = prop.get("tilewidth", Integer.class); //size of each tile
                      int tilePixelHeight = prop.get("tileheight", Integer.class);
                  
                      // Calculate total map size
                      int worldSizeX = mapWidth * tilePixelWidth;
                      int worldSizeY = mapHeight * tilePixelHeight;
                  
                      // Calculate min/max camera points inside the map
                      float minCameraX = camera.zoom * (camera.viewportWidth / 2); 
                      float maxCameraX = worldSizeX - minCameraX;
                      float minCameraY = camera.zoom * (camera.viewportHeight / 2);
                      float maxCameraY = worldSizeY - minCameraY;
                  
                      // set the camera to either the player or the min/max of the camera based on player position
                      camera.position.set(
                              Math.min(maxCameraX, Math.max(player.position.x + 32 / 2, minCameraX)),
                              Math.min(maxCameraY, Math.max(player.position.y + 32 / 2, minCameraY)),
                              0);
                      camera.update();
                      renderer.getSpriteBatch().setProjectionMatrix(camera.combined);
                  
                  
                  
                      renderer.getSpriteBatch().begin();
                      renderer.getSpriteBatch().draw(player.currentFrame, player.position.x, player.position.y);
                      renderer.getSpriteBatch().end();
                  }
                  
                  }
                  

                  推荐答案

                  如果你想检测两个矩形之间的碰撞,有一个我用 sprites 使用的例子.

                  If you want to detect collision between two rectangles, there is an example I used with sprites.

                  public class TestSpriteOne extends Sprite{  
                      private Rectangle rectangle;
                  

                  添加构造函数.

                  rectangle = new Rectangle(getX(), getY(), getWidth(), getHeight());
                  

                  更新方法

                  rectangle.setPosition(getX(), getY());
                  

                  是一个网络

                  public Rectangle getColliderActor(){
                      return this.rectangle;
                  }
                  

                  其他类

                  public class TestSpriteTwo extends Sprite{
                      private Rectangle rectangle;
                  

                  构造函数中.

                  rectangle = new Rectangle(getX(), getY(), getWidth(), getHeight());
                  

                  更新方法中

                  rectangle.setPosition(getX(), getY());
                  

                  方法中

                  public Rectangle getColliderActor(){
                     return this.rectangle;
                  }
                  

                  //在TestSpriteOne类中调用

                  // Call in the TestSpriteOne class

                  boolean hasCollided = TestSpriteTwo.getColliderActor().overlaps(getColliderActor());
                  

                  //在GameScreen类中调用

                  //Call in the GameScreen class

                  boolean hasCollided = TestSpriteOne.getColliderActor().overlaps(TestSpriteTwo.getColliderActor());
                  

                  这个矩形是否与另一个矩形重叠,所以hasCollided变量会变为true.

                  It overlaps whether this rectangle overlaps the other rectangle, so hasCollided variable will become true.

                  如果动画改变了它的宽度或高度,你可以在 update 方法中调整矩形的大小

                  if the animation changes its width or height, you could resize the rectangle in the update method

                  rectangle.setSize (width, height);
                  

                  这篇关于矩形内的 libgdx 动画 - 碰撞检测 - 矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Box2D 中的链形 下一篇:gdx.input.getY 被翻转

                  相关文章

                  最新文章

                  <tfoot id='oXQN4'></tfoot>
                    <bdo id='oXQN4'></bdo><ul id='oXQN4'></ul>
                  1. <i id='oXQN4'><tr id='oXQN4'><dt id='oXQN4'><q id='oXQN4'><span id='oXQN4'><b id='oXQN4'><form id='oXQN4'><ins id='oXQN4'></ins><ul id='oXQN4'></ul><sub id='oXQN4'></sub></form><legend id='oXQN4'></legend><bdo id='oXQN4'><pre id='oXQN4'><center id='oXQN4'></center></pre></bdo></b><th id='oXQN4'></th></span></q></dt></tr></i><div id='oXQN4'><tfoot id='oXQN4'></tfoot><dl id='oXQN4'><fieldset id='oXQN4'></fieldset></dl></div>
                    <legend id='oXQN4'><style id='oXQN4'><dir id='oXQN4'><q id='oXQN4'></q></dir></style></legend>

                    <small id='oXQN4'></small><noframes id='oXQN4'>