我有一个难以解释的问题,所以我会尽力做到......
我有以下代码创建两个新块:
Block block2 = new Block(new Location(30,30,30),false, 20, blockTexture2, blockTexture2, blockTexture2, blockTexture2, blockTexture2, blockTexture2, false);
hittableCubes.add(block2);
block2.draw();
Block block = new Block(new Location(100,100,100),false, 20, blockTexture2, blockTexture2, blockTexture2, blockTexture2, blockTexture2, blockTexture2, false);
hittableCubes.add(block);
block.draw();
然后我有以下代码来检测碰撞:
public static boolean hitBlock(){
int count = 0;
for(Block block: hittableCubes){
System.out.println(block.getLocation().getX() + " " + block.getLocation().getY() + " " + block.getLocation().getZ());
if(-block.getLocation().getX()+block.getSize() >= camera.getX() && -block.getLocation().getX()-block.getSize() <= camera.getX()
&& -block.getLocation().getY()+block.getSize() >= camera.getY() && -block.getLocation().getY()-block.getSize() <= camera.getY()
&& -block.getLocation().getZ()+block.getSize() >= camera.getZ() && -block.getLocation().getZ()-block.getSize() <= camera.getZ()
){
count++;
}
}
if(count != 0){
return true;
}else{
return false;
}
}
碰撞检测适用于&#34;块&#34;但不是&#34;块2&#34;。 block2的位置实际上是130,130,130而不是100,100,100。当我在20,20,20创建另一个块时,新块的位置将是150,150,150,而block2将是130,130,130。知道为什么会这样吗?
这是我的Block类:
package threeD;
public class Block {
Texture textureFront;
Texture textureBehind;
Texture textureLeft;
Texture textureRight;
Texture textureBottom;
Texture textureTop;
float cubeSize;
Location location;
boolean debuggingMode = false;
CameraMovement camera;
/*
*
* XY controls the spot the cube is located (XY,XY)
*
* Z controls the size of the cube (Z,Z)
*
*/
public Block(Location location, boolean hitable, float cubeSize,Texture textureFront, Texture textureBehind, Texture textureLeft, Texture textureRight, Texture textureBottom, Texture textureTop, boolean debuggingMode){
this.textureFront = textureFront;
this.textureBehind = textureBehind;
this.textureLeft = textureLeft;
this.textureRight = textureRight;
this.textureBottom = textureBottom;
this.textureTop = textureTop;
this.location = location;
this.cubeSize = cubeSize;
}
public void setSize(float size){
cubeSize = size;
}
public Location getLocation(){
return location;
}
public float getSize(){
return cubeSize;
}
public void setFrontTexture(Texture texture){
textureFront = texture;
}
public void setBehindTexture(Texture texture){
textureBehind = texture;
}
public void setLeftTexture(Texture texture){
textureLeft = texture;
}
public void setRightTexture(Texture texture){
textureRight = texture;
}
public void setBottomTexture(Texture texture){
textureBottom = texture;
}
public void setTopTexture(Texture texture){
textureTop = texture;
}
public void debuggingMode(boolean debug){
debuggingMode = debug;
}
public void draw(){
glTranslatef(location.getX(), location.getY(), location.getZ());
//draws the block with a lot of code(not relevant to my question)
}
}
这是我的位置类:
package threeD;
public class Location {
float x;
float y;
float z;
public Location(float f, float g, float h){
this.x = f;
this.y = g;
this.z = h;
}
public float getX(){
return x;
}
public float getZ(){
return z;
}
public float getY(){
return y;
}
public void setX(float x){
this.x = x;
}
public void setY(float y){
this.y = y;
}
public void setZ(float z){
this.z = z;
}
public void setLocation(float x, float y, float z){
this.x = x;
this.y = y;
this.z = z;
}
public void translate(float x, float y, float z){
this.x += x;
this.y += y;
this.z += z;
}
}
然而,碰撞检测是正确的。它在100,100,100的位置,但块不是。