What you think of my game until now? How can i make the archer move properly?

Heya guys!!!
Well, i have a little gameplay video to show, and hm, i was wondering, how can i make my archer and animals move properly in the slopes??
Heres the movement code i have, its from dermetfan but i changed so much due to my own needs hehe :stuck_out_tongue:

So anyway, i need help, at least with theory so i can start drawning ideas and logics in my book :stuck_out_tongue:

So heres the video :

And heres the movement code of the archer :

 public void update(float delta) {

        if (data.getCurrent_Health() <= 0) {
            alive = false;
        }

        if (alive) {
            // apply gravity
            velocity.y -= gravity * delta;

            // clamp velocity
            if (velocity.y > speed) {
                velocity.y = speed;
            } else if (velocity.y < -speed) {
                velocity.y = -speed;
            }

            // save old position
            float oldX = getX(), oldY = getY(), tileWidth = collisionLayer.getTileWidth(), tileHeight = collisionLayer.getTileHeight();
            boolean collisionX = false, collisionY = false;

            // move on x
            setX(getX() + velocity.x * delta);

            if (velocity.x < 0) { // going left
                // top left
                TiledMapTileLayer.Cell cell = collisionLayer.getCell((int) (getX() / tileWidth), (int) ((getY() + getHeight()) / tileHeight));
                if (cell != null) {
                    collisionX = cell.getTile().getProperties().containsKey("blocked");
                }

                // middle left
                if (!collisionX) {
                    TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) (getX() / tileWidth), (int) ((getY() + getHeight() / 2) / tileHeight));
                    if (cell1 != null) {
                        collisionX = cell1.getTile().getProperties().containsKey("blocked");
                    }
                }

                // bottom left
                if (!collisionX) {
                    TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) (getX() / tileWidth), (int) (getY() / tileHeight));
                    if (cell1 != null) {
                        collisionX = cell1.getTile().getProperties().containsKey("blocked");
                    }
                }
            } else if (velocity.x > 0) { // going right
                // top right
                TiledMapTileLayer.Cell cell = collisionLayer.getCell((int) ((getX() + getWidth()) / tileWidth), (int) ((getY() + getHeight()) / tileHeight));
                if (cell != null) {
                    collisionX = cell.getTile().getProperties().containsKey("blocked");
                }
                // middle right
                if (!collisionX) {
                    TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) ((getX() + getWidth()) / tileWidth), (int) ((getY() + getHeight() / 2) / tileHeight));
                    if (cell1 != null) {
                        collisionX = cell1.getTile().getProperties().containsKey("blocked");
                    }
                }

                // bottom right
                if (!collisionX) {
                    TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) ((getX() + getWidth()) / tileWidth), (int) (getY() / tileHeight));
                    if (cell1 != null) {
                        collisionX = cell1.getTile().getProperties().containsKey("blocked");
                    }
                }
            }

            // react to x collision
            if (collisionX) {
                setX(oldX);
                velocity.x = 0;
            }

            // move on y
            setY(getY() + velocity.y * delta);

            if (velocity.y < 0) { // going down
                // bottom left
                TiledMapTileLayer.Cell cell = collisionLayer.getCell((int) (getX() / tileWidth), (int) (getY() / tileHeight));
                if (cell != null) {
                    collisionY = cell.getTile().getProperties().containsKey("blocked");
                }
                // bottom middle
                if (!collisionY) {
                    TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) ((getX() + getWidth() / 2) / tileWidth), (int) (getY() / tileHeight));
                    if (cell1 != null) {
                        collisionY = cell1.getTile().getProperties().containsKey("blocked");
                    }
                }

                // bottom right
                if (!collisionY) {
                    TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) ((getX() + getWidth()) / tileWidth), (int) (getY() / tileHeight));
                    if (cell1 != null) {
                        collisionY = cell1.getTile().getProperties().containsKey("blocked");
                    }
                }

                canJump = collisionY;
            } else if (velocity.y > 0) { // going up
                // top left
                TiledMapTileLayer.Cell cell = collisionLayer.getCell((int) (getX() / tileWidth), (int) ((getY() + getHeight()) / tileHeight));
                if (cell != null) {
                    collisionY = cell.getTile().getProperties().containsKey("blocked");
                }

                // top middle
                if (!collisionY) {
                    TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) ((getX() + getWidth() / 2) / tileWidth), (int) ((getY() + getHeight()) / tileHeight));
                    if (cell1 != null) {
                        collisionY = cell1.getTile().getProperties().containsKey("blocked");
                    }
                }

                // top right
                if (!collisionY) {
                    TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) ((getX() + getWidth()) / tileWidth), (int) ((getY() + getHeight()) / tileHeight));
                    if (cell1 != null) {
                        collisionY = cell1.getTile().getProperties().containsKey("blocked");
                    }
                }
            }

            // react to y collision
            if (collisionY) {
                setY(oldY);
                velocity.y = 0;
            }
        }
       
