I’m trying to move a block to another blocks position (bit by bit using Vector3f). My output just shows that the player is always at (0,0,0) When I’ve set him to be at (-20,1,10).
Main
[java]package mygame;
import com.jme3.app.SimpleApplication;
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.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import java.util.ArrayList;
/**
- test
-
@author normenhansen
/
public class Main extends SimpleApplication implements ActionListener {
ArrayList<Geometry> geos = new ArrayList();
public static void main(String[] args) {
Main app = new Main();
app.start();
}
Box[] players = new Box[1];
Person per = new Person();
Geometry geom_BB;
Geometry geom_Player;
@Override
public void simpleInitApp() {
Material mat;
initKeys();
players[0] = new Box(new Vector3f(-20,1,10), 1, 1, 1);
geom_Player = new Geometry("Player", players[0]);
Material mat_default = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat_default.setTexture("ColorMap",assetManager.loadTexture("41.jpg"));
mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setTexture("ColorMap", assetManager.loadTexture("Textures/4.jpg"));
geom_Player.setMaterial(mat);
rootNode.attachChild(geom_Player);
flyCam.setMoveSpeed(10f);
setUpWorld();
}
private void initKeys(){
inputManager.addMapping("Left",new KeyTrigger(KeyInput.KEY_J));
inputManager.addMapping("Right",new KeyTrigger(KeyInput.KEY_H));
inputManager.addMapping("Up",new KeyTrigger(KeyInput.KEY_U));
inputManager.addMapping("Down",new KeyTrigger(KeyInput.KEY_M));
inputManager.addListener(this, "Left");
inputManager.addListener(this, "Right");
inputManager.addListener(this, "Up");
inputManager.addListener(this, "Down");
}
public void onAction(String name, boolean keyPressed, float tpf) {
if (name.equals("Left") && !keyPressed) {
System.out.println("HIS: " + geom_Player.getLocalTransform());
//geom_Player.setLocalTranslation(( geom_Player.getLocalTranslation().x-10), geom_Player.getLocalTranslation().y , geom_Player.getLocalTranslation().z);
// simpleUpdate(1);
//rootNode.collideWith(ray, results);
}else if(name.equals("Right") && !keyPressed)
{
geom_BB.setLocalTranslation((geom_BB.getLocalTranslation().x-10),geom_BB.getLocalTranslation().y , geom_BB.getLocalTranslation().z);
simpleUpdate(1);
}else if (name.equals("Up") && !keyPressed)
{
geom_BB.setLocalTranslation((geom_BB.getLocalTranslation().x),geom_BB.getLocalTranslation().y+10 , geom_BB.getLocalTranslation().z);
simpleUpdate(1);
}else if (name.equals("Down") && !keyPressed)
{
geom_BB.setLocalTranslation((geom_BB.getLocalTranslation().x),geom_BB.getLocalTranslation().y-10 , geom_BB.getLocalTranslation().z);
simpleUpdate(1);
}
}
public void setUpWorld()
{
Material mat;
Geometry geom;
Box b;
for (int x = 0; x < 20; x++)
{
x++;
b = new Box(new Vector3f(x,1,1), 1, 1, 1);
geom = new Geometry("Wood-10", b);
mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setTexture("ColorMap", assetManager.loadTexture("Textures/4.jpg"));
geom.setMaterial(mat);
rootNode.attachChild(geom);
for (int z = 0;z<10;z++)
{
z++;
b = new Box(new Vector3f(x,1,z), 1, 1, 1);
geom = new Geometry("Wood-10", b);
mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setTexture("ColorMap", assetManager.loadTexture("Textures/4.jpg"));
geom.setMaterial(mat);
rootNode.attachChild(geom);
}
}
}
@Override
public void simpleUpdate(float tpf) {
Vector3f newPos = per.goTo( geom_Player.getLocalTranslation(), new Vector3f(30f,30f,30f));
rootNode.detachChild(geom_Player);
geom_Player.setLocalTranslation(newPos.x,newPos.y,newPos.z);
rootNode.attachChild(geom_Player);
}
@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
}
[/java]
[java]/
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package mygame;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
-
@author x00062057
*/
public class Person{
String F_Name,L_Name;
double str,intl, speed;
int amt_wood = 0;
Box b;
private Geometry g;
public Person(String FName,String LName,double _str,double _intl,double _speed,
Box _b)
{
F_Name = FName;
L_Name = LName;
str = _str;
intl = _intl;
speed = _speed;
b = _b;
}
public Person(Box _b,Geometry _g)
{
g = _g;
str = intl = speed = 0;
F_Name = L_Name = "Martin";
b = _b;
}
public Person()
{
}
public void setName(String FName, String LName)
{
F_Name = FName;
L_Name = LName;
}
public String getName()
{
return F_Name +" "+ L_Name;
}
public void setStr(int _Str)
{
str = _Str;
}
public void setintl(int _intel)
{
intl = _intel;
}
public void setSpeed(int _speed)
{
speed = _speed;
}
public void getWood()
{
}
public Vector3f goTo(Vector3f currentPosition,Vector3f target)
{
float C_x,C_y,C_z,T_x,T_y,T_z;
C_x = currentPosition.x;
C_y = currentPosition.y;
C_z = currentPosition.z;
T_x = target.x;
T_y = target.y;
T_z = target.z;
if (C_x > 0 && T_x > 0 && C_x > T_x)
{
C_x --;
}else if(C_x < 0 && T_x < 0 && C_x > T_x)
{
C_x++;
}
if (C_y < 0 && T_y < 0 && C_y < T_y)
{
C_y–;
}
if (C_y > 0 && T_y > 0 && C_y > T_y)
{
C_y ++;
}
if (C_z < 0 && T_z < 0 && C_z < T_z)
{
C_z ++;
}
if (C_z > 0 && T_z > 0 && C_z > T_z)
{
C_z --;
}
return new Vector3f (C_x,C_y,C_z);
}
}
[/java]
Other questions:
How do i detect when on block hit another.
Is there a good gun tutorial (First person) Tutorial around?