Fixed staircase climbing collision stuck/bugs

origin
Joshua Sigona 3 years ago
parent 817231fc49
commit d63f8ea2fa
  1. 10
      src/sig/Block.java
  2. 10
      src/sig/BlockDefinition.java
  3. 4
      src/sig/Mesh.java
  4. 47
      src/sig/SigRenderer.java
  5. 45
      src/sig/models/Staircase.java

@ -111,6 +111,16 @@ public class Block {
if (b.block instanceof Staircase && block instanceof Staircase) { if (b.block instanceof Staircase && block instanceof Staircase) {
neighbors.DOWN=false; neighbors.DOWN=false;
b.neighbors.UP=true; b.neighbors.UP=true;
} else
if ((b.block instanceof Cube && block instanceof Staircase)||(b.block instanceof Staircase && block instanceof Cube)) {
Block staircase = (b.block instanceof Staircase)?b:this;
if (b.equals(staircase)) {
neighbors.DOWN=b.neighbors.UP=false;
} else {
neighbors.DOWN=false;
b.neighbors.UP=true;
}
} else { } else {
neighbors.DOWN=b.neighbors.UP=block.triangles.get(Texture.BOTTOM).tex.hasTransparency==b.block.triangles.get(Texture.TOP).tex.hasTransparency&&block.triangles.get(Texture.BOTTOM).tex.hasTranslucency==b.block.triangles.get(Texture.TOP).tex.hasTranslucency; neighbors.DOWN=b.neighbors.UP=block.triangles.get(Texture.BOTTOM).tex.hasTransparency==b.block.triangles.get(Texture.TOP).tex.hasTransparency&&block.triangles.get(Texture.BOTTOM).tex.hasTranslucency==b.block.triangles.get(Texture.TOP).tex.hasTranslucency;
} }

@ -0,0 +1,10 @@
package sig;
public class BlockDefinition {
Class<?> cl;
BlockType type;
BlockDefinition(Class<?> cl,BlockType type) {
this.cl=cl;
this.type=type;
}
}

@ -30,4 +30,8 @@ public class Mesh {
public boolean handleCollision(Block b,float x,float y) { public boolean handleCollision(Block b,float x,float y) {
return false; return false;
} }
@Override
public String toString() {
return "Mesh [triangles=" + triangles + ", type=" + type + "]";
}
} }

