View Javadoc
1   /*
2    * Created on 2004-12-10
3    *
4    * TODO To change the template for this generated file go to
5    * Window - Preferences - Java - Code Style - Code Templates
6    */
7   package org.sourceforge.jvb3d.Model;
8   
9   import java.io.IOException;
10  import java.io.ObjectInput;
11  import java.io.ObjectOutput;
12  
13  import javax.media.j3d.Appearance;
14  import javax.media.j3d.BoundingBox;
15  import javax.media.j3d.BranchGroup;
16  import javax.media.j3d.ColoringAttributes;
17  import javax.media.j3d.Node;
18  import javax.media.j3d.PickCylinderSegment;
19  import javax.media.j3d.SceneGraphPath;
20  import javax.media.j3d.Shape3D;
21  import javax.media.j3d.Transform3D;
22  import javax.media.j3d.TransformGroup;
23  import javax.media.j3d.PickBounds;
24  import javax.media.j3d.Bounds;
25  import javax.vecmath.Color3f;
26  import javax.vecmath.Vector3f;
27  import javax.vecmath.Point3d;
28  import javax.media.j3d.BoundingSphere;
29  
30  
31  
32  import com.sun.corba.se.impl.interceptors.PINoOpHandlerImpl;
33  import com.sun.j3d.utils.geometry.Sphere;
34  
35  /***
36   * @author spootnick
37   *
38   * Klasa reprezentująca obiekt mogący się poruszać ale sterowalny tylko za pomocą serializacji
39   */
40  public class MovableObject extends DynamicObject {
41  	protected Transform3D tmpTransform=new Transform3D();
42  	protected Vector3f translation= new Vector3f(0f,0f,0f),vector = new Vector3f();
43  	protected Vector3f forward= new Vector3f(0f,0f,1f);
44  	protected Vector3f up= new Vector3f(0f,1f,0f);
45  	protected Vector3f left= new Vector3f(-1f,0f,0f);
46  	protected Vector3f baseForward= new Vector3f(0f,0f,1f);
47  	protected Vector3f baseLeft= new Vector3f(-1f,0f,0f);
48  	protected float rotation=0;//,vRotation=0;
49  	protected float speed=0,strafeSpeed=0;
50  	protected Vector3f tmpTranslation =new Vector3f();
51  	protected Vector3f nextTranslation = new Vector3f();
52  	protected Point3d begin=new Point3d(),end= new Point3d();
53  	protected PickCylinderSegment picker= new PickCylinderSegment();
54  	protected PickBounds pickerBounds = new PickBounds();
55  	protected float radius=0.4f;
56  	
57  	//private Vector3f prevTranslation= new Vector3f();
58  	
59  	/***
60  	 * Tworzy nowy obiekt z reprezentacją graficzną będącą białą kulą
61  	 */
62  	public MovableObject(){
63  		super();
64  //		robi biala kulke defaultowo jak sie nie poda wygladu
65  		Appearance app= new Appearance();
66  		ColoringAttributes color= new ColoringAttributes();
67  		color.setColor(new Color3f(1f,1f,1f));
68  		app.setColoringAttributes(color);
69  		Sphere s = new Sphere(radius,app);
70  		appearance = s; 
71  		s.setBoundsAutoCompute(false);
72  		BoundingSphere b =new BoundingSphere();
73  		b.setRadius(radius);
74  		s.setBounds(b);
75  		/*BoundingSphere b =new BoundingSphere();
76  		b.setRadius(radius);
77  		this.setBounds(s.getBounds());*/
78  		
79  		//sklada do kupy tzn podpina pod transformacje wyglad i ustawia mozliwosc czytania
80  		transformGroup.setTransform(transform);
81  		transformGroup.addChild(appearance);
82  		this.addChild(transformGroup);
83  	
84  		this.transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
85  		this.transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
86  		//this.setCapability(BranchGroup.ALLOW_COLLISION_BOUNDS_READ);
87  		this.setCapability(BranchGroup.ALLOW_BOUNDS_READ);
88  		this.setCapability(BranchGroup.ALLOW_DETACH);
89  	}
90  	/***
91  	 * @see org.sourceforge.jvb3d.Model.IMakesTurn#nextTurn(long)
92  	 */
93  	public synchronized void nextTurn(long time){
94  		if(time>ttl)
95  			ttl=0;
96  		else
97  			ttl-=time;
98  		float t=(float)time/100000000;
99  		nextTranslation.set(translation);
100 		
101 		//nextTranslation to przesuniecie po czasie t
102 		//jest to przesuniecie aktualne + przesuniecie p/t + przesuniecie l/p
103 		vector.set(forward);
104 		vector.scale(speed*t);
105 		nextTranslation.add(vector);
106 		vector.set(left);
107 		vector.scale(strafeSpeed*t);
108 		nextTranslation.add(vector);
109 	
110 	
111 		tmpTranslation.set(translation);
112 		translation.set(nextTranslation);
113 		applyTransform();
114 		//System.out.println("Po: "+this.getBounds());	
115 		pickerBounds.set(this.getBounds());
116 		Object o; 
117 		if(staticWorld!=null)
118 			o=staticWorld.pickClosest(pickerBounds);
119 		else
120 			o=null;
121 		if(o!=null){
122 			translation.set(tmpTranslation);
123 			applyTransform();
124 		}
125 		
126 	}
127 	/***
128 	 * Powoduje przełożenie aktualnego stanu na transformację grafiki 
129 	 */
130 	protected synchronized void applyTransform(){
131 		transform.set(translation);
132 		tmpTransform.rotY(rotation);
133 		transform.mul(tmpTransform);
134 		this.transformGroup.setTransform(transform);
135 	}
136 	
137 	/***
138 	 * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput)
139 	 */
140 	public synchronized void writeExternal(ObjectOutput arg0) throws IOException {
141 		// TODO Auto-generated method stub
142 		super.writeExternal(arg0);
143 		//if(isServer)
144 		//{
145 			//System.out.println("Serializacja obiektu na serwerze");
146 			arg0.writeFloat(translation.x);
147 			arg0.writeFloat(translation.y);
148 			arg0.writeFloat(translation.z);
149 		//}
150 		
151 		arg0.writeFloat(forward.x);
152 		arg0.writeFloat(forward.y);
153 		arg0.writeFloat(forward.z);
154 		arg0.writeFloat(left.x);
155 		arg0.writeFloat(left.y);
156 		arg0.writeFloat(left.z);
157 		arg0.writeFloat(rotation);
158 		arg0.writeFloat(speed);
159 		arg0.writeFloat(strafeSpeed);
160 	}
161 
162 	/*** 
163 	*	Puki co wersja jest taka:
164 	*	kazdy lokalny movable object decyduje o swojej pozycji w swiecie wiec wyrzuca dane
165 	*	odnosnie swojego polozenia które dostanie od serwera
166 	*	inne reprezentujące graczy zdalnych czytaja te dane bo muszą je dostawac z zwenatrz bo to kto inny 
167 	*	nimi steruje
168 	* @see java.io.Externalizable#readExternal(java.io.ObjectInput)
169 	*/
170 	public synchronized void readExternal(ObjectInput arg0) throws IOException,
171 			ClassNotFoundException {
172 		// TODO Auto-generated method stub
173 		super.readExternal(arg0);
174 		ttl=baseTtl;
175 		
176 		if(isServer)
177 		{
178 			arg0.readFloat();
179 			arg0.readFloat();
180 			arg0.readFloat();
181 		//}
182 			arg0.readFloat();
183 			arg0.readFloat();
184 			arg0.readFloat();
185 			arg0.readFloat();
186 			arg0.readFloat();
187 			arg0.readFloat();
188 			arg0.readFloat();
189 			arg0.readFloat();
190 			arg0.readFloat();
191 		}
192 		else
193 		{
194 			translation.x=arg0.readFloat();
195 			translation.y=arg0.readFloat();
196 			translation.z=arg0.readFloat();
197 		//}
198 			forward.x=arg0.readFloat();
199 			forward.y=arg0.readFloat();
200 			forward.z=arg0.readFloat();
201 			left.x=arg0.readFloat();
202 			left.y=arg0.readFloat();
203 			left.z=arg0.readFloat();
204 			rotation=arg0.readFloat();
205 			speed=arg0.readFloat();
206 			strafeSpeed=arg0.readFloat();
207 		}
208 		
209 	}
210 	
211 	public boolean equals(Object obj){
212 		if(obj instanceof MovableObject){
213 			MovableObject mov = (MovableObject)obj;
214 			return mov.forward.equals(this.forward) &&
215 			mov.left.equals(this.left) &&
216 			mov.translation.equals(this.translation) &&
217 			mov.rotation==this.rotation &&
218 			mov.speed==this.speed &&
219 			mov.strafeSpeed==this.strafeSpeed;
220 		}
221 		return false;
222 	}
223 	
224 	public String toString(){	
225 		return translation.toString();
226 	}
227 	
228 }