Include side block collisions

Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com>
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
sigonasr2 3 years ago
parent 65294a1343
commit 7eb988061d
  1. BIN
      maps/world1.map
  2. 52
      src/sig/objects/Player.java

Binary file not shown.

@ -10,7 +10,10 @@ import sig.map.Tile;
import java.awt.event.KeyEvent;
public class Player extends Object{
double y_acceleration = 1;
final double GRAVITY = 90;
final double NORMAL_FRICTION = 500;
double y_acceleration = GRAVITY;
double y_acceleration_limit = 100;
double x_acceleration = 0;
double x_acceleration_limit = 100;
@ -20,7 +23,7 @@ public class Player extends Object{
double y_velocity_limit = 192;
double horizontal_drag = 2000;
double horizontal_friction = 500;
double horizontal_friction = NORMAL_FRICTION;
double horizontal_air_drag = 100;
double horizontal_air_friction = 7;
@ -46,29 +49,50 @@ public class Player extends Object{
Math.abs(y_velocity+y_acceleration*updateMult)>y_velocity_limit
?Math.signum(y_velocity+y_acceleration*updateMult)*y_velocity_limit
:y_velocity+y_acceleration*updateMult;
double displacement = y_velocity*updateMult;
double displacement_y = y_velocity*updateMult;
double displacement_x = x_velocity*updateMult;
boolean groundCollision = false;
boolean sideCollision = false;
//System.out.println(x_velocity);
//System.out.println(((int)(getX()+getSprite().getWidth()/2+displacement_x)/Tile.TILE_WIDTH)+"//"+((int)(getY()+getSprite().getHeight()/2)/Tile.TILE_HEIGHT));
for(int i=0;i<4;i++){
double check_distance = (displacement/4)*(i+1);
Tile checked_tile_top = RabiClone.CURRENT_MAP.getTile((int)getX()/Tile.TILE_WIDTH, (int)(getY()+check_distance)/Tile.TILE_HEIGHT);
Tile checked_tile_bottom_right = RabiClone.CURRENT_MAP.getTile((int)(getX()+getSprite().getWidth()/2)/Tile.TILE_WIDTH, (int)(getY()+getSprite().getHeight()/2+check_distance)/Tile.TILE_HEIGHT);
Tile checked_tile_bottom_left = RabiClone.CURRENT_MAP.getTile((int)(getX()-getSprite().getWidth()/2)/Tile.TILE_WIDTH, (int)(getY()+getSprite().getHeight()/2+check_distance)/Tile.TILE_HEIGHT);
if (sideCollision&&groundCollision) {
break;
}
double check_distance_y = (displacement_y/4)*(i+1);
double check_distance_x = (displacement_x/4)*(i+1);
Tile checked_tile_top_right = RabiClone.CURRENT_MAP.getTile((int)(getX()+getSprite().getWidth()/2-4+check_distance_x)/Tile.TILE_WIDTH, (int)(getY())/Tile.TILE_HEIGHT);
Tile checked_tile_top_left = RabiClone.CURRENT_MAP.getTile((int)(getX()-getSprite().getWidth()/2+4+check_distance_x)/Tile.TILE_WIDTH, (int)(getY())/Tile.TILE_HEIGHT);
Tile checked_tile_bottom_right = RabiClone.CURRENT_MAP.getTile((int)(getX()+getSprite().getWidth()/2)/Tile.TILE_WIDTH, (int)(getY()+getSprite().getHeight()/2+check_distance_y)/Tile.TILE_HEIGHT);
Tile checked_tile_bottom_left = RabiClone.CURRENT_MAP.getTile((int)(getX()-getSprite().getWidth()/2)/Tile.TILE_WIDTH, (int)(getY()+getSprite().getHeight()/2+check_distance_y)/Tile.TILE_HEIGHT);
//System.out.println((int)getX()/Tile.TILE_WIDTH);
if(checked_tile_bottom_right.getCollision()==CollisionType.BLOCK||checked_tile_bottom_left.getCollision()==CollisionType.BLOCK){
setY((getY()+check_distance));
if(!groundCollision&&checked_tile_bottom_right.getCollision()==CollisionType.BLOCK||checked_tile_bottom_left.getCollision()==CollisionType.BLOCK){
setY((getY()+check_distance_y));
y_acceleration = 0;
y_velocity = 0;
groundCollision=true;
break;
}
if(!sideCollision&&checked_tile_top_right.getCollision()==CollisionType.BLOCK||checked_tile_top_left.getCollision()==CollisionType.BLOCK){
//System.out.println(checked_tile_top_right.getCollision()+"//"+checked_tile_top_left.getCollision());
setX((getX()-check_distance_x));
x_acceleration = 0;
x_velocity = 0;
sideCollision=true;
}
}
if (!groundCollision){
this.setY(this.getY()+displacement);
handleKeyboardMovement(updateMult, right-left, horizontal_air_friction, horizontal_air_drag);
this.setY(this.getY()+displacement_y);
y_acceleration = GRAVITY;
if (!sideCollision) {
handleKeyboardMovement(updateMult, right-left, horizontal_air_friction, horizontal_air_drag);
this.setX(this.getX()+displacement_x);
}
} else {
handleKeyboardMovement(updateMult, right-left, horizontal_friction, horizontal_drag);
if (!sideCollision) {
handleKeyboardMovement(updateMult, right-left, horizontal_friction, horizontal_drag);
this.setX(this.getX()+displacement_x);
}
}
this.setX(this.getX()+x_velocity*updateMult);
}

Loading…
Cancel
Save