I have a gun attached to my player that follows him arround. It seams there is also a gun at 0,0. I do not have it duplicate the gun or anything. I use spatial.move to attach it to my player. Second question, the gun follows my player just fine but it seams to cull out the parts of the gun close to the player, is there a fix?
Question 1) Post some code, please
Question 2) Try rendering the gun in a different viewport, thats how most fps games work. But make sure you’re not rendering the scene and gun in each camera.
Yeah, we’ll need to see some code. If the gun is in your scene twice then you put it there twice. It’s just a matter of figuring out where.
The clipping issue is probably the near plane. You can adjust it closer but that may cause other issues that are hard to determine given the limited information provided. A viewport can help but you will still potentially have near plane clipping problems.
[java]/*
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package net.gyroninja.LaserRaid;
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.input.FlyByCamera;
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.terrain.geomipmap.TerrainLodControl;
import org.lwjgl.input.Mouse;
/**
*
-
@author gyroninja
*/
public class LRGameState extends AbstractAppState implements ActionListener {
SimpleApplication app;
BulletAppState bulletAppState;
Node rootNode;
ViewPort viewPort;
Camera cam;
FlyByCamera flyCam;
InputManager inputManager;
AssetManager assetManager;
Node guiNode;
Spatial terrain;
Spatial gun;
Material gunMat;
Material laserMat;
BulletAppState bulletappstate;
RigidBodyControl landscape;
CharacterControl player;
Vector3f walkDirection = new Vector3f();
boolean left, right, up, down;
@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
this.app = (SimpleApplication) app;
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
this.rootNode = this.app.getRootNode();
this.viewPort = this.app.getViewPort();
this.cam = this.app.getCamera();
this.flyCam = this.app.getFlyByCamera();
this.inputManager = this.app.getInputManager();
this.assetManager = this.app.getAssetManager();
this.guiNode = this.app.getGuiNode();
terrain = assetManager.loadModel("Scenes/Terrain.j3o");
gun = assetManager.loadModel("Models/LaserGun/LaserGun.j3o");
gunMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
gunMat.setTexture("ColorMap", assetManager.loadTexture("Models/LaserGun/LaserGun.png"));
gun.setMaterial(gunMat);
viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));
flyCam.setMoveSpeed(50);
setUpKeys();
CollisionShape sceneShape = CollisionShapeFactory.createMeshShape((Node) terrain);
landscape = new RigidBodyControl(sceneShape, 0);
terrain.addControl(landscape);
CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);
player = new CharacterControl(capsuleShape, 0.05f);
player.setJumpSpeed(25);
player.setFallSpeed(35);
player.setGravity(30);
player.setPhysicsLocation(new Vector3f(0, 20, 0));
TerrainLodControl lodControl = ((Node)terrain).getControl(TerrainLodControl.class);
if (lodControl != null) {
lodControl.setCamera(app.getCamera());
}
rootNode.attachChild(terrain);
rootNode.attachChild(gun);
bulletAppState.getPhysicsSpace().add(landscape);
bulletAppState.getPhysicsSpace().add(player);
}
@Override
public void render(RenderManager rm) {
}
@Override
public void update(float tpf) {
Vector3f camDir = cam.getDirection().clone().multLocal(0.6f);
Vector3f camLeft = cam.getLeft().clone().multLocal(0.4f);
walkDirection.set(0, 0, 0);
if (left) {
walkDirection.addLocal(camLeft);
}
if (right) {
walkDirection.addLocal(camLeft.negate());
}
if (up) {
walkDirection.addLocal(camDir);
}
if (down) {
walkDirection.addLocal(camDir.negate());
}
player.setWalkDirection(walkDirection);
cam.setLocation(player.getPhysicsLocation());
Vector3f gunPos = player.getPhysicsLocation();
gunPos.setY(gunPos.getY() - 1);
moveSpatial(gun, gunPos);
if (Mouse.isButtonDown(0)) {
}
}
public void onAction(String binding, boolean value, float tpf) {
if (binding.equals("Left")) {
left = value;
}
if (binding.equals("Right")) {
right = value;
}
if (binding.equals("Up")) {
up = value;
}
if (binding.equals("Down")) {
down = value;
}
if (binding.equals("Jump")) {
player.jump();
}
}
public void setUpKeys() {
inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addListener(this, "Left");
inputManager.addListener(this, "Right");
inputManager.addListener(this, "Up");
inputManager.addListener(this, "Down");
inputManager.addListener(this, "Jump");
}
public void moveSpatial(Spatial spatial, Vector3f spot) {
Vector3f localLoc = new Vector3f();
spatial.worldToLocal(spot, localLoc);
spatial.move(localLoc);
}
}
[/java]
Sorry for posting my whole state
Try rewriting your method. You dont need a this stuff from.what I can tell. Here, try:
[java]
Node gunNode = new Node();
rootNode.attachChild(gunNode);
//Set up your gun
gun.setLocalTranslation(You know the right numbers);
gunNode.attachChild(gun);
@Override
public void update(float tpf) {
//Move gunNode (by extension, gun) to player location
gunNode.setLocalTranslation(player.getLocalTranslation());
//I cant remember the code for rotation off the top of my head
//But get cam rotation in a quarternion, and
gunNode.setLocalRotation(camRotation);
}
[/java]
Most likely cause of double-guns could be that maybe you call initialize() yourself when the state manager will already be calling it.
sorry but my CharacterControl dosen’t have a getLocalTranslation() method. How exactly do I put the gun in a new viewport or adjust the near plane field? Sorry about these questions I just started jme3.
Got the clipping thing fixed :D.
Sorry, make gunNode follow cam instead
[java]
public void update(float tpf) {
gunNode.setLocalTranslation(cam.getLocation());
gunNode.setLocalRotation(cam.getRotation());
}
[/java]
And for the other viewport, which is imo the most elegant solution
[java]
gunNode = new Node();
rootNode.attachChild(gunNode);
cam.setViewPort(0.0f, 1f, 0f, 1f);
// Setup second (gun) camera
gunCam = cam.clone();
gunCam.setViewPort(0f, 1f, 0f, 1f);
gunCam.setLocation(cam.getLocation());
gunCam.setRotation(cam.getRotation());
//Now for the second viewport
ViewPort gunView = renderManager.createMainView(“GunView”, gunCam);
gunView.setClearDepth(true);
gunView.attachScene(gunNode); ///Make sure you’re not rendering gunNode twice…
viewPort.detachScene(gunNode);
[/java]
@glcheetham said:
And for the other viewport, which is imo the most elegant solution
He has actually fixed the clipping thing, so no need for that man :p