I am starting to make some java games and I think i have sorted out the collision side of things ( i have made a space invaders game) so i know how to shoot and kill enemies. I am now wanting to stop players/monsters going through walls etc?

p.s. what is the best way to do walls? (just paint() them)

thanks for you help

Dani AI

Generated

Nice quick fix by — storing a last-safe position and restoring it after a collision is a common, simple way to keep sprites out of walls. 's idea of reversing direction is also fine for dumb enemies. A few practical clarifications and a slightly more robust pattern follow.

Painting walls with paint() is only the visual step. Keep a separate collision model (tile boolean array, list of rectangles or Shape objects) that the physics code tests against. For axis-aligned walls use AABB detection and resolve penetration by pushing the mover out along the smallest overlap axis; that produces stable stopping or sliding instead of teleporting back and forth. Example (Java-style) for resolution:

Rectangle2D next = new Rectangle2D.Double(x + vx*dt, y + vy*dt, w, h);
for (Rectangle2D wall : walls) {
    if (next.intersects(wall)) {
        double overlapX = Math.min(next.getMaxX(), wall.getMaxX()) - Math.max(next.getMinX(), wall.getMinX());
        double overlapY = Math.min(next.getMaxY(), wall.getMaxY()) - Math.max(next.getMinY(), wall.getMinY());
        if (overlapX < overlapY) {
            x += (vx > 0) ? -overlapX - 1e-6 : overlapX + 1e-6;
            vx = 0;
        } else {
            y += (vy > 0) ? -overlapY - 1e-6 : overlapY + 1e-6;
            vy = 0;
        }
    }
}

Fast-moving objects can still “tunnel” through thin walls. Fixes: swept-AABB/time-of-impact checks, raycasts for bullets, or simple sub-stepping (split the frame move into small steps and resolve each step). Sub-stepping example:

int steps = (int)Math.ceil(Math.hypot(vx*dt, vy*dt) / maxStep);
for (int i=0; i<steps; i++) { x += vx*dt/steps; y += vy*dt/steps; resolveCollisions(); }

Performance tips: only test nearby colliders (tile lookup, spatial hash or quadtree), draw debug bounding boxes, handle X and Y moves separately for stable sliding on corners, and use a tiny epsilon when pushing objects out so they do not immediately re-intersect and get stuck.

Recommended Answers

All 2 Replies

If you just have simple, axis-aligned walls, then just compare the distance between the enemy and wall with the size of the enemy. If the distance becomes less, then it has hit the wall. After that, just reverse the direction of the enemy. That's the most simple of collision methods. Others involve swept collisions, which work regardless of an object's speed.

problem solved
just stored the values like
GTC[0]=x;
GTC[1]=y;
before the collsion and where it collides
if (a instanceof Monster ) {
x = GTC[0];
y = GTC[1];
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.