gpt4 book ai didi

java - 如何让相机跟随2个物体?

转载 作者:行者123 更新时间:2023-12-01 13:47:53 26 4
gpt4 key购买 nike

我正在制作我的第一个 3D 游戏,它是一种街头霸王/铁拳游戏。我已经了解了某些相机模式的工作原理。 chatcam 和 camnode。我不知道如何让摄像机同时跟随两个玩家。我希望摄像机在玩家彼此靠近时放大,在玩家不靠近时缩小。

如果您能提供想法或可能的解决方案,我将不胜感激。

谢谢

最佳答案

如果您知道两条信息,即您希望相机在哪里以及您希望它看起来在哪里,您可以通过手动设置相机位置来相当轻松地实现此目的

相机应该看向哪里

这是最容易确定的,相机应该看着两个物体的中间位置。一旦您知道您可以使用设置相机正在寻找的位置

Vector3f boxsCentre=box1.getWorldTranslation().add(box2.getWorldTranslation()).mult(0.5f);
cam.lookAt(boxsCentre, Vector3f.UNIT_Y);

相机应该在哪里

相机应该放在哪里比较棘手。您知道它应该位于从两个对象的中心沿垂直于这两个对象之间的线的方向延伸的线上的某个位置。值得庆幸的是,叉积给了我们这个。我们希望相机始终与物体处于同一水平面上,因此通过垂直向上的 vector 与分离 vector 相交,我们得到了垂直线

Vector3f seperationVector=box2.getWorldTranslation().subtract(box1.getWorldTranslation());
Vector3f perpendicularFromTheAction= seperationVector.cross(Vector3f.UNIT_Y);

perpendicularFromTheAction.normalizeLocal();

所以,这给了我们这条线,但是我们应该把相机放在这条线上的哪里。我刚刚尝试了一下,发现对象之间距离的两倍看起来很漂亮,所以

float distance=2*seperationVector.length();
Vector3f newCameraLocation=boxsCentre.add(perpendicularFromTheAction.mult(distance));

然后您可以设置相机位置

cam.setLocation(newCameraLocation);

把它们放在一起

我将此代码与两个循环移动的盒子一起使用来演示这一点,正如您所看到的,您得到了您想要的效果

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.*;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;

public class FightTest extends SimpleApplication {

Geometry box1;
Geometry box2;
public static void main(String[] args) {
FightTest app = new FightTest();
app.start();
}

@Override
public void simpleInitApp() {

//put in some reference boxes
for(int i=-20;i<=20;i+=20){
for(int j=-20;j<=20;j+=20){
if (j!=0||i!=0){
Geometry referenceBox = createBox(ColorRGBA.Red);
referenceBox.setLocalTranslation(i, 0, j);
rootNode.attachChild(referenceBox);
}

}
}

//put in our two players
box1 = createBox(ColorRGBA.Blue);
box1.setLocalTranslation(5, 0, 0);

box2 = createBox(ColorRGBA.Green);

rootNode.attachChild(box1);
rootNode.attachChild(box2);
}

private Geometry createBox(ColorRGBA color){

Box b = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry box = new Geometry("Box", b);

Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", color);
box.setMaterial(mat);

return box;
}

@Override
public void simpleUpdate(float tpf) {
adjustCam();
movePlayers(tpf);
}

@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}

private void adjustCam(){
//we want our camera to look at the centre of the boxes
Vector3f boxsCentre=box1.getWorldTranslation().add(box2.getWorldTranslation()).mult(0.5f);

cam.lookAt(boxsCentre, Vector3f.UNIT_Y);

//we also want our camera to move closer/further away as the boxes seperate.
//and move around so its always parallel to the action

//parallel to the action means on the line given by the cross product of the
//box seperation and the upwards vector

Vector3f seperationVector=box2.getWorldTranslation().subtract(box1.getWorldTranslation());
Vector3f perpendicularFromTheAction= seperationVector.cross(Vector3f.UNIT_Y);

perpendicularFromTheAction.normalizeLocal();

//we could (and you should) get complicated on exactly how far the camera should
//move backwards, but I'm just going to make the camera twice as far away as the
//objects are seperated
float distance=2*seperationVector.length();

Vector3f newCameraLocation=boxsCentre.add(perpendicularFromTheAction.mult(distance));

cam.setLocation(newCameraLocation);
}

float timeAccumulator=0;
private void movePlayers(float tpf){
//basic movement, just for demo
timeAccumulator+=tpf;

if (timeAccumulator<2){
box1.move(new Vector3f(5f*tpf,0,0));
box2.move(new Vector3f(0,0,5f*tpf));
}else if (timeAccumulator<4){
box1.move(new Vector3f(-5f*tpf,0,0));
box2.move(new Vector3f(0,0,-5f*tpf));
}else{
timeAccumulator=0;
}

}
}

关于java - 如何让相机跟随2个物体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20213512/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com