.....
            
        }
        
    }

How can i fix this?
http://img545.imageshack.us/img545/2733/9nmg.jpg

private int default_x_width = 38; //40
    private int default_y_height = 40; //44
    

    public void updateMovement(TiledMapTileLayer collisionLayer, Vector2 velocity, float gravity, float delta, float speed) {
        //player.setOrigin(player.getOriginX() + player.getOriginX() / 3, player.getOriginY());
        // apply gravity
        velocity.y -= gravity * delta;

        // clamp velocity
        if (velocity.y > speed) {
            velocity.y = speed;
        } else if (velocity.y < -speed) {
            velocity.y = -speed;
        }

        // save old position
        float oldX = player.getX(), oldY = player.getY(), tileWidth = collisionLayer.getTileWidth(), tileHeight = collisionLayer.getTileHeight();
        boolean collisionX = false, collisionY = false;

        // move on x
        player.setX(player.getX() + velocity.x * delta);

        if (velocity.x < 0) { // going left
            // top left
            TiledMapTileLayer.Cell cell = collisionLayer.getCell((int) (player.getX() / tileWidth), (int) ((player.getY() + default_y_height) / tileHeight));
            if (cell != null) {
                collisionX = cell.getTile().getProperties().containsKey("blocked");
            }

            // middle left
            if (!collisionX) {
                TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) (player.getX() / tileWidth), (int) ((player.getY() + default_y_height / 2) / tileHeight));
                if (cell1 != null) {
                    collisionX = cell1.getTile().getProperties().containsKey("blocked");
                }
            }

            // bottom left
            if (!collisionX) {
                TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) (player.getX() / tileWidth), (int) (player.getY() / tileHeight));
                if (cell1 != null) {
                    collisionX = cell1.getTile().getProperties().containsKey("blocked");
                }
            }
        } else if (velocity.x > 0) { // going right
            // top right
            TiledMapTileLayer.Cell cell = collisionLayer.getCell((int) ((player.getX() + default_x_width) / tileWidth), (int) ((player.getY() + default_y_height) / tileHeight));
            if (cell != null) {
                collisionX = cell.getTile().getProperties().containsKey("blocked");
            }
            // middle right
            if (!collisionX) {
                TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) ((player.getX() + default_x_width) / tileWidth), (int) ((player.getY() + default_y_height / 2) / tileHeight));
                if (cell1 != null) {
                    collisionX = cell1.getTile().getProperties().containsKey("blocked");
                }
            }

            // bottom right
            if (!collisionX) {
                TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) ((player.getX() + default_x_width) / tileWidth), (int) (player.getY() / tileHeight));
                if (cell1 != null) {
                    collisionX = cell1.getTile().getProperties().containsKey("blocked");
                }
            }
        }

        // react to x collision
        if (collisionX) {
            player.setX(oldX);
            velocity.x = 0;
        }

        // move on y
        player.setY(player.getY() + velocity.y * delta);

        if (velocity.y < 0) { // going down
            // bottom left
            TiledMapTileLayer.Cell cell = collisionLayer.getCell((int) (player.getX() / tileWidth), (int) (player.getY() / tileHeight));
            if (cell != null) {
                collisionY = cell.getTile().getProperties().containsKey("blocked");
            }
            // bottom middle
            if (!collisionY) {
                TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) ((player.getX() + default_x_width / 2) / tileWidth), (int) (player.getY() / tileHeight));
                if (cell1 != null) {
                    collisionY = cell1.getTile().getProperties().containsKey("blocked");
                }
            }

            // bottom right
            if (!collisionY) {
                TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) ((player.getX() + default_x_width) / tileWidth), (int) (player.getY() / tileHeight));
                if (cell1 != null) {
                    collisionY = cell1.getTile().getProperties().containsKey("blocked");
                }
            }

            player.canJump = collisionY;
        } else if (velocity.y > 0) { // going up
            // top left
            TiledMapTileLayer.Cell cell = collisionLayer.getCell((int) (player.getX() / tileWidth), (int) ((player.getY() + default_y_height) / tileHeight));
            if (cell != null) {
                collisionY = cell.getTile().getProperties().containsKey("blocked");
            }

            // top middle
            if (!collisionY) {
                TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) ((player.getX() + default_x_width / 2) / tileWidth), (int) ((player.getY() + default_y_height) / tileHeight));
                if (cell1 != null) {
                    collisionY = cell1.getTile().getProperties().containsKey("blocked");
                }
            }

            // top right
            if (!collisionY) {
                TiledMapTileLayer.Cell cell1 = collisionLayer.getCell((int) ((player.getX() + default_x_width) / tileWidth), (int) ((player.getY() + default_y_height) / tileHeight));
                if (cell1 != null) {
                    collisionY = cell1.getTile().getProperties().containsKey("blocked");
                }
            }
        }

        // react to y collision
        if (collisionY) {
            player.setY(oldY);
            velocity.y = 0;
        }

    }

