2014-11-26 2 views
0

자유롭게 떨어지는 (중력 아래에서) 총구를 사용하는 매우 간단한 3 인칭 카메라를 사용해 보았습니다. 그리고 render()에서 나는 구의 약간 위의 카메라 위치를 설정하고 구의 움직임 방향에 따라 카메라의 look-at를 설정하려고했습니다.Libgdx - Bullet Physics 3 인칭 카메라, 시간이 지남에 따라 작아 지거나 멀리 떨어져 나가는 카메라

처음에는 약간의 시간 동안 구가 렌더링되지만 나중에 점차 (약 5 초 후) 작아 지거나 카메라에서 멀리 떨어지거나 구의 크기가 관측되는 경우가 있습니다.

나는 다음과 같은 코드를 사용하고 있습니다 :

package a.b.c; 

import com.badlogic.gdx.ApplicationListener; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.graphics.Color; 
import com.badlogic.gdx.graphics.GL20; 
import com.badlogic.gdx.graphics.PerspectiveCamera; 
import com.badlogic.gdx.graphics.VertexAttributes.Usage; 
import com.badlogic.gdx.graphics.g3d.Environment; 
import com.badlogic.gdx.graphics.g3d.Material; 
import com.badlogic.gdx.graphics.g3d.Model; 
import com.badlogic.gdx.graphics.g3d.ModelBatch; 
import com.badlogic.gdx.graphics.g3d.ModelInstance; 
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute; 
import com.badlogic.gdx.graphics.g3d.attributes.FloatAttribute; 
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight; 
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder; 
import com.badlogic.gdx.math.MathUtils; 
import com.badlogic.gdx.math.Vector3; 
import com.badlogic.gdx.physics.bullet.collision.btBoxShape; 
import com.badlogic.gdx.physics.bullet.collision.btBroadphaseInterface; 
import com.badlogic.gdx.physics.bullet.collision.btCollisionConfiguration; 
import com.badlogic.gdx.physics.bullet.collision.btCollisionDispatcher; 
import com.badlogic.gdx.physics.bullet.collision.btCollisionShape; 
import com.badlogic.gdx.physics.bullet.collision.btDbvtBroadphase; 
import com.badlogic.gdx.physics.bullet.collision.btDefaultCollisionConfiguration; 
import com.badlogic.gdx.physics.bullet.collision.btSphereShape; 
import com.badlogic.gdx.physics.bullet.dynamics.btConstraintSolver; 
import com.badlogic.gdx.physics.bullet.dynamics.btDiscreteDynamicsWorld; 
import com.badlogic.gdx.physics.bullet.dynamics.btDynamicsWorld; 
import com.badlogic.gdx.physics.bullet.dynamics.btRigidBody; 
import com.badlogic.gdx.physics.bullet.dynamics.btRigidBody.btRigidBodyConstructionInfo; 
import com.badlogic.gdx.physics.bullet.dynamics.btSequentialImpulseConstraintSolver; 
import com.badlogic.gdx.physics.bullet.linearmath.btDefaultMotionState; 
import com.badlogic.gdx.tests.bullet.BaseBulletTest; 
import com.badlogic.gdx.tests.bullet.BulletTest; 
    import com.badlogic.gdx.utils.Array; 


public class BasicBulletTest1 implements ApplicationListener { 
    ModelBatch modelBatch; 
    Environment lights; 
    btRigidBody sphereBody ; 
    ModelBuilder modelBuilder = new ModelBuilder(); 
    ModelInstance sphere; 
    btCollisionConfiguration collisionConfiguration; 
    btCollisionDispatcher dispatcher; 
    btBroadphaseInterface broadphase; 
    btConstraintSolver solver; 
    btDynamicsWorld collisionWorld; 
    Vector3 gravity = new Vector3(0, -9.81f, 0); 
    Vector3 tempVector = new Vector3(); 

    Array<Model> models = new Array<Model>(); 
    Array<ModelInstance> instances = new Array<ModelInstance>(); 
    Array<btDefaultMotionState> motionStates = new Array<btDefaultMotionState>(); 
    Array<btRigidBodyConstructionInfo> bodyInfos = new Array<btRigidBodyConstructionInfo>(); 
    Array<btCollisionShape> shapes = new Array<btCollisionShape>(); 
    Array<btRigidBody> bodies = new Array<btRigidBody>(); 
    public PerspectiveCamera camera; 
    @Override 
    public void create() { 



     lights = new Environment(); 
     lights.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.2f, 0.2f, 0.2f, 1.f)); 
     lights.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -0.5f, -1f, -0.7f)); 

     // Set up the camera 
