Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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 285 times)
0 Members and 1 Guest are viewing this topic.
Offline Ed_RockStarGuy
« Posted 2013-03-16 15: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  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

Mr.CodeIt (24 views)
2014-12-23 03:34:11

rwatson462 (55 views)
2014-12-15 09:26:44

Mr.CodeIt (45 views)
2014-12-14 19:50:38

BurntPizza (86 views)
2014-12-09 22:41:13

BurntPizza (110 views)
2014-12-08 04:46:31

JscottyBieshaar (81 views)
2014-12-05 12:39:02

SHC (91 views)
2014-12-03 16:27:13

CopyableCougar4 (98 views)
2014-11-29 21:32:03

toopeicgaming1999 (157 views)
2014-11-26 15:22:04

toopeicgaming1999 (154 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!