@ -7,6 +7,7 @@ import sig.models.Staircase;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseListener; import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener; import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster; import java.awt.image.WritableRaster;
import java.io.File; import java.io.File;
@ -26,8 +27,9 @@ import java.awt.Point;
import java.awt.Cursor; import java.awt.Cursor;
import java.awt.GraphicsDevice; import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment; import java.awt.GraphicsEnvironment;
import java.awt.event.MouseWheelListener;
public class SigRenderer implements KeyListener,MouseListener,MouseMotionListener{ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListener,MouseWheelListener{
public static boolean WIREFRAME = false; public static boolean WIREFRAME = false;
public static boolean PROFILING = false; public static boolean PROFILING = false;
@ -100,6 +102,8 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
public static Cursor invisibleCursor; public static Cursor invisibleCursor;
public static int selectedMode=0;
void addSpeed(Vector v) { void addSpeed(Vector v) {
//vCameraSpeed = Vector.add(vCameraSpeed,v); //vCameraSpeed = Vector.add(vCameraSpeed,v);
xSpeed+=v.x; xSpeed+=v.x;
@ -112,12 +116,6 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
Block b2 = blockGrid.get((float)Math.floor(vCamera.x+x-cameraCollisionPadding)+"_"+(float)Math.floor(vCamera.y+y+yy)+"_"+(float)Math.floor(vCamera.z+z+cameraCollisionPadding)); Block b2 = blockGrid.get((float)Math.floor(vCamera.x+x-cameraCollisionPadding)+"_"+(float)Math.floor(vCamera.y+y+yy)+"_"+(float)Math.floor(vCamera.z+z+cameraCollisionPadding));
Block b3 = blockGrid.get((float)Math.floor(vCamera.x+x+cameraCollisionPadding)+"_"+(float)Math.floor(vCamera.y+y+yy)+"_"+(float)Math.floor(vCamera.z+z-cameraCollisionPadding)); Block b3 = blockGrid.get((float)Math.floor(vCamera.x+x+cameraCollisionPadding)+"_"+(float)Math.floor(vCamera.y+y+yy)+"_"+(float)Math.floor(vCamera.z+z-cameraCollisionPadding));
Block b4 = blockGrid.get((float)Math.floor(vCamera.x+x-cameraCollisionPadding)+"_"+(float)Math.floor(vCamera.y+y+yy)+"_"+(float)Math.floor(vCamera.z+z-cameraCollisionPadding)); Block b4 = blockGrid.get((float)Math.floor(vCamera.x+x-cameraCollisionPadding)+"_"+(float)Math.floor(vCamera.y+y+yy)+"_"+(float)Math.floor(vCamera.z+z-cameraCollisionPadding));
if (b1!=null && !(b1.block instanceof Staircase)||
b2!=null && !(b2.block instanceof Staircase)||
b3!=null && !(b3.block instanceof Staircase)||
b4!=null && !(b4.block instanceof Staircase)) {
return false;
}
if (b1!=null) {if (!b1.block.handleCollision(b1,x,z)) {return false;}} if (b1!=null) {if (!b1.block.handleCollision(b1,x,z)) {return false;}}
if (b2!=null) {if (!b2.block.handleCollision(b2,x,z)) {return false;}} if (b2!=null) {if (!b2.block.handleCollision(b2,x,z)) {return false;}}
if (b3!=null) {if (!b3.block.handleCollision(b3,x,z)) {return false;}} if (b3!=null) {if (!b3.block.handleCollision(b3,x,z)) {return false;}}
@ -298,24 +296,35 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
dirVal=directions[(index+answer.t.b.getFacingDirection().ordinal())%4]; dirVal=directions[(index+answer.t.b.getFacingDirection().ordinal())%4];
} }
} }
BlockDefinition bl = null;
switch (selectedMode) {
case 1:{
bl = new BlockDefinition(Staircase.class,BlockType.PLANKS);
}break;
default:{
bl = new BlockDefinition(Cube.class,BlockType.DIRT);
}break;
}
switch (dirVal) { switch (dirVal) {
case BlockType.FRONT:{ case BlockType.FRONT:{
addBlock(Vector.add(answer.t.b.pos,new Vector(0,0,-1)),Staircase.class,BlockType.PLANKS,FacingDirection.SOUTH); addBlock(Vector.add(answer.t.b.pos,new Vector(0,0,-1)),bl.cl,bl.type,FacingDirection.SOUTH);
}break; }break;
case BlockType.BACK:{ case BlockType.BACK:{
addBlock(Vector.add(answer.t.b.pos,new Vector(0,0,1)),Staircase.class,BlockType.PLANKS,FacingDirection.SOUTH); addBlock(Vector.add(answer.t.b.pos,new Vector(0,0,1)),bl.cl,bl.type,FacingDirection.SOUTH);
}break; }break;
case BlockType.LEFT:{ case BlockType.LEFT:{
addBlock(Vector.add(answer.t.b.pos,new Vector(-1,0,0)),Staircase.class,BlockType.PLANKS,FacingDirection.SOUTH); addBlock(Vector.add(answer.t.b.pos,new Vector(-1,0,0)),bl.cl,bl.type,FacingDirection.SOUTH);
}break; }break;
case BlockType.RIGHT:{ case BlockType.RIGHT:{
addBlock(Vector.add(answer.t.b.pos,new Vector(1,0,0)),Staircase.class,BlockType.PLANKS,FacingDirection.SOUTH); addBlock(Vector.add(answer.t.b.pos,new Vector(1,0,0)),bl.cl,bl.type,FacingDirection.SOUTH);
}break; }break;
case BlockType.TOP:{ case BlockType.TOP:{
addBlock(Vector.add(answer.t.b.pos,new Vector(0,1,0)),Staircase.class,BlockType.PLANKS,FacingDirection.SOUTH); addBlock(Vector.add(answer.t.b.pos,new Vector(0,1,0)),bl.cl,bl.type,FacingDirection.SOUTH);
}break; }break;
case BlockType.BOTTOM:{ case BlockType.BOTTOM:{
addBlock(Vector.add(answer.t.b.pos,new Vector(0,-1,0)),Staircase.class,BlockType.PLANKS,FacingDirection.SOUTH); addBlock(Vector.add(answer.t.b.pos,new Vector(0,-1,0)),bl.cl,bl.type,FacingDirection.SOUTH);
}break; }break;
} }
} else } else
@ -418,6 +427,7 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
f.getContentPane().addMouseListener(this); f.getContentPane().addMouseListener(this);
f.getContentPane().addMouseMotionListener(this); f.getContentPane().addMouseMotionListener(this);
f.addKeyListener(this); f.addKeyListener(this);
f.getContentPane().addMouseWheelListener(this);
f.setSize(SCREEN_WIDTH,SCREEN_HEIGHT); f.setSize(SCREEN_WIDTH,SCREEN_HEIGHT);
f.add(panel,BorderLayout.CENTER); f.add(panel,BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
@ -519,10 +529,15 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
@Override @Override
public void mouseDragged(MouseEvent e) { public void mouseDragged(MouseEvent e) {
controlCamera(e);
} }
@Override @Override
public void mouseMoved(MouseEvent e) { public void mouseMoved(MouseEvent e) {
controlCamera(e);
}
private void controlCamera(MouseEvent e) {
Point middle = new Point((int)(panel.getLocationOnScreen().x+panel.getWidth()/2), (int)(panel.getLocationOnScreen().y+panel.getHeight()/2)); Point middle = new Point((int)(panel.getLocationOnScreen().x+panel.getWidth()/2), (int)(panel.getLocationOnScreen().y+panel.getHeight()/2));
//System.out.println((middle.x-e.getXOnScreen())+","+(middle.y-e.getYOnScreen())); //System.out.println((middle.x-e.getXOnScreen())+","+(middle.y-e.getYOnScreen()));
int diffX=Math.max(-100,Math.min(100,e.getXOnScreen()-middle.x)); int diffX=Math.max(-100,Math.min(100,e.getXOnScreen()-middle.x));
@ -623,4 +638,10 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
}break; }break;
} }
} }
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
selectedMode+=Math.signum(e.getWheelRotation());
System.out.println("Mode "+selectedMode+".");
}
} }

