Vistas de página en total

viernes, 7 de agosto de 2009

Java 3D - moving camera



Pues en Arqui2 siempre dejan un proyecto de JAVA3D que por cierto no he terminado, xD siempre que he usado java3d ha sido un dolor de cabeza mover la camara aqui les va un ejemplo





Ejemplo Del Movimiento De La Camara API JAVA3D:
Movimiento Que la Camara sigue un Personaje





package muytux;

import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.awt.*;
import javax.media.j3d.*;
import javax.swing.JFrame;
import javax.vecmath.*;
public class TuxFrame extends JFrame{
public TuxFrame(){
setLayout(new BorderLayout());
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
setSize(400, 400);

Canvas3D canvas3D = new Canvas3D(config);
add("Center", canvas3D);
BranchGroup scene = new BranchGroup();
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);

simpleU.getViewingPlatform().setNominalViewingTransform();
scene.addChild(TuxEscenario(simpleU));
simpleU.addBranchGraph(scene);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setVisible(true);
}
/*El fondo*/
Shape3D createLand(){
LineArray landGeom = new LineArray(44, GeometryArray.COORDINATES
| GeometryArray.COLOR_3);
float l = -50.0f;
for(int c = 0 ; c < 44d ; c+=4d){
landGeom.setCoordinate( c+0, new Point3f( -50.0f, 0.0f, l ));
landGeom.setCoordinate( c+1, new Point3f( 50.0f, 0.0f, l ));
landGeom.setCoordinate( c+2, new Point3f( l , 0.0f, -50.0f ));
landGeom.setCoordinate( c+3, new Point3f( l , 0.0f, 50.0f ));
l += 10.0f;
}
Color3f c = new Color3f(0.1f, 0.8f, 0.1f);
for(int i = 0; i < 44; i++) landGeom.setColor( i, c);
return new Shape3D(landGeom);
}
/*El personaje*/
public TransformGroup TuxPersonaje(){
Transform3D tr=new Transform3D();
tr.rotY(Math.PI/4);
TransformGroup tuxtr=new TransformGroup(tr);
tuxtr.addChild(new ColorCube(.4));
return tuxtr;
}
/*El Escenario*/
public TransformGroup TuxEscenario(SimpleUniverse simpleU){
//CONTENEDOR
TransformGroup Tuxcontent = new TransformGroup();
TransformGroup Tuxobj = new TransformGroup();
Tuxcontent.addChild(Tuxobj);
Tuxcontent.addChild(this.createLand());

TransformGroup Tuxtrans=new TransformGroup();
Tuxtrans.addChild(TuxPersonaje());
Tuxcontent.addChild(Tuxtrans);

TuxBehaior TuxBe=new TuxBehaior(simpleU,Tuxtrans);
Tuxcontent.addChild(TuxBe);

//ILUMINACION
BoundingSphere bounds = new BoundingSphere();
DirectionalLight lightD = new DirectionalLight();
lightD.setDirection(new Vector3f(0.0f,-0.7f,-0.7f));
lightD.setInfluencingBounds(bounds);
Tuxcontent.addChild(lightD);
AmbientLight lightA = new AmbientLight();
lightA.setInfluencingBounds(bounds);
Tuxcontent.addChild(lightA);

//FONDO BLANCO
Background background = new Background();
background.setColor(1.0f, 1.0f, 1.0f);
background.setApplicationBounds(bounds);
Tuxcontent.addChild(background);

return Tuxcontent;

}
public static void main(String[] args) {
new TuxFrame();
}
}


Ahora un Behaior para Controla el movimiento






package muytux;

import com.sun.j3d.utils.universe.SimpleUniverse;
import java.awt.AWTEvent;
import java.awt.event.KeyEvent;
import java.util.Enumeration;
import javax.media.j3d.*;
import javax.vecmath.*;
public class TuxBehaior extends Behavior{
TransformGroup Camara;
TransformGroup Node;
private WakeupOnAWTEvent trigger;
Integer Zoom=3;
private void TuxPosicion(){
Node.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Transform3D tr1 = new Transform3D();
tr1.set(new Vector3d(0f, 0.6f, +Zoom));
Camara.setTransform(tr1);
tr1.set(new Vector3f(0f, 0.45f, 0f));
Node.setTransform(tr1);
}
TuxBehaior(SimpleUniverse su,TransformGroup Node){
this.Node=Node;
this.Camara=su.getViewingPlatform().getViewPlatformTransform();
trigger=new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED);
TuxPosicion();
setSchedulingBounds(new BoundingSphere( new Point3d( 0.0, 0.0, 0.0 ), 100.0 ));
}