final float width = Gdx.graphics.getWidth(); 
final float height = Gdx.graphics.getHeight(); 
if (width > height) 
    camera = new PerspectiveCamera(67f, 3f * width/height, 3f); 
else 
    camera = new PerspectiveCamera(67f, 3f, 3f * height/width); 
camera.position.set(0f, 35f, 0f); 
camera.lookAt(0, 0, 0); 
camera.update(); 
// Create the model batch 
modelBatch = new ModelBatch(); 
// Create some basic models 
final Model groundModel = modelBuilder.createRect(
    20f, 
    0f, 
    -20f, 
    -20f, 
    0f, 
    -20f, 
    -20f, 
    0f, 
    20f, 
    20f, 
    0f, 
    20f, 
    0, 
    1, 
    0, 
    new Material(ColorAttribute.createDiffuse(Color.BLUE), ColorAttribute.createSpecular(Color.WHITE), FloatAttribute 
     .createShininess(16f)), Usage.Position | Usage.Normal); 
models.add(groundModel); 
final Model sphereModel = modelBuilder.createSphere(
    1f, 
    1f, 
    1f, 
    10, 
    10, 
    new Material(ColorAttribute.createDiffuse(Color.RED), ColorAttribute.createSpecular(Color.WHITE), FloatAttribute 
     .createShininess(64f)), Usage.Position | Usage.Normal); 
models.add(sphereModel); 
// Load the bullet library 
BaseBulletTest.init(); // Normally use: Bullet.init(); 
// Create the bullet world 
collisionConfiguration = new btDefaultCollisionConfiguration(); 
dispatcher = new btCollisionDispatcher(collisionConfiguration); 
broadphase = new btDbvtBroadphase(); 
solver = new btSequentialImpulseConstraintSolver(); 
collisionWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration); 
collisionWorld.setGravity(gravity); 
// Create the shapes and body construction infos 
btCollisionShape groundShape = new btBoxShape(tempVector.set(20, 0, 20)); 
shapes.add(groundShape); 
btRigidBodyConstructionInfo groundInfo = new btRigidBodyConstructionInfo(0f, null, groundShape, Vector3.Zero); 
bodyInfos.add(groundInfo); 
btCollisionShape sphereShape = new btSphereShape(0.5f); 
shapes.add(sphereShape); 
sphereShape.calculateLocalInertia(1f, tempVector); 
btRigidBodyConstructionInfo sphereInfo = new btRigidBodyConstructionInfo(1f, null, sphereShape, tempVector); 
bodyInfos.add(sphereInfo); 
// Create the ground 
ModelInstance ground = new ModelInstance(groundModel); 
instances.add(ground); 
btDefaultMotionState groundMotionState = new btDefaultMotionState(); 
groundMotionState.setWorldTransform(ground.transform); 
motionStates.add(groundMotionState); 
btRigidBody groundBody = new btRigidBody(groundInfo); 
groundBody.setMotionState(groundMotionState); 
bodies.add(groundBody); 
collisionWorld.addRigidBody(groundBody); 
// Create the spheres 
//for (float x = -10f; x <= 10f; x += 2f) { 
//for (float y = 5f; y <= 15f; y += 2f) { 
//for (float z = 0f; z <= 0f; z += 2f) { 
float x=30,y=10,z=-10; 
      sphere = new ModelInstance(sphereModel); 
      instances.add(sphere); 
      sphere.transform.trn(x + 0.1f * MathUtils.random(), y + 0.1f * MathUtils.random(), z + 0.1f * MathUtils.random()); 
      btDefaultMotionState sphereMotionState = new btDefaultMotionState(); 
      sphereMotionState.setWorldTransform(sphere.transform); 
      motionStates.add(sphereMotionState); 
      sphereBody = new btRigidBody(sphereInfo); 
      sphereBody.setMotionState(sphereMotionState); 
      bodies.add(sphereBody); 
      collisionWorld.addRigidBody(sphereBody); 
     //} 
