Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (290)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Block colision detection problems(off centered)  (Read 114 times)
0 Members and 1 Guest are viewing this topic.
Offline Ed_RockStarGuy

Junior Newbie





« Posted 2013-03-16 16:14:52 »

The problem i am having is when making the games block colliton center onto the blocks and for some reason the colision box is a few blocks NW from where it should be.
Player file:
package com.AdamWalters.com.zain.mob;

import com.AdamWalters.com.zain.Level.Level;
import com.AdamWalters.com.zain.Level.tiles.Tile;
import com.AdamWalters.com.zain.graphics.Screen;
import com.AdamWalters.com.zain.graphics.Sprite;
import com.AdamWalters.com.zain.input.InputHandler;

public class Player extends Mob {

   private InputHandler input;
   private Sprite sprite;
   private int anim = 0;
   private boolean walking = false;
   
   static final int maxHealth = 2000;
   static int health = 2000;

   int speed = 40;
   
   public Player(Level level, int x, int y, InputHandler input) {
        super(level, "Player", x, y, 1);
        this.input = input;
        sprite = Sprite.player_forward;
    }


   public void update() {
      boolean running;
      int xa = 0, ya = 0;
      if (anim < 7500)
         anim++;
      else
         anim = 0;
      if (input.shift){
         running = true;
         speed  = 20;
      }else{
         running = false;
         speed  = 40;
      }
      if (input.up&&running)
         ya--;
      if (input.down&&running)
         ya++;
      if (input.left&&running)
         xa--;
      if (input.right&&running)
         xa++;
      
      if (input.up)
         ya--;
      if (input.down)
         ya++;
      if (input.left)
         xa--;
      if (input.right)
         xa++;

      if (xa != 0 || ya != 0) {
         move(xa, ya);
         walking = true;
      } else {
         walking = false;
      }
   }

   public void render(Screen screen) {
      int flip = 0;
      if (dir == 2) {
         sprite = Sprite.player_forward;
         if (walking) {
            if (anim % speed > speed/2) {
               sprite = Sprite.player_forward_1;
            } else {
               sprite = Sprite.player_forward_2;

            }
         }
      }
      if (dir == 1) {
         sprite = Sprite.player_side;
         if (walking) {
            if (anim % speed > speed/2) {
               sprite = Sprite.player_side_1;
            } else {
               sprite = Sprite.player_side_2;
            }
         }
      }
      if (dir == 0) {
         sprite = Sprite.player_back;
         if (walking) {
            if (anim % speed > speed/2) {
               sprite = Sprite.player_back_1;
            } else {
               sprite = Sprite.player_back_2;
            }
         }
      }

      if (dir == 3) {
         sprite = Sprite.player_side;
         if (walking) {
            if (anim % speed > speed/2) {
               sprite = Sprite.player_side_1;
            } else {
               sprite = Sprite.player_side_2;
            }

         }
         flip = 1;
      }//this has to be changed for the player sprite. e.g. 16 for 32 * 32 sprites and 32 for 16 * 16 sprites.
      screen.renderPlayer(x - 16, y - 16, sprite, flip);
   }
   
   public boolean hasCollided(int xa, int ya) {
        int xMin = 0;
        int xMax = 15;
        int yMin = 7;
        int yMax = 15;
        for (int x = xMin; x < xMax; x++) {
            if (isSolidTile(xa, ya, x, yMin)) {
                return true;
            }
        }
        for (int x = xMin; x < xMax; x++) {
            if (isSolidTile(xa, ya, x, yMax)) {
                return true;
            }
        }
        for (int y = yMin; y < yMax; y++) {
            if (isSolidTile(xa, ya, xMin, y)) {
                return true;
            }
        }
        for (int y = yMin; y < yMax; y++) {
            if (isSolidTile(xa, ya, xMax, y)) {
                return true;
            }
        }
        return false;
    }

   public static int getHealthPersentage() {
      int persentage;
      persentage = (health/100)/maxHealth;
      return persentage;
   }
   public static int maxHealth(){
      return maxHealth;
   }
   public static int health(){
      return health;
   }
   public static void addDamage(int damage){
      health = health - damage;
   }

}

mob file:
package com.AdamWalters.com.zain.mob;

import com.AdamWalters.com.zain.Level.Level;
import com.AdamWalters.com.zain.Level.tiles.Tile;
import com.AdamWalters.com.zain.entity.Entity;
import com.AdamWalters.com.zain.graphics.Sprite;

public abstract class Mob extends Entity {

   protected Sprite sprite;
   protected int dir = 0;   // 0 = north, 1 = east, 2 = south, 3 = west
   protected boolean moving = false;
   int speed;
   
