From ca4073dbd9a81fcc0263300c66acd9057e4d1020 Mon Sep 17 00:00:00 2001 From: "sigonasr2, Sig, Sigo" Date: Tue, 14 Jun 2022 19:19:04 +0000 Subject: [PATCH] Setup baseline for physics objects. Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com> Co-authored-by: sigonasr2 --- Java/scripts/md5 | 2 +- src/sig/objects/Player.java | 200 ++-------------- src/sig/objects/actor/PhysicsObject.java | 225 ++++++++++++++++++ .../actor/PhysicsObjectRequirements.java | 6 + src/sig/objects/actor/RenderedObject.java | 1 - 5 files changed, 250 insertions(+), 184 deletions(-) create mode 100644 src/sig/objects/actor/PhysicsObject.java create mode 100644 src/sig/objects/actor/PhysicsObjectRequirements.java diff --git a/Java/scripts/md5 b/Java/scripts/md5 index fd1d942..72ae9a9 100644 --- a/Java/scripts/md5 +++ b/Java/scripts/md5 @@ -1,4 +1,4 @@ -build2.sh:cf159b0f2193ff280c455a7547126073 - +build2.sh:b1c6b7c6b2eb19ba54be6824bda8805b - build.sh:5df470d49036bf7565493739aeebeaa8 - clean.sh:668a2f9b568c55f6a044a509315032f6 - commit.sh:5e4448db9ad48e72ec3a1ff4f5763b41 - diff --git a/src/sig/objects/Player.java b/src/sig/objects/Player.java index 8c4eac2..a42d5f5 100644 --- a/src/sig/objects/Player.java +++ b/src/sig/objects/Player.java @@ -6,28 +6,22 @@ import sig.engine.Panel; import sig.engine.Rectangle; import sig.engine.Sprite; import sig.engine.Transform; -import sig.engine.objects.AnimatedObject; -import sig.engine.objects.CollisionEntity; import sig.map.Map; import sig.map.Tile; import sig.map.View; +import sig.objects.actor.PhysicsObject; import sig.objects.actor.RenderedObject; import sig.objects.actor.State; import sig.utils.TimeUtils; -public class Player extends AnimatedObject implements CollisionEntity,RenderedObject{ - final static double GRAVITY = 1300; - final static double NORMAL_FRICTION = 6400; - final static double NORMAL_JUMP_VELOCITY = -300; +public class Player extends PhysicsObject implements RenderedObject{ final static boolean LEFT = false; final static boolean RIGHT = true; final static long jump_fall_AnimationWaitTime = TimeUtils.millisToNanos(200); final static long slide_AnimationWaitTime = TimeUtils.millisToNanos(100); final static long slide_duration = TimeUtils.millisToNanos(700); - final static double WALKING_SPEED_LIMIT = 164; - - double y_acceleration = GRAVITY; + double y_acceleration = PhysicsObject.GRAVITY; double y_acceleration_limit = 100; double x_acceleration = 0; double x_acceleration_limit = 100; @@ -39,11 +33,11 @@ public class Player extends AnimatedObject implements CollisionEntity,RenderedOb double sliding_acceleration = 120; double horizontal_drag = 2000; - double horizontal_friction = NORMAL_FRICTION; + double horizontal_friction = PhysicsObject.NORMAL_FRICTION; double horizontal_air_drag = 800; double horizontal_air_friction = 180; - double jump_velocity = NORMAL_JUMP_VELOCITY; + double jump_velocity = PhysicsObject.NORMAL_JUMP_VELOCITY; int maxJumpCount = 2; int jumpCount = maxJumpCount; @@ -88,6 +82,7 @@ public class Player extends AnimatedObject implements CollisionEntity,RenderedOb if (prvState != State.FALLING) { jump_slide_fall_StartAnimationTimer = RabiClone.TIME; setAnimatedSpr(Sprite.ERINA_JUMP_FALL1); + spacebarPressed = 0; } if (RabiClone.TIME - jump_slide_fall_StartAnimationTimer > jump_fall_AnimationWaitTime) { setAnimatedSpr(Sprite.ERINA_JUMP_FALL); @@ -100,8 +95,8 @@ public class Player extends AnimatedObject implements CollisionEntity,RenderedOb break; } - jump_velocity = NORMAL_JUMP_VELOCITY; - horizontal_friction = NORMAL_FRICTION; + jump_velocity = PhysicsObject.NORMAL_JUMP_VELOCITY; + horizontal_friction = PhysicsObject.NORMAL_FRICTION; jump_slide_fall_StartAnimationTimer = -1; if (x_velocity != 0) { @@ -340,175 +335,6 @@ public class Player extends AnimatedObject implements CollisionEntity,RenderedOb RabiClone.level_renderer.setY(newY < 0 ? 0 : newY); } - private void handleMovementPhysics(double updateMult) { - int right = KeyHeld(Action.MOVE_RIGHT)?1:0; - int left = KeyHeld(Action.MOVE_LEFT)?1:0; - if(state==State.SLIDE){ - right=0; - left=0; - } - x_velocity = - Math.abs(x_velocity+x_acceleration*updateMult)>x_velocity_limit - ?Math.signum(x_velocity+x_acceleration*updateMult)*x_velocity_limit - :x_velocity+x_acceleration*updateMult; - y_velocity = - 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 = y_velocity*updateMult; - double displacement_x = x_velocity*updateMult; - - boolean sideCollision = false; - boolean hitAbove=false; - if (y_velocity==0) { - if (!(checkCollision(getX()-getSprite().getWidth()/2+getCollisionBox().getX2(),getY()-getSprite().getHeight()/2+getCollisionBounds().getY2()+2)|| - checkCollision(getX()-getSprite().getWidth()/2+getCollisionBox().getX(),getY()-getSprite().getHeight()/2+getCollisionBounds().getY2()+2))) { - groundCollision=false; - } else { - groundCollision=true; - jumpCount=maxJumpCount; - } - } else { - double startingY=getY(); - groundCollision=false; - if (displacement_y>0) { - for (int y=(int)getY();ystartingY+displacement_y;y--) { - if (y==getY()) { - continue; - } - if (checkCollision(getX()-getSprite().getWidth()/2+getCollisionBox().getX2()-1,y+getCollisionBox().getY()-getSprite().getHeight()/2)|| - checkCollision(getX()-getSprite().getWidth()/2+getCollisionBox().getX()+1,y+getCollisionBox().getY()-getSprite().getHeight()/2)) { - setY(y+1); - hitAbove=true; - spacebarPressed=0; - state=State.FALLING; - y_acceleration = 0; - y_velocity = 0; - displacement_y=0; - //groundCollision = true; - break; - } - } - } - } - - double startingX=getX(); - if (displacement_x>0.00001) { - for (int x=(int)getX();xstartingX+displacement_x;x--) { - if (x==getX()) { - continue; - } - if (checkCollision((x+getCollisionBox().getX()-getSprite().getWidth()/2),getY()-getSprite().getHeight()/2+getCollisionBounds().getY2())|| - checkCollision((x+getCollisionBox().getX()-getSprite().getWidth()/2),getY()-getSprite().getHeight()/2+getCollisionBounds().getY())) { - if (!(checkCollision(x+getCollisionBox().getX()-getSprite().getWidth()/2,(getY()-getSprite().getHeight()/2+getCollisionBounds().getY2()-2))|| - checkCollision(x+getCollisionBox().getX()-getSprite().getWidth()/2,(getY()-getSprite().getHeight()/2+getCollisionBounds().getY()-2)))) { - setY(getY()-1); - } else { - x_acceleration = 0; - x_velocity = -0.000001; - sideCollision=true; - setX(x+0.1); - break; - } - } else { - if (!hitAbove&&!checkCollision(x+getCollisionBox().getX2()-getSprite().getWidth()/2,(getY()-getSprite().getHeight()/2+getCollisionBounds().getY2()+1))&& - checkCollision(x+getCollisionBox().getX2()-getSprite().getWidth()/2,(getY()-getSprite().getHeight()/2+getCollisionBounds().getY2()+2))) { - //System.out.println("Performs check."+System.currentTimeMillis()); - setY(getY()+1); - //x_velocity = ; - } - } - } - } - if (!groundCollision){ - this.setY(this.getY()+displacement_y); - y_acceleration = GRAVITY; - if(y_velocity>0 && state!=State.SLIDE){ - state = State.FALLING; - } - if (!sideCollision) { - handleKeyboardMovement(updateMult, right-left, horizontal_air_friction, horizontal_air_drag); - this.setX(this.getX()+displacement_x); - } - } else { - if (!sideCollision) { - handleKeyboardMovement(updateMult, right-left, horizontal_friction, horizontal_drag); - this.setX(this.getX()+displacement_x); - } - } - } - - private boolean checkCollision(double x,double y) { - int index = (int)y*RabiClone.BASE_WIDTH*Tile.TILE_WIDTH+(int)x; - if (index>=0&&index 0 - ? x_velocity - friction * updateMult > 0 - ? x_velocity - friction * updateMult - : 0 - : x_velocity + friction * updateMult < 0 - ? x_velocity + friction * updateMult - : 0; - } - x_acceleration = 0; - } - } - @Override public void draw(byte[] p) { } @@ -535,4 +361,14 @@ public class Player extends AnimatedObject implements CollisionEntity,RenderedOb public Transform getSpriteTransform() { return facing_direction?Transform.HORIZONTAL:Transform.NONE; } + + @Override + public boolean rightKeyHeld() { + return KeyHeld(Action.MOVE_RIGHT); + } + + @Override + public boolean leftKeyHeld() { + return KeyHeld(Action.MOVE_LEFT); + } } diff --git a/src/sig/objects/actor/PhysicsObject.java b/src/sig/objects/actor/PhysicsObject.java new file mode 100644 index 0000000..cf2bb6c --- /dev/null +++ b/src/sig/objects/actor/PhysicsObject.java @@ -0,0 +1,225 @@ +package sig.objects.actor; + +import sig.RabiClone; +import sig.engine.AnimatedSprite; +import sig.engine.Panel; +import sig.engine.Rectangle; +import sig.engine.Transform; +import sig.engine.objects.AnimatedObject; +import sig.engine.objects.CollisionEntity; +import sig.map.Tile; + +public abstract class PhysicsObject extends AnimatedObject implements CollisionEntity,PhysicsObjectRequirements{ + final public static double GRAVITY = 1300; + final public static double NORMAL_FRICTION = 6400; + final public static double NORMAL_JUMP_VELOCITY = -300; + final public static double WALKING_SPEED_LIMIT = 164; + + State state = State.IDLE; + double x_velocity,y_velocity; + double x_acceleration,y_acceleration; + double x_velocity_limit,y_velocity_limit; + boolean groundCollision; + byte maxJumpCount=2; + byte jumpCount=0; + + double horizontal_air_friction,horizontal_air_drag; + double horizontal_friction,horizontal_drag; + + protected PhysicsObject(AnimatedSprite spr, double animationSpd, Panel panel) { + super(spr, animationSpd, panel); + } + + + + @Override + public void update(double updateMult) { + super.update(updateMult); + handleMovementPhysics(updateMult); + } + + protected void handleMovementPhysics(double updateMult) { + int right = rightKeyHeld()?1:0; + int left = leftKeyHeld()?1:0; + if(state==State.SLIDE){ + right=0; + left=0; + } + x_velocity = + Math.abs(x_velocity+x_acceleration*updateMult)>x_velocity_limit + ?Math.signum(x_velocity+x_acceleration*updateMult)*x_velocity_limit + :x_velocity+x_acceleration*updateMult; + y_velocity = + 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 = y_velocity*updateMult; + double displacement_x = x_velocity*updateMult; + + boolean sideCollision = false; + boolean hitAbove=false; + if (y_velocity==0) { + if (!(checkCollision(getX()-getSprite().getWidth()/2+getCollisionBox().getX2(),getY()-getSprite().getHeight()/2+getCollisionBounds().getY2()+2)|| + checkCollision(getX()-getSprite().getWidth()/2+getCollisionBox().getX(),getY()-getSprite().getHeight()/2+getCollisionBounds().getY2()+2))) { + groundCollision=false; + } else { + groundCollision=true; + jumpCount=maxJumpCount; + } + } else { + double startingY=getY(); + groundCollision=false; + if (displacement_y>0) { + for (int y=(int)getY();ystartingY+displacement_y;y--) { + if (y==getY()) { + continue; + } + if (checkCollision(getX()-getSprite().getWidth()/2+getCollisionBox().getX2()-1,y+getCollisionBox().getY()-getSprite().getHeight()/2)|| + checkCollision(getX()-getSprite().getWidth()/2+getCollisionBox().getX()+1,y+getCollisionBox().getY()-getSprite().getHeight()/2)) { + setY(y+1); + hitAbove=true; + state=State.FALLING; + y_acceleration = 0; + y_velocity = 0; + displacement_y=0; + //groundCollision = true; + break; + } + } + } + } + + double startingX=getX(); + if (displacement_x>0.00001) { + for (int x=(int)getX();xstartingX+displacement_x;x--) { + if (x==getX()) { + continue; + } + if (checkCollision((x+getCollisionBox().getX()-getSprite().getWidth()/2),getY()-getSprite().getHeight()/2+getCollisionBounds().getY2())|| + checkCollision((x+getCollisionBox().getX()-getSprite().getWidth()/2),getY()-getSprite().getHeight()/2+getCollisionBounds().getY())) { + if (!(checkCollision(x+getCollisionBox().getX()-getSprite().getWidth()/2,(getY()-getSprite().getHeight()/2+getCollisionBounds().getY2()-2))|| + checkCollision(x+getCollisionBox().getX()-getSprite().getWidth()/2,(getY()-getSprite().getHeight()/2+getCollisionBounds().getY()-2)))) { + setY(getY()-1); + } else { + x_acceleration = 0; + x_velocity = -0.000001; + sideCollision=true; + setX(x+0.1); + break; + } + } else { + if (!hitAbove&&!checkCollision(x+getCollisionBox().getX2()-getSprite().getWidth()/2,(getY()-getSprite().getHeight()/2+getCollisionBounds().getY2()+1))&& + checkCollision(x+getCollisionBox().getX2()-getSprite().getWidth()/2,(getY()-getSprite().getHeight()/2+getCollisionBounds().getY2()+2))) { + //System.out.println("Performs check."+System.currentTimeMillis()); + setY(getY()+1); + //x_velocity = ; + } + } + } + } + if (!groundCollision){ + this.setY(this.getY()+displacement_y); + y_acceleration = GRAVITY; + if(y_velocity>0 && state!=State.SLIDE){ + state = State.FALLING; + } + if (!sideCollision) { + handleKeyboardMovement(updateMult, right-left, horizontal_air_friction, horizontal_air_drag); + this.setX(this.getX()+displacement_x); + } + } else { + if (!sideCollision) { + handleKeyboardMovement(updateMult, right-left, horizontal_friction, horizontal_drag); + this.setX(this.getX()+displacement_x); + } + } + } + + protected boolean checkCollision(double x,double y) { + int index = (int)y*RabiClone.BASE_WIDTH*Tile.TILE_WIDTH+(int)x; + if (index>=0&&index 0 + ? x_velocity - friction * updateMult > 0 + ? x_velocity - friction * updateMult + : 0 + : x_velocity + friction * updateMult < 0 + ? x_velocity + friction * updateMult + : 0; + } + x_acceleration = 0; + } + } + + @Override + public void draw(byte[] p) {} + + @Override + public Transform getSpriteTransform() { + return null; + } + + @Override + public Rectangle getCollisionBounds() { + return null; + } + + @Override + public void setCollisionBounds(Rectangle bounds){} + +} diff --git a/src/sig/objects/actor/PhysicsObjectRequirements.java b/src/sig/objects/actor/PhysicsObjectRequirements.java new file mode 100644 index 0000000..c4b475e --- /dev/null +++ b/src/sig/objects/actor/PhysicsObjectRequirements.java @@ -0,0 +1,6 @@ +package sig.objects.actor; + +public interface PhysicsObjectRequirements { + boolean rightKeyHeld(); + boolean leftKeyHeld(); +} diff --git a/src/sig/objects/actor/RenderedObject.java b/src/sig/objects/actor/RenderedObject.java index bc227cb..26c7809 100644 --- a/src/sig/objects/actor/RenderedObject.java +++ b/src/sig/objects/actor/RenderedObject.java @@ -1,5 +1,4 @@ package sig.objects.actor; public interface RenderedObject { - }