【问题标题】:Collision detection in andengine gles2andengine gles2中的碰撞检测
【发布时间】:2014-09-30 16:16:58
【问题描述】:

我正在尝试在 andengine Gles2 的 TMXmap 示例中的所有仙人掌上的仙人掌属性项目上设置碰撞检测。我尝试了各种方法 - 谁能给我一种有效的方法?

原始代码 Tmxmaps andengine

一个建议的解决方案: collision detection

另一个建议的解决方案: from andengine.org

我试过了:

if(pTMXTileProperties.containsTMXProperty("cactus", "true")) {
    final Rectangle rect = new Rectangle(pTMXTile.getTileX()+10, pTMXTile.getTileY(),14, 14);
    final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 1f);
    PhysicsFactory.createBoxBody(mPhysicsWorld, rect, BodyType.StaticBody, boxFixtureDef);
    rect.setVisible(false);
    mScene.attachChild(rect);
}

这是来自AndEngine: Handling collisions with TMX Objects

但我收到此错误:

Physicsfactory not found

【问题讨论】:

    标签: java android andengine


    【解决方案1】:

    我正在使用您那里的 TMX 示例作为我的游戏的基础。

    这是碰撞的主要代码块:

    // 定义块行为

        mPathFinderMap = new IPathFinderMap<TMXLayer>(){
    
                private boolean mCollide;
    
                @Override
                public boolean isBlocked(final int pX, final int pY, final TMXLayer pTMXLayer) {
                    /*
                     * This is where collisions happen and are detected 
                     */
                    mCollide = false;
                    //Null check. Used since not all tiles have properties
                    if(pTMXLayer.getTMXTile(pX, pY).getTMXTileProperties(mTiledMap) != null){
                        //Get tiles with collision property
                        if(pTMXLayer.getTMXTile(pX, pY).getTMXTileProperties(mTiledMap).containsTMXProperty("COLLISION", "true")) 
                            mCollide = true;                                        
                    }   
    
                    if(mTMXmapLoader.getCollideTiles().contains(pTMXLayer.getTMXTile(pX, pY)))
                        mCollide = true;
    
                    return mCollide;
                }
    
        };
    
    /*
     * This method moves the sprite to the designated location
     */
    public void walkTo(TMXTile pFinalPosition) {
        if(mHasFinishedPath){
            mHasFinishedPath = false;//This prevents overlapping paths when the user double clicks. Used to prevent stutter
            //Player coordinates
            final float[] lPlayerCordinates = mPlayerSprite.convertLocalToSceneCoordinates(mPlayerSprite.getWidth()/2, mPlayerSprite.getHeight()/2);
            // Get the tile the center of the player are currently waking on. 
            TMXTile lPlayerPosition = SceneManager.mWorldScene.getTouchLayer().getTMXTileAt(lPlayerCordinates[Constants.VERTEX_INDEX_X], lPlayerCordinates[Constants.VERTEX_INDEX_Y]);
            mFinalPosition = pFinalPosition;
    
            // Sets the A* path from the player location to the touched location.
            if(mPathFinderMap.isBlocked(pFinalPosition.getTileColumn(), pFinalPosition.getTileRow(), SceneManager.mWorldScene.getTouchLayer())){    
                pFinalPosition = getNextTile(lPlayerPosition, pFinalPosition);
            }
    
            // These are the parameters used to determine the 
            int lFromCol = lPlayerPosition.getTileColumn(); int lFromRow = lPlayerPosition.getTileRow();
            int lToCol = pFinalPosition.getTileColumn();    int lToRow = pFinalPosition.getTileRow();
            boolean lAllowDiagonal = false;
            // Find the path. This needs to be refreshed
            AStarPath = mAStarPathFinder.findPath(MAX_SEARCH_DEPTH, mPathFinderMap, 0, 0, mTiledMap.getTileColumns() - 1, mTiledMap.getTileRows() - 1, SceneManager.mWorldScene.getTouchLayer(), 
                    lFromCol, lFromRow, lToCol, lToRow, lAllowDiagonal, mHeuristic, mCostCallback);
    
            //Log.i("AstarPath", "AStarPath  " + AStarPath);
            //Only loads the path if the AStarPath is not null
            Path lPlayerPath = loadPathFound();
            //Log.i("AstarPath", "lPlayerPath  " + lPlayerPath);
            if(lPlayerPath != null)
                moveSprite(lPlayerPath);//Moves the sprite along the path
            else
                mHasFinishedPath = true;//If the path is null the player has not moved. Set the flag to true allows input to effect the sprite
        }else{
            //Update parameters
            mFinalPosition = pFinalPosition;
            mWaypointIndex = 0;
        }
    }
    
    /*
     * Updates the path
     */
    public void updatePath(TMXTile pFinalPosition) {    
        //Player coordinates
        final float[] lPlayerCordinates = mPlayerSprite.convertLocalToSceneCoordinates(mPlayerSprite.getWidth()/2, mPlayerSprite.getHeight()/2);
        // Get the tile the feet of the player are currently waking on. 
        TMXTile lPlayerPosition = SceneManager.mWorldScene.getTouchLayer().getTMXTileAt(lPlayerCordinates[Constants.VERTEX_INDEX_X], lPlayerCordinates[Constants.VERTEX_INDEX_Y]);
    
        // Sets the A* path from the player location to the touched location.
        if(mPathFinderMap.isBlocked(pFinalPosition.getTileColumn(), pFinalPosition.getTileRow(), SceneManager.mWorldScene.getTouchLayer())){    
            pFinalPosition = getNextTile(lPlayerPosition, pFinalPosition);
        }
    
        // Determine the tile locations
        int FromCol = lPlayerPosition.getTileColumn();
        int FromRow = lPlayerPosition.getTileRow();
        int ToCol = pFinalPosition.getTileColumn();
        int ToRow = pFinalPosition.getTileRow();
        // Find the path. This needs to be refreshed
        AStarPath = mAStarPathFinder.findPath(MAX_SEARCH_DEPTH, mPathFinderMap, 0, 0, mTiledMap.getTileColumns()-1, mTiledMap.getTileRows()-1, SceneManager.mWorldScene.getTouchLayer(), 
                FromCol, FromRow, ToCol, ToRow, false, mHeuristic, mCostCallback);
    
        //Loads the path with the astar specifications
        Path lPlayerPath = loadPathFound();
        //Moves the sprite along the path
        if(lPlayerPath != null){
            moveSprite(lPlayerPath);
        }else{
            //If the path is still null after the path manipulation then the path is finished
            mHasFinishedPath = true;
            mWaypointIndex = 0;
            //mPlayerSprite.stopAnimation();
            //AStarPath = null;
        }
    }
    

    TMXmapLoader 完成剩下的工作:

    //Get the collision, ext, and changing tiles from the object sets on the map
        mCollideTiles = this.getObjectGroupPropertyTiles("COLLIDE",  TMXGroupObjects);  
        mExitTiles = this.getObjectPropertyTiles("EXIT", mTMXObjects);
        mChangingTiles = this.getObjectGroupPropertyTiles("CHANGE", TMXGroupObjects);
    ...
    public ArrayList<TMXTile> getCollideTiles(){
        return mCollideTiles;       
    }
    ...
    public ArrayList<TMXTile> getObjectGroupPropertyTiles(String pName, final int pLayer, ArrayList<TMXObjectGroup> pTMXObjectGroups){
        ArrayList<TMXTile> ObjectTile = new ArrayList<TMXTile>();
        for (final TMXObjectGroup pObjectGroups : pTMXObjectGroups) {
            // Iterates through the properties and assigns them to the new variable
            for (final TMXObjectGroupProperty pGroupProperties : pObjectGroups.getTMXObjectGroupProperties()) {
                //Sees if any of the elements have this condition
                if (pGroupProperties.getName().contains(pName)) {
                    for (final TMXObject pObjectTiles : pObjectGroups.getTMXObjects()) {
                        int ObjectX = pObjectTiles.getX();
                        int ObjectY = pObjectTiles.getY();
                        // Gets the number of rows and columns in the object
                        int ObjectRows = pObjectTiles.getHeight() / WorldActivity.TILE_HEIGHT;
                        int ObjectColumns = pObjectTiles.getWidth() / WorldActivity.TILE_WIDTH;
    
                        for (int TileRow = 0; TileRow < ObjectRows; TileRow++) {
                            for (int TileColumn = 0; TileColumn < ObjectColumns; TileColumn++) {
                                float lObjectTileX = ObjectX + TileColumn * WorldActivity.TILE_WIDTH;
                                float lObjectTileY = ObjectY + TileRow * WorldActivity.TILE_HEIGHT;
                                ObjectTile.add(mTMXTiledMap.getTMXLayers().get(pLayer).getTMXTileAt(lObjectTileX, lObjectTileY));                       
                            }                            
                        }
                    }
                }           
            }
        }
        return ObjectTile;
    }
    

    【讨论】:

    • 嘿,所以你基本上是在制作特殊瓷砖的坐标数组并在它们上面创建矩形对象以处理碰撞?
    • 那种。一组特殊瓷砖的坐标,是的。矩形?不。这段代码中实际上根本没有 box2d 物理。您可以使用 isBlocked() 来确定目标图块是否是碰撞对象。但是一旦设置好,你就可以在 Tiled 中创建一个碰撞层,代码会在玩家四处走动时处理这些瓷砖的碰撞。
    • 你有这个我可以下载/结帐的工作示例
    • 这有帮助吗?
    【解决方案2】:

    我不熟悉android开发,但是错误似乎表明PhysicsFactory没有被导入。也许尝试在文件顶部添加这样的导入语句?

    import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
    

    【讨论】:

    • 我回家后会测试一下
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多