   public Mob(Level level, String name, int x, int y, int speed) {
        super(level);
        this.x = x;
        this.y = y;
        this.speed = speed;
    }
   
   
   public void move(int xa, int ya) {
      if (xa > 0) dir = 1;
      if (xa < 0) dir = 3;
      if (ya > 0) dir = 2;
      if (ya < 0) dir = 0;
      
        if (!hasCollided(xa, ya)) {
            if (ya < 0)
                dir = 0;
            if (ya > 0)
               dir = 2;
            if (xa < 0)
               dir = 3;
            if (xa > 0)
               dir = 1;
            x += xa * 1;
            y += ya * 1;
        }
   }
    public abstract boolean hasCollided(int xa, int ya);
   protected boolean isSolidTile(int xa, int ya, int x, int y) {
        if (level == null) {
            return false;
        }
        Tile lastTile = level.getTile((this.x + x) >> 7, (this.y + y) >> 7);
        Tile newTile = level.getTile((this.x + x + xa) >> 7, (this.y + y + ya) >> 7);
        if (!lastTile.equals(newTile) && newTile.isSolid()) {
            return true;
        }
        return false;
    }

   public void update() {
   }
   
   public void render() {
   }
   
}

level file:
package com.AdamWalters.com.zain.Level;

import com.AdamWalters.com.zain.graphics.Screen;
import com.AdamWalters.com.zain.Level.tiles.Tile;

public class Level {
   
   protected static int width;
   protected static int height;
   protected int[] tilesInt;
   protected static int[] tiles;
   
   public static Level spawn = new SpawnLevel ("/levels/level.png");
   
   public Level(int width, int height) {
      this.width = width;
      this.height = height;
      tilesInt = new int [width * height];
      generateLevel();
   }
   
   public Level(String path) {
      loadLevel(path);
      generateLevel();
   }

   protected void generateLevel() {
   }
   
   protected void loadLevel(String path) {
   }

   public void tick() {
   }
   
   @SuppressWarnings
   ("unused") private void time() {
   }
   
   public void render(int xScroll, int yScroll, Screen screen) {
      
        if (xScroll < 0)
           xScroll = 0;
        if (xScroll > ((width << 3) - screen.width))
           xScroll = ((width << 3) - screen.width);
        if (yScroll < 0)
           yScroll = 0;
        if (yScroll > ((height << 3) - screen.height))
           yScroll = ((height << 3) - screen.height);
      
      screen.setOffset(xScroll, yScroll);
      /** >> 4 = /16 **/
      int x0 = xScroll >> 4;
      int x1 = (xScroll + screen.width + 16) >> 4;
      int y0 = yScroll >> 4;
      int y1 = (yScroll + screen.height + 16) >> 4;
      for (int y = y0; y < y1; y++) {
         for (int x = x0; x < x1; x++) {
            getTile(x, y).render(x, y, screen);
         }
      }
   }
   //Grass = 0xFF00 / 0x00FF00
   //Flower = 0xFFFF00
   //Rock = 0x878787
   //first two ff = 100% opaque & first two 00 = 100% transparent.
   public Tile getTile(int x, int y) {
      if (x < 0 || y < 0 || x >= width || y >= height){
         return Tile.voidTile;
      }
      if (tiles[x + y * width] == Tile.col_spawn_grass){
         return Tile.grass;
      }
      if (tiles[x + y * width] == Tile.col_spawn_log){
         return Tile.log;
      }
      if (tiles[x + y * width] == Tile.col_spawn_leaves){
         return Tile.leaves;
      }
      return Tile.voidTile;
   }
   public static int getTileID(int x, int y) {
      if (x < 0 || y < 0 || x >= width || y >= height){
         return 1;
      }
      if (tiles[x + y * width] == Tile.col_spawn_grass){
         return 2;
      }
      if (tiles[x + y * width] == Tile.col_spawn_log){
         return 3;
      }
      if (tiles[x + y * width] == Tile.col_spawn_leaves){
         return 4;
      }
      return 1;
   }
   

}
if anyone has  a solution to this it would be  appreciated if you left me a message or drop me a skype PM at :mythirdleg35
Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (61 views)
2013-05-17 21:29:12

alaslipknot (70 views)
2013-05-16 21:24:48

gouessej (100 views)
2013-05-16 00:53:38

gouessej (98 views)
2013-05-16 00:17:58

theagentd (107 views)
2013-05-15 15:01:13

theagentd (98 views)
2013-05-15 15:00:54

StreetDoggy (144 views)
2013-05-14 15:56:26

kutucuk (167 views)
2013-05-12 17:10:36

kutucuk (166 views)
2013-05-12 15:36:09

UnluckyDevil (175 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.125 seconds with 20 queries.