public void initialize() {
this.wakeupOn(trigger);
}

public void processStimulus(Enumeration criteria) {
while (criteria.hasMoreElements()) {
WakeupCriterion wakeup = (WakeupCriterion) criteria.nextElement();
if (wakeup instanceof WakeupOnAWTEvent) {

AWTEvent[] arr = ((WakeupOnAWTEvent) (wakeup)).getAWTEvent();
KeyEvent ke = (KeyEvent) arr[0];
switch (ke.getKeyCode()) {
case KeyEvent.VK_UP:
TuxArriba();
break;
case KeyEvent.VK_DOWN:
TuxAbajo();
break;
case KeyEvent.VK_LEFT:
TuxIzq();
break;
case KeyEvent.VK_RIGHT:
TuxDer();
break;
}

}
}
wakeupOn(trigger);
}
private void TuxArriba(){
Transform3D tr1=new Transform3D();
Transform3D tr2=new Transform3D();
this.Camara.getTransform(tr2);
Vector3f vec=new Vector3f();
vec.z=-0.1f;
tr1.set(vec);
tr2.mul(tr1);
this.Camara.setTransform(tr2);
vec.z=-0.1f;
this.Node.getTransform(tr2);
tr1.set(vec);
tr2.mul(tr1);
Node.setTransform(tr2);
}
private void TuxAbajo(){
Transform3D tr1=new Transform3D();
Transform3D tr2=new Transform3D();
this.Camara.getTransform(tr2);
Vector3f vec=new Vector3f();
vec.z=+0.1f;
tr1.set(vec);
tr2.mul(tr1);
this.Camara.setTransform(tr2);
vec.z=+0.1f;
this.Node.getTransform(tr2);
tr1.set(vec);
tr2.mul(tr1);
Node.setTransform(tr2);
}
private void TuxIzq(){
Transform3D tr1=new Transform3D();
Transform3D tr2=new Transform3D();
tr1.rotY(Math.PI/20);
this.Node.getTransform(tr2);
tr2.mul(tr1);
this.Node.setTransform(tr2);

Vector3f vec=new Vector3f(0f,0f,-this.Zoom);
tr1.set(vec);
this.Camara.getTransform(tr2);
tr2.mul(tr1);
tr1.rotY(Math.PI/20);
tr2.mul(tr1);
vec=new Vector3f(0f,0f,this.Zoom);
tr1.set(vec);
tr2.mul(tr1);
Camara.setTransform(tr2);

}
private void TuxDer(){
Transform3D tr1=new Transform3D();
Transform3D tr2=new Transform3D();
tr1.rotY(-Math.PI/20);
this.Node.getTransform(tr2);
tr2.mul(tr1);
this.Node.setTransform(tr2);

Vector3f vec=new Vector3f(0f,0f,-this.Zoom);
tr1.set(vec);
this.Camara.getTransform(tr2);
tr2.mul(tr1);
tr1.rotY(-Math.PI/20);
tr2.mul(tr1);
vec=new Vector3f(0f,0f,this.Zoom);
tr1.set(vec);
tr2.mul(tr1);
Camara.setTransform(tr2);

}
}

5 comentarios:

  1. Gracias, que buen ejemplo hiciste es un gran aporte para el proyecto. Felicidades por tu nuevo blog, espero que sigas adelante y se vuelva un blog muy conocido por sus grandes aportes y ayuda en programacion :). Hugo Espinosa 070809

    ResponderEliminar
  2. Muy bueno el programita... se nota que esto es lo tuyo jeje...

    ResponderEliminar
  3. Hola que tal, me gusto tu ejemplo, gracias por la ayuda, me gustaria aportar tambien un poco, en la clase TuxBehavior la llamada al TransformGroup podrias ahorrartela ya que usas el TransformGroup "Camara" que extraes del Universo y con hacerle cambios a el se propaga la funcionalidad hacia todos sus hijos :D yo lo probe y me funciono :p

    ResponderEliminar
  4. ok, gracias pero fíjate que hace 6 meses le hice otra actualización a este ejemplo aquí esta el post, disculpa por contestar rápido muchas actividades: http://muytux.blogspot.com/2010/02/colisiones-y-movimiento-de-la-camara.html

    ResponderEliminar