To put player together with AI for 2D games Library

edited January 2014 in Library Questions

I would like place a user-controlled player inside a game made from AI_for_2D_games library. But having problems with wall and bullet collision. Did I need to load an extra collision map for constrain player to the wall like this http://processing.org/discourse/beta/num_1272507983.html or is there other ways?

Answers

  • edited January 2014 Answer ✓

    PLAYER WALL CONSTRAINT

    extra collision map for constrain player to the wall

    If you simply want to prevent the player passing through the wall, then you can create an object of type Vehicle (or from a class that extends Vehicle) and use the Wall Avoidance behaviour. Then use these methods to tweak the player / wall interaction.

    // wall avoidance detection factors
    player.AP().wallAvoidFactors(nbrWhiskers, whiskerLength, fov, shortOnSide);
    // adjust weighting
    player.AP().wallAvoidWeight(weight)
    

    My website has some guides on these Wall Avoidance and Tweaking the Steering Behaviour

    BULLET WALL COLLISION DETECTION

    I am assuming that In this case we simply need the bullet to disappear when it hits a wall. In this case the bullet must be created from the MovingEntity class (or any class that extends MovingEntity) and add a method to detect a collision with a wall.

    There are several ways of coding this, all of which are difficult to explain in words so I have created a simple applet to demonstrate the most flexible approach. The code is below and you can see it in action HERE.

    import game2dai.entities.*;
    import game2dai.entityshapes.ps.*;
    import game2dai.maths.*;
    import game2dai.*;
    import game2dai.entityshapes.*;
    import game2dai.fsm.*;
    import game2dai.steering.*;
    import game2dai.utils.*;
    import game2dai.graph.*;
    import java.util.*;
    
    World world;
    Domain domain;
    Bullet bullet;
    ArrowPic bulletView;
    StopWatch watch;
    float bulletSpeed = 400;
    
    public void setup() {
      size(600, 400);
      world = new World(width, height);
      domain = new Domain(0, 0, width, height);
      bulletView = new ArrowPic(this, 14, color(255, 80, 80), color(128, 0, 0), 1);
      makeWalls();
      watch = new StopWatch();
    }
    
    public void draw() {
      float deltaTime = (float) watch.getElapsedTime();
      if (bullet != null) {
        Set<Wall> walls = world.getWalls(bullet);
        for (Wall w : walls) {
          if (bullet.hasPassedThroughWall(w)) {
            bullet.velocity(0, 0);
            bullet.die(world, 5); // die after 5 seconds
            bullet = null;
            break; // no need to test anymore walls
          }
        }
      }
      background(220, 255, 220);
      fill(240, 240, 255);
      stroke(0, 0, 128);
      ellipse(width/2, height/2, 5, 5);
      world.update(deltaTime);
      world.draw();
    }
    
    public void mouseClicked() {
      if (bullet == null) {
        Vector2D dir = new Vector2D(mouseX - width/2, mouseY-height/2);
        dir.normalize();
        Vector2D vel = Vector2D.mult(dir, bulletSpeed);
        bullet = new Bullet(new Vector2D(width/2, height/2), 6, vel, bulletSpeed, dir, 1, 12, 200);
        bullet.renderer(bulletView);
        bullet.worldDomain(domain);
        bullet.worldDomainConstraint(SBF.REBOUND);
        world.add(bullet);
      }
    }
    
    public void makeWalls() {
      int[] x = {  
        80, 200, 50, 140, 200, 240, 400, 400, 400, 550, 550, 550, 340, 530
      };
      int[] y = { 
        100, 50, 350, 190, 300, 330, 70, 130, 100, 50, 110, 280, 260, 320
      };
      for (int n = 0; n < x.length - 1; n+=2) {
        WallPic wp = new WallPic(this, color(0, 0, 128), 2);
        Wall w = new Wall(new Vector2D(x[n], y[n]), new Vector2D(x[n+1], y[n+1]), true);
        w.renderer(wp);
        world.add(w);
      }
    }
    
    public class Bullet extends MovingEntity {
    
      public Bullet(Vector2D position, double radius, 
      Vector2D velocity, double max_speed, Vector2D heading, 
      double mass, double max_turn_rate, double max_force) {
        super(position, radius, velocity, max_speed, heading, mass, 
        max_turn_rate, max_force);
      }
    
      public boolean hasPassedThroughWall(Wall wall) {
        return wall.isEitherSide(prevPos, pos);
      }
    }
    
  • Woo. That really works. Thanks. Never thought define player unit as Vehicle.

Sign In or Register to comment.