You need to use an appropriately sized capsule for your physics collision model.

What you mean?
Am i not doing that now already? with default size width?

Right now, the default width is being based upon the image size, which by the looks of it, is probably 36wide, by 48 pixels wide

for example, I made an outline of whats happening

You can see the bounding box and why he is doing that.

What you need to do, is set probably defaultWidth/2 and then offset it a bit to be centered

So it looks and behaves more like

However,
many games do support a ‘stand on ledge’ by 1 pixel type approach
megaman for example

http://img62.imageshack.us/img62/9759/dontdothis.png

As you can see here, the bounding box is slightly smaller than the character, and his feet overlap the ground tile

However somegames actually use a combination box AND a ‘circle’ for the character. (box for left/right and top collsion) but then a circle for feet, so they can stand really close to the edge. But because of the ‘roundness’ of the circle. They could potentially slide off the edge.

However there are many approaches.

I would suggest looking at this site, it has some good information

:wink: 8) :stuck_out_tongue: :emo: :point: ::slight_smile: :point: :-\ :clue: :persecutioncomplex: :clue:

“offset it a bit to be centered”

Does that mean to change the origin?

Depends on your logic and rendering code

But you need to cut your bounding box to about 2/3 of its current size (play around with it a bit)

Also offset your rendering sprite a bit too( try tweaking some settings, maybe the offset will do it) there are several ways to go about fixing this.

But now that you have a slightly better idea of whats going on, you can try and fix it.

Also, your collision code that you posted before could use some work. There are several ways, of simplifying it without having ot have so many nested tiledmap calls that you are currently doing. Such as building a simple integer or boolean[][] 2d array of ‘blocked’ and open, that you simply compile/construct when the map first loads. May make it easier than what you have right now.