@ -84,23 +84,22 @@ public class Staircase extends Mesh{
Block b3 = SigRenderer.blockGrid.get((float)Math.floor(x+SigRenderer.cameraCollisionPadding)+"_"+(float)Math.floor(y+yy)+"_"+(float)Math.floor(z-SigRenderer.cameraCollisionPadding)); Block b3 = SigRenderer.blockGrid.get((float)Math.floor(x+SigRenderer.cameraCollisionPadding)+"_"+(float)Math.floor(y+yy)+"_"+(float)Math.floor(z-SigRenderer.cameraCollisionPadding));
Block b4 = SigRenderer.blockGrid.get((float)Math.floor(x-SigRenderer.cameraCollisionPadding)+"_"+(float)Math.floor(y+yy)+"_"+(float)Math.floor(z-SigRenderer.cameraCollisionPadding)); Block b4 = SigRenderer.blockGrid.get((float)Math.floor(x-SigRenderer.cameraCollisionPadding)+"_"+(float)Math.floor(y+yy)+"_"+(float)Math.floor(z-SigRenderer.cameraCollisionPadding));
//System.out.println(b1+","+b2+","+b3+","+b4); //System.out.println(b1+","+b2+","+b3+","+b4);
if ((b1!=null&&!b1.block.equals(staircaseCheck))|| if ((b1!=null)||
(b2!=null&&!b2.block.equals(staircaseCheck))|| (b2!=null)||
(b3!=null&&!b3.block.equals(staircaseCheck))|| (b3!=null)||
(b4!=null&&!b4.block.equals(staircaseCheck))) { (b4!=null)) {
return false; return false;
} }
} }
return true; return true;
} }
public boolean handleCollision(Block b,float x,float z) { public boolean handleCollision(Block b,float x,float z) {
if (SigRenderer.currentStaircase!=null&& if (SigRenderer.currentStaircase!=null&&b.pos.y==SigRenderer.currentStaircase.triangles.get(0).b.pos.y) {
SigRenderer.currentStaircase==this) { float diffX=Math.min(1,Math.max(0f,SigRenderer.vCamera.x-b.pos.x));
float diffX=Math.min(1,Math.max(0.3f,SigRenderer.vCamera.x+x-b.pos.x)); float diffZ=Math.min(1,Math.max(0f,SigRenderer.vCamera.z-b.pos.z));
float diffZ=Math.min(1,Math.max(0.3f,SigRenderer.vCamera.z+z-b.pos.z));
switch (b.getFacingDirection()) { switch (b.getFacingDirection()) {
case EAST: { case EAST: {
if (checkCollision(SigRenderer.vCamera.x+x, b.pos.y+diffX+1.3f, SigRenderer.vCamera.z+z,SigRenderer.currentStaircase)) { if (checkCollision(SigRenderer.vCamera.x+x, b.pos.y+diffX+x+1.3f, SigRenderer.vCamera.z+z,SigRenderer.currentStaircase)) {
SigRenderer.vCamera.y=b.pos.y+diffX+0.3f; SigRenderer.vCamera.y=b.pos.y+diffX+0.3f;
} else { } else {
SigRenderer.fallSpd=0; SigRenderer.fallSpd=0;
@ -108,7 +107,7 @@ public class Staircase extends Mesh{
} }
}break; }break;
case NORTH: { case NORTH: {
if (checkCollision(SigRenderer.vCamera.x+x, b.pos.y+(1-diffZ)+1.3f, SigRenderer.vCamera.z+z,SigRenderer.currentStaircase)) { if (checkCollision(SigRenderer.vCamera.x+x, b.pos.y+(1-diffZ+z)+1.3f, SigRenderer.vCamera.z+z,SigRenderer.currentStaircase)) {
SigRenderer.vCamera.y=b.pos.y+(1-diffZ)+0.3f; SigRenderer.vCamera.y=b.pos.y+(1-diffZ)+0.3f;
} else { } else {
SigRenderer.fallSpd=0; SigRenderer.fallSpd=0;
@ -116,7 +115,7 @@ public class Staircase extends Mesh{
} }
}break; }break;
case SOUTH: { case SOUTH: {
if (checkCollision(SigRenderer.vCamera.x+x, b.pos.y+diffZ+1.3f, SigRenderer.vCamera.z+z,SigRenderer.currentStaircase)) { if (checkCollision(SigRenderer.vCamera.x+x, b.pos.y+diffZ+z+1.3f, SigRenderer.vCamera.z+z,SigRenderer.currentStaircase)) {
SigRenderer.vCamera.y=b.pos.y+diffZ+0.3f; SigRenderer.vCamera.y=b.pos.y+diffZ+0.3f;
} else { } else {
SigRenderer.fallSpd=0; SigRenderer.fallSpd=0;
@ -124,7 +123,7 @@ public class Staircase extends Mesh{
} }
}break; }break;
case WEST: { case WEST: {
if (checkCollision(SigRenderer.vCamera.x+x, b.pos.y+(1-diffX)+1.3f, SigRenderer.vCamera.z+z,SigRenderer.currentStaircase)) { if (checkCollision(SigRenderer.vCamera.x+x, b.pos.y+(1-diffX+x)+1.3f, SigRenderer.vCamera.z+z,SigRenderer.currentStaircase)) {
SigRenderer.vCamera.y=b.pos.y+(1-diffX)+0.3f; SigRenderer.vCamera.y=b.pos.y+(1-diffX)+0.3f;
} else { } else {
SigRenderer.fallSpd=0; SigRenderer.fallSpd=0;
@ -135,41 +134,39 @@ public class Staircase extends Mesh{
SigRenderer.fallSpd=0; SigRenderer.fallSpd=0;
return true; return true;
} }
if (this!=SigRenderer.currentStaircase && SigRenderer.vCamera.y>=b.pos.y) { if (SigRenderer.vCamera.y>=b.pos.y&&SigRenderer.vCamera.y<b.pos.y+1) {
boolean valid=false; boolean valid=false;
if (SigRenderer.vCamera.y>=b.pos.y+3f) { if (SigRenderer.vCamera.y>=b.pos.y+3f) {
valid=true; valid=true;
} else { } else {
float diffX=Math.min(1,Math.max(0.3f,SigRenderer.vCamera.x+x-b.pos.x));
float diffZ=Math.min(1,Math.max(0.3f,SigRenderer.vCamera.z+z-b.pos.z));
switch (b.getFacingDirection()) { switch (b.getFacingDirection()) {
case EAST: { case EAST: {
if (SigRenderer.vCamera.x<b.pos.x+0.5f&&checkCollision(SigRenderer.vCamera.x+x, b.pos.y+diffX+0.3f, SigRenderer.vCamera.z+z,this)) { if (SigRenderer.vCamera.x<b.pos.x+0.5f) {
valid=true; valid=true;
} }
}break; }break;
case NORTH: { case NORTH: {
if (SigRenderer.vCamera.z>b.pos.z+0.5f&&checkCollision(SigRenderer.vCamera.x+x, b.pos.y+(1-diffZ)+0.3f, SigRenderer.vCamera.z+z,this)) { if (SigRenderer.vCamera.z>b.pos.z+0.5f) {
valid=true; valid=true;
} }
}break; }break;
case SOUTH: { case SOUTH: {
if (SigRenderer.vCamera.z<b.pos.z+0.5f&&checkCollision(SigRenderer.vCamera.x+x, b.pos.y+diffZ+0.3f, SigRenderer.vCamera.z+z,this)) { if (SigRenderer.vCamera.z<b.pos.z+0.5f) {
valid=true; valid=true;
} }
}break; }break;
case WEST: { case WEST: {
if (SigRenderer.vCamera.x>b.pos.x+0.5f&&checkCollision(SigRenderer.vCamera.x+x, b.pos.y+(1-diffX)+0.3f, SigRenderer.vCamera.z+z,this)) { if (SigRenderer.vCamera.x>b.pos.x+0.5f) {
valid=true; valid=true;
} }
}break; }break;
} }
} if (valid) {
if (valid) { SigRenderer.currentStaircase=this;
SigRenderer.currentStaircase=this; return true;
return true; }
} }
} }
return false; return false;
} }
} }
Loading…
Cancel
Save