//} 
//} 
} 
private Vector3 positionOld=new Vector3(); 
private Vector3 positionNew=new Vector3(); 

@Override 
public void render() { 
    sphereBody.getWorldTransform().getTranslation(positionOld);  


    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT); 


    ((btDynamicsWorld)collisionWorld).stepSimulation(Gdx.graphics.getDeltaTime(), 5); 


    int c = motionStates.size; 
    for (int i = 0; i < c; i++) { 
     motionStates.get(i).getWorldTransform(instances.get(i).transform); 
    } 

    modelBatch.begin(camera); 
    modelBatch.render(instances, lights); 
    modelBatch.end(); 



    //super.render(); 
sphereBody.getWorldTransform().getTranslation(positionNew); 

Vector3 subV=positionNew.cpy(); 
Vector3 temp=positionNew.cpy(); 
subV.sub(positionOld); 
//System.out.println("OLD: "+positionOld+" NEW: "+positionNew+" Temp"+temp); 
//positionNew.z=positionNew.z+3; 
temp.y=temp.y+3; 
camera.position.set(temp); 
System.out.println(subV.y+"                    "+Gdx.graphics.getDeltaTime()+"  "+positionOld); 
    camera.lookAt(positionNew.add(subV)); 
    camera.update(); 
} 

@Override 
public void dispose() { 
    collisionWorld.dispose(); 
    solver.dispose(); 
    broadphase.dispose(); 
    dispatcher.dispose(); 
    collisionConfiguration.dispose(); 

    for (btRigidBody body : bodies) 
     body.dispose(); 
    bodies.clear(); 
    for (btDefaultMotionState motionState : motionStates) 
     motionState.dispose(); 
    motionStates.clear(); 
    for (btCollisionShape shape : shapes) 
     shape.dispose(); 
    shapes.clear(); 
    for (btRigidBodyConstructionInfo info : bodyInfos) 
     info.dispose(); 
    bodyInfos.clear(); 

    modelBatch.dispose(); 
    instances.clear(); 
    for (Model model : models) 
     model.dispose(); 
    models.clear(); 
} 

@Override 
public void resize (int width, int height) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void pause() { 
    // TODO Auto-generated method stub 

} 

@Override 
public void resume() { 
    // TODO Auto-generated method stub 

    } 
} 

이 스타터 클래스입니다

import a.b.c.BasicBulletTest1; 

import com.badlogic.gdx.backends.lwjgl.LwjglApplication; 
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration; 

public class Main { 
    public static void main(String[] args) { 
     LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration(); 
     cfg.title = "hello-world"; 

     cfg.width = 640; 
     cfg.height = 480; 

     new LwjglApplication(new BasicBulletTest1(), cfg); 

    } 
} 

모든 일이 문제 좀 도와주십시오 또한 간단한 3 번째 사람을 만드는 좀 도와주세요 카메라.

는 하나 .. 내가 당신을 감사

libgdx 3D

위한 제 3의 사람이 카메라를 필요로 해주십시오에서 나를 도울 수 있습니다.

+1

아마도 도움이 될 것입니다. https://github.com/xoppa/world/blob/master/src/com/xoppa/android/world/ChaseCamera.java – noone

+0

감사합니다. 하지만 위의 코드에서 무엇이 잘못되었는지 알려주시겠습니까? 문제는 libgdx로 생각하거나 잘못된 방법으로 사용하고있는 것일 수 있습니다. – user1573344

답변

0
Vector3 subV=positionNew.cpy(); 
subV.sub(positionOld); 
camera.lookAt(positionNew.add(subV)); 

이 잠시 후

camera.lookAt(positionNew + positionNew - positionOld); // Hypothetical non-java code 

동등하다 positionNewpositionOld과의 차이는 상당해질 것이다.

안정성을 위해 subV을 고정 된 크기로 정규화하는 것이 좋습니다.

희망이 도움이됩니다.

+0

귀하의 의견을 보내 주셔서 감사합니다. 그러나 오브젝트가 9.8의 중력으로 자유롭게 떨어지는 동안 positionOld와 positionNew 사이의 간격도 점차적으로 커집니다. Hense는 positionNew와 거의 같지 않을 수 있습니다. 내가 worng 경우 수정하십시오. – user1573344