Guest User

AABB Code

a guest
Nov 21st, 2015
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. // Map.java
  2.  
  3. public List<BoundingBox> getBoundingBoxes() {
  4. List<BoundingBox> boxes = new ArrayList<BoundingBox>();
  5.  
  6. int i, j;
  7. for(int x = 0; x < this.size.x; x++) {
  8. for(int y = 0; y < this.size.y; y++) {
  9. if(this.getTile(x, y).collide()) {
  10. boxes.add(new BoundingBox(new Vec2(i = x * 16, j = y * 16), new Vec2(i + 16, j + 16)));
  11. }
  12. }
  13. }
  14. return boxes;
  15. }
  16.  
  17. // Player.java
  18.  
  19. public void handleInput() {
  20. BoundingBox last = this.pos.clone();
  21. if(Keys.A.state) {
  22. this.pos.offset(new Vec2(-1, 0));
  23. }
  24. if(Keys.D.state) {
  25. this.pos.offset(new Vec2(1, 0));
  26. }
  27. if(Keys.W.state) {
  28. this.pos.offset(new Vec2(0, -1));
  29. }
  30. if(Keys.S.state) {
  31. this.pos.offset(new Vec2(0, 1));
  32. }
  33. List<BoundingBox> boxes = Crawler.INSTANCE.map.getBoundingBoxes();
  34. for(BoundingBox box : boxes) {
  35. box.collide(last, this.pos);
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment