3d游戏(所有浮点数) 玩家移动:
public void update(float dt){ //in MainClass dt = Math.min(Gdx.graphics.getDeltaTime(), 1/20f);
this.dt = dt;
time += 60*dt;
collision = new NigelCollision(getPos(), dir, kick);
controller.update(dt);
detector.detectCollisions(4, 0, 1, getPos(), dir, speed *dt, vspeed, dt);
if (detector.isDetect()) {
if (kick){
sword2.play();
bSword2.opacity = 0;
bSword1.opacity = 1;
bSwing.opacity = 0;
if (detector.isBottomDetect())
controller.setAnimation("stay");
else controller.setAnimation("jump-D");
kick = false;
}
if (!gShifted) {
speed = detector.getGshift();
gShifted = true;
}
else speed = 0;
}
else gShifted = false;
if (detector.isBottomDetect() || onPlatform) {
if(fall == true){
if (speed > 0)
controller.setAnimation("move", -1);
else controller.setAnimation("stay");
jump2.play();
fall = false;
}
if (!vShifted) {
vspeed = -detector.getVshift();
vShifted = true;
}
else vspeed = 0;
}
else {
if(fall == false && vspeed < 0){
controller.setAnimation("jump-D");
fall = true;
}
if (!climb) {
vShifted = false;
gravity = 1.2f * dt;
}
if(vspeed > -36 *dt)
vspeed-= gravity;
}
if (climb && !detector.isRopeDetect()) {
if(!vShifted) {
vspeed = detector.getVshift();
vShifted = true;
}
else {
move(dir);
vspeed = 0;
climbing.stop();
climb = false;
vShifted = false;
}
}
if (detector.isRopeDetect()) {
if (!climb) {
climb();
gravity = 0;
climb = true;
}
}
//else climb = false;
if (detector.isPlatformDetect()) onPlatform = true;
if (detector.isBottomDetect()) onPlatform = false;
if (onPlatform) {
if (dir == 0)
nigel.transform.translate(detector.getpSpeed().x, detector.getpSpeed().y,
detector.isDetect() || onPos ? speed + detector.getpSpeed().z: speed *dt + detector.getpSpeed().z);
if (dir == 90)
nigel.transform.translate(-detector.getpSpeed().z, vspeed + detector.getpSpeed().y,
detector.isDetect() || onPos ? speed + detector.getpSpeed().x: speed *dt + detector.getpSpeed().x);
if (dir == 180)
nigel.transform.translate(-detector.getpSpeed().x, vspeed + detector.getpSpeed().y,
detector.isDetect() || onPos ? speed - detector.getpSpeed().z: speed *dt - detector.getpSpeed().z);
if (dir == 270)
nigel.transform.translate(detector.getpSpeed().z, vspeed + detector.getpSpeed().y,
detector.isDetect() || onPos ? speed - detector.getpSpeed().x: speed *dt - detector.getpSpeed().x);
}
else nigel.transform.translate(0, detector.isRopeDetect()? vspeed *dt: vspeed,
detector.isDetect() || onPos? speed: speed *dt);
其他播放器方法:
public void kick(int rot){
//k = 0;
controller.setAnimation("kick", 1, 0.7f, new AnimationController.AnimationListener() {
@Override
public void onEnd(AnimationController.AnimationDesc animation) {
controller.setAnimation("stay", -1);
bSword2.opacity = 0;
bSword1.opacity = 1;
bSwing.opacity = 0;
kick = false;
}
@Override
public void onLoop(AnimationController.AnimationDesc animation) {
}
});
bSword2.opacity = 1;
bSword1.opacity = 0;
sword1.play();
bSwing.opacity = 0.5f;
kick = true;
}
public void movedKick(int rot){
//k = 0;
controller.setAnimation("kick", 1, 0.7f, new AnimationController.AnimationListener() {
@Override
public void onEnd(AnimationController.AnimationDesc animation) {
controller.setAnimation("move", -1);
bSword2.opacity = 0;
bSword1.opacity = 1;
bSwing.opacity = 0;
kick = false;
}
@Override
public void onLoop(AnimationController.AnimationDesc animation) {
}
});
speed = 12;
bSword2.opacity = 1;
bSword1.opacity = 0;
sword1.play();
bSwing.opacity = 0.5f;
kick = true;
}
public void move(int rot){
speed = 12;
nigel.transform.rotate(Vector3.Y, rot - dir);
if (detector.isBottomDetect() || onPlatform)
controller.setAnimation("move", -1);
dir = rot;
}
public void stay(int rot){
speed = 0;
nigel.transform.rotate(Vector3.Y, rot - dir);
if (detector.isBottomDetect() || onPlatform)
controller.setAnimation("stay");
dir = rot;
}
public void jump(int rot){
if (detector.isBottomDetect() || onPlatform) {
onPlatform = false;
vspeed = 0.4f;
if(!kick)
controller.setAnimation("jump-U");
jump1.play();
bSword2.opacity = 0;
bSword1.opacity = 1;
bSwing.opacity = 0;
}
}
public void take(){
}
public void climb(){
vspeed = 5;
controller.setAnimation("move-U", -1);
climbing.play();
climbing.loop();
}
public void damage(){
}
public void die(){
}
public void setPos(){
nigel.transform.setTranslation(newPos);
}
public Vector3 getPos() {
return nigel.transform.getTranslation(pos);
}
碰撞检测器:
public void detectCollisions(int height, int centerY, int radius, Vector3 position, int dir, float speed, float vspeed, float dt){
frontDetectX = frontDetectZ = backDetectX = backDetectZ = bottomDetect = topDetect = ropeDetect = platformDetect = kickDetect = false;
nigelCollision = nigel.collision;
if(nigelCollision.kick && position != nigelCollision.pos)
if(position.y - nigelCollision.pos.y < 3 && nigelCollision.pos.y - position.y < 3) {
if (nigelCollision.rot == 0 && position.z - nigelCollision.pos.z < 4 && position.z - nigelCollision.pos.z >= 1
&& position.x - nigelCollision.pos.x <= 2 && position.x - nigelCollision.pos.x >= -2)
kickDetect = true;
if (nigelCollision.rot == 270 && position.x - nigelCollision.pos.x > -4 && position.x - nigelCollision.pos.x <= -1
&& position.z - nigelCollision.pos.z <= 2 && position.z - nigelCollision.pos.z >= -2)
kickDetect = true;
if (nigelCollision.rot == 180 && position.z - nigelCollision.pos.z > -4 && position.z - nigelCollision.pos.z <= -1
&& position.x - nigelCollision.pos.x <= 2 && position.x - nigelCollision.pos.x >= -2)
kickDetect = true;
if (nigelCollision.rot == 90 && position.x - nigelCollision.pos.x < 4 && position.x - nigelCollision.pos.x >= 1
&& position.z - nigelCollision.pos.z <= 2 && position.z - nigelCollision.pos.z >= -2)
kickDetect = true;
}
for (int p = 0; p < platforms.size; p++){
if(platforms.get(p).pos.y +1 + platforms.get(p).yspeed >= position.y - centerY + vspeed - 1.2f *dt
&& platforms.get(p).pos.y +1 + platforms.get(p).yspeed < position.y - centerY + height
&& platforms.get(p).pos.z > position.z -2 && platforms.get(p).pos.z < position.z +2
&& platforms.get(p).pos.x > position.x -2 && platforms.get(p).pos.x < position.x +2) {
platformDetect = true;
//bottomDetect = true;
vshift = position.y - centerY - (platforms.get(p).pos.y + 1 + platforms.get(p).yspeed);
pNum = p;
}
pSpeed = new Vector3(platforms.get(pNum).xspeed, platforms.get(pNum).yspeed, platforms.get(pNum).zspeed);
}
for (int i = 0; i < walls.size; i++){
if(walls.get(i).rot.x == 0)
if(walls.get(i).endPos.y > position.y - centerY -1 && walls.get(i).beginPos.y < position.y - centerY + height +1){
if(pSpeed.x < 0 && position.x - radius + pSpeed.x <= walls.get(i).endPos.x +1 && position.x - radius >= walls.get(i).endPos.x +1
&& position.z > walls.get(i).beginPos.z -1 - radius && position.z < walls.get(i).endPos.z +1 + radius) {
pSpeed.x = (position.x - radius) - (walls.get(i).endPos.x +1);
}
if(pSpeed.x > 0 && position.x + radius + pSpeed.x >= walls.get(i).beginPos.x -1 && position.x + radius <= walls.get(i).beginPos.x -1
&& position.z > walls.get(i).beginPos.z -1 - radius && position.z < walls.get(i).endPos.z +1 + radius) {
pSpeed.x = (walls.get(i).endPos.x -1) - (position.x + radius);
}
if(pSpeed.z < 0 && position.z - radius + pSpeed.z <= walls.get(i).endPos.z +1 && position.z - radius >= walls.get(i).endPos.z +1
&& position.x > walls.get(i).beginPos.x -1 - radius && position.x < walls.get(i).endPos.x +1 + radius) {
pSpeed.z = (position.z - radius) - (walls.get(i).endPos.z +1);
}
if(pSpeed.z > 0 && position.z + radius + pSpeed.z >= walls.get(i).beginPos.z -1 && position.z + radius <= walls.get(i).beginPos.z -1
&& position.x > walls.get(i).beginPos.x -1 - radius && position.x < walls.get(i).endPos.x +1 + radius) {
pSpeed.z = (walls.get(i).endPos.z -1) - (position.z + radius);
}
}
if(walls.get(i).rot.x == -90)
if(walls.get(i).endPos.y +1 >= position.y - centerY + vspeed - 1.2f *dt && walls.get(i).endPos.y +1 < position.y - centerY + height //?
&& walls.get(i).endPos.z > position.z -2 && walls.get(i).beginPos.z < position.z +2
&& walls.get(i).endPos.x > position.x -2 && walls.get(i).beginPos.x < position.x +2) {
bottomDetect = true;
vshift = position.y - centerY - (walls.get(i).endPos.y + 1);
}
if(speed > 0 && walls.get(i).rot.x == 0){
if(walls.get(i).endPos.y > position.y - centerY -1 && walls.get(i).beginPos.y < position.y - centerY + height +1){
if(dir == 90 && walls.get(i).beginPos.x <= position.x + speed +1 + radius && walls.get(i).beginPos.x >= position.x +1 + radius
&& walls.get(i).endPos.z > position.z -1 - radius && walls.get(i).beginPos.z < position.z +1 + radius) {
frontDetectX = true;
gshift = walls.get(i).beginPos.x - (position.x + 1 + radius);
}
if(dir == 0 && walls.get(i).beginPos.z <= position.z + speed +1 + radius && walls.get(i).beginPos.z >= position.z +1 + radius
&& walls.get(i).endPos.x > position.x -1 - radius && walls.get(i).beginPos.x < position.x +1 + radius) {
frontDetectZ = true;
gshift = walls.get(i).beginPos.z - (position.x + 1 + radius);
}
if(dir == 270 && position.x <= walls.get(i).endPos.x + speed +1 + radius && position.x -1 - radius >= walls.get(i).endPos.x
&& position.z > walls.get(i).beginPos.z -1 - radius && position.z < walls.get(i).endPos.z +1 + radius) {
backDetectX = true;
gshift = position.x - (walls.get(i).endPos.x + 1 + radius);
}
if(dir == 180 && position.z <= walls.get(i).endPos.z + speed +1 + radius && position.z -1 - radius >= walls.get(i).endPos.z
&& position.x > walls.get(i).beginPos.x -1 - radius && position.x < walls.get(i).endPos.x +1 + radius) {
backDetectZ = true;
gshift = position.z - (walls.get(i).beginPos.z + 1 + radius);
}
}
}
}
for (int o = 0; o < objects.size; o++){
if (objects.get(o).object.equals("rope") || objects.get(o).object.equals("ropeEnd")){
if ((dir == 270 && objects.get(o).rot == 90) || (dir == 180 && objects.get(o).rot == 0))
if(objects.get(o).endPos.y > position.y -1 && objects.get(o).beginPos.y < position.y + height +1) {
if (dir == 270 && position.x == objects.get(o).beginPos.x + 2 && position.z < objects.get(o).beginPos.z +0.5f
&& position.z > objects.get(o).beginPos.z -0.5f) {
ropeDetect = true;
vshift = objects.get(o).endPos.y - (position.y -1);
}
if (dir == 180 && position.z == objects.get(o).beginPos.z + 2 && position.x < objects.get(o).beginPos.x +0.5f
&& position.x > objects.get(o).beginPos.x -0.5f) {
ropeDetect = true;
vshift = objects.get(o).endPos.y - (position.y -1);
}
}
if(ropeDetect && objects.get(o).object.equals("ropeEnd")){
if(objects.get(o).endPos.y +1 < position.y) {
vshift = position.y - objects.get(o).endPos.y +1;
//objects.get(o).endPos.y - (position.y - 1);
ropeDetect = false;
}
}
}
}
}
public boolean isDetect(){
if(frontDetectX || frontDetectZ || backDetectX || backDetectZ)
detect = true;
else detect = false;
return detect;
}
public boolean isBottomDetect(){
return bottomDetect;
}
public boolean isRopeDetect(){
return ropeDetect;
}
public boolean isPlatformDetect(){
return platformDetect;
}
public boolean isKickDetect(){
return kickDetect;
}
public float getVshift(){
return vshift;
}
public float getGshift(){
return gshift;
}
public Vector3 getpSpeed(){
return pSpeed;
}
玩家沿着坐标x以12的速度移动,z坐标为0 日志(速度/位置):
12.0/(14.0,1.0,0.0)
12.0/(13.807683,1.0,1.1462985E-8)
12.0/(13.468298,1.0,3.169194E-8)
12.0/(13.372773,1.0,3.738564E-8)
12.0/(13.210449,1.0,4.7060894E-8)
12.0/(13.011898,1.0,5.889545E-8)
12.0/(12.803396,1.0,7.132311E-8)
12.0/(12.609674,1.0,8.2869825E-8)
12.0/(12.406296,1.0,9.499213E-8)
12.0/(12.1833935,1.0,1.08278115E-7)
沿x的运动是正确的,z为0,但是如果玩家喝醉了,则z坐标会改变! 问题出现在零坐标处,如果沿着z坐标走,则1正确:
12.0/(14.0,1.0,1.0)
12.0/(13.859465,1.0,1.0)
12.0/(13.647637,1.0,1.0)
12.0/(13.364415,1.0,1.0)
12.0/(13.256388,1.0,1.0)
12.0/(13.026965,1.0,1.0)
12.0/(12.545555,1.0,1.0)
到达零位时会发生相同的问题 移至所需位置的代码(目标位置取自.json文件):
public void programmHandling(int id, int pTime, int pos, String method, int value) {
if(pTime < 0) {
if(dir == 0 && getPos().z + speed *dt > pos) {
speed = pos - getPos().z;
onPos = true;
}
if(dir == 90 && getPos().x + speed *dt > pos) {
speed = pos - getPos().x;
onPos = true;
}
if(dir == 180 && getPos().z - speed *dt < pos){
speed = getPos().z - pos;
onPos = true;
}
if(dir == 270 && getPos().x - speed *dt < pos){
speed = getPos().x - pos;
onPos = true;
}
if ((dir == 0 && getPos().z >= pos) || (dir == 180 && getPos().z <= pos) ||
(dir == 90 && getPos().x >= pos) || (dir == 270 && getPos().x <= pos)) {
onPos = false;
try {
Method m = this.getClass().getMethod(method, int.class);
m.invoke(this, value);
if (!method.equals("stay"))
speed = 12;
handler.i++;
} catch (Exception e) {}
}
}
else {
if (time >= pTime) {
try {
Method m = this.getClass().getMethod(method, int.class);
m.invoke(this, value);
handler.i++;
} catch (Exception e) {}
}
}
}
位置6,迎面而来的正常工作
log(速度/位置,目标位置):
12.0/(6.5635056,1.0,1.0),6
12.0/(6.3890247,1.0,1.0),6
0.18205595/(6.182056,1.0,1.0),6
0.0/(6.0,1.0,1.0),6
12.0/(5.6435366,1.0,1.0),0
但即将到达位置0:
12.0/(0.5863363,1.0,1.0),0
12.0/(0.39619145,1.0,1.0),0
0.1771532/(0.1771532,1.0,1.0),0
1.4901161E-8/(1.4901161E-8,1.0,1.0),0
8.881784E-16/(8.881784E-16,1.0,1.0),0
5.293956E-23/(5.293956E-23,1.0,1.0),0
3.1554436E-30/(3.1554436E-30,1.0,1.0),0
1.880791E-37/(1.880791E-37,1.0,1.0),0
1.1E-44/(1.1E-44,1.0,1.0),0
1.1E-44/(0.0,1.0,1.0),0
12.0/(-0.27459404,1.3725406,1.0),-6
我不明白搜索互联网没有给出什么问题