⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 particle3d.java

📁 运用java3D模拟刚体间的碰撞,爆炸及在万有引力作用下的运动轨迹,设置适当的参数可以模拟天体运动等多种物理现象.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		}
		else
		{
			ufrBounding = new Point3d(Double.parseDouble(line),
										Double.parseDouble(r.readLine()),
										Double.parseDouble(r.readLine()));
			lblBounding = new Point3d(Double.parseDouble(r.readLine()),
										Double.parseDouble(r.readLine()),
										Double.parseDouble(r.readLine()));
		}
		////////////////////////
		
		//Read all the particles in
		for(int i = 0; i < length; i++)
		{
			String name = r.readLine();
			Vector3d position = new Vector3d(Double.parseDouble(r.readLine()),
											Double.parseDouble(r.readLine()),
											Double.parseDouble(r.readLine()));
			Vector3d velocity = new Vector3d(Double.parseDouble(r.readLine()),
											Double.parseDouble(r.readLine()),
											Double.parseDouble(r.readLine()));
			double mass = Double.parseDouble(r.readLine());
			double radius = Double.parseDouble(r.readLine());
			double elasticity = Double.parseDouble(r.readLine());
			
			t.addParticle(name, position, velocity, mass, radius, elasticity);
		
		}
		////////////////////////////////
	}
	
	//Accessor / Setter functions
	public static void setGravity(double g)
	{
		gravity = g;
	}
	
	public static double getGravity()
	{
		return gravity;
	}
	
	public void setRadius(double newRadius)
	{
		
		Transform3D newTransform = new Transform3D();
		object3D.getTransform(newTransform);
		radius = newRadius;
		newTransform.setScale(radius * 10);
		object3D.setTransform(newTransform);
		
		
	}
	
	public double getRadius()
	{
		return radius;
	}
	
	
	public double getMass()
	{
		return mass;
	}
	
	public Vector3d getPosition()
	{
		return position;
	}
	
	public Point3d getPositionP3d()
	{
		return new Point3d(position.x, position.y, position.z);
	}
	
	public Vector3d getSpeed()
	{
		return velocityVector;
	}
	
	public void setPosition(Vector3d newPosition)
	{
		Transform3D newPositionT3d = new Transform3D();
		object3D.getTransform(newPositionT3d);
		newPositionT3d.setTranslation(newPosition);
		
		object3D.setTransform(newPositionT3d);
		
		lastPosition = null;

		initialPosition = (Vector3d)newPosition.clone();
		position = newPosition;
	}
	
	public Quat4d getOrientation()
	{
		return (Quat4d)orientation.clone();
	}
	
	public void setOrientation(Quat4d newOrientation)
	{
		Transform3D newOrientationT3d = new Transform3D();
		object3D.getTransform(newOrientationT3d);
		newOrientationT3d.setRotation(newOrientation);
		
		object3D.setTransform(newOrientationT3d);
		
		lastOrientation = null;

		initialOrientation = (Quat4d)newOrientation.clone();
		orientation = newOrientation;
	
	}
	
	public void setSpeed(Vector3d newSpeed)
	{
		velocityVector = newSpeed;
		initialVelocityVector = (Vector3d)newSpeed.clone();
	}
	
	public Vector3d getAngularSpeed()
	{
		return (Vector3d)angularVelocity.clone();
	}
	
	public void setAngularSpeed(Vector3d newASpeed)
	{
		angularVelocity = newASpeed;
		initialAngularVelocity = (Vector3d)newASpeed.clone();
	}
	
	public void setMass(double newMass)
	{
		mass = newMass;
	}
	
	public String getName()
	{
		return name;
	}
	
	public void setName(String newName)
	{
		name = newName;
	}
	
	public double getElasticity()
	{
		return elasticity;
	}
	
	public void setElasticity(double newElasticity)
	{
		elasticity = newElasticity;
	}
	
	public static void setTraceNumFrames(int newTraceNumFrames)
	{
		traceNumFrames = newTraceNumFrames;
		if(traceNumFrames != -1)
			currentFrame = 0;
		else
			currentFrame = -1;
	}
	
	public static int getTraceNumFrames()
	{
		return traceNumFrames;
	}
	
	public static void setRoot(BranchGroup theBgRoot)
	{
		bgRoot = theBgRoot; 
	}
	
	public static Object[] getParticles()
	{
		return particles.toArray();
	}
	
	public String toString()
	{
		return name;
	}
	
	public static Particle3D getNextParticle(Particle3D p)
	{
		int index = particles.indexOf(p);
		index++;
		
		if(index == particles.size())
			index = 0;
			
		return (Particle3D)particles.get(index);
	}
	
	public static Particle3D getPrevParticle(Particle3D p)
	{
		int index = particles.indexOf(p);
		index--;
		
		if(index == -1)
			index = particles.size() - 1;
			
		return (Particle3D)particles.get(index);
	}
	
	public static void setBoundingBox(Point3d ufr, Point3d lbl)
	{
		ufrBounding = ufr;
		lblBounding = lbl;
	}
	
	public static Point3d getUFRBounding()
	{
		return ufrBounding;
	}
	
	public static Point3d getLBLBounding()
	{
		return lblBounding;
	}
		
	public void setAppearance(Appearance app)
	{
		shape3D.setAppearance(app);
	}
	
	public Shape3D getShape3D()
	{
		return shape3D;
	}
	
	public Vector3d getMomentum()
	{
		Vector3d v = ((Vector3d)velocityVector.clone());
		v.scale(mass);
		return v;
	}
	
	public double getKineticEnergy()
	{
		return velocityVector.lengthSquared() * mass;
	}
	
	public static void setCollisionHandling(int newCollisionHandling)
	{
		if(newCollisionHandling == MOMENTUM || newCollisionHandling == EXPLODE)
			collisionHandling = newCollisionHandling;
	}
	
	public static int getCollsionHandling()
	{
		return collisionHandling;
	}
	
	
	public static long getTime()
	{
		return time;
	}
	
	public static Quat4d makeQFromEulerAngles(double x, double y, double z)
	{
		Quat4d q;
		
		double cx, cy, cz, sx, sy, sz;
		double ccy, ssy, csy, scy;
		
		cz = Math.cos(0.5 * z);
		cy = Math.cos(0.5 * y);
		cx = Math.cos(0.5 * x);
		
		sz = Math.sin(0.5 * z);
		sy = Math.sin(0.5 * y);
		sx = Math.sin(0.5 * x);
		
		ccy = cz * cy;
		ssy = sz * sy;
		csy = cz * sy;
		scy = sz * sy;
		
		q = new Quat4d((ccy * sx - ssy * cx),
						(csy * cx + scy * sx),
						(scy * cx - csy * sx),
						(ccy * cx + ssy * sx));
		
		return q;
	}
	
	public static Vector3d makeEulerAnglesFromQ(Quat4d q)
	{
		double r11,r21,r31,r32,r33,r12,r13;
		double q00,q11,q22,q33;
		double tmp;
		Vector3d u = new Vector3d(0.0,0.0,0.0);
		
		q00 = q.w * q.w;
		q11 = q.x * q.x;
		q22 = q.y * q.y;
		q33 = q.z * q.z;
		
		r11 = q00 + q11 - q22 - q33;
		r21 = 2 * (q.x * q.y + q.w * q.z);
		r31 = 2 * (q.x * q.z - q.w * q.y);
		r32 = 2 * (q.y * q.z + q.w * q.x);
		r33 = q00 - q11 - q22 + q33;
		
		tmp = Math.abs(r31);
		
		if(tmp > 0.999999)
		{
			r12 = 2 * (q.x * q.y - q.w * q.z);
			r13 = 2 * (q.x * q.z + q.w * q.y);
			
			u.x = 0.0;
			u.y = (-(Math.PI / 2) * r31 / tmp);
			u.z = (Math.atan2(-r12, -r31*r13));
			
			return u;
		}
	
		u.x = Math.atan2(r32, r33);
		u.y = Math.asin(-r31);
		u.z = Math.atan2(r21, r11);
		
		return u;
	
	}
	
	//////////////////////////////////////////
	
	private class Point3DAdjacent
	{
		Point3d point = null;
		int key[] = null;
		
		public boolean equals(Object p)
		{
			if(p instanceof Point3d)
			{
				Point3d pt = (Point3d)p;
				return point.x == pt.x && point.y == pt.y && point.z == pt.z;
			}
			return false;
		}
	}
	
	private class Feature
	{
		Point3d vertex = null;
		int[] vertexIndex = null;
		
		Point3d[] face = null;
		Point3d[] line = null;
		
		public Feature(Point3d v, int index)
		{	
			setVertex(v, index);
		}
		
		public Feature(Point3d p1, Point3d p2, Point3d p3, int i1, int i2, int i3)
		{
			setFace(p1,p2,p3, i1, i2, i3);
		}
		public Feature(Point3d p1, Point3d p2, int i1, int i2)
		{
			setLine(p1,p2, i1, i2);
		}
		
		public void setVertex(Point3d v, int index)
		{
			
			vertex = v;
			vertexIndex = new int[1];
			vertexIndex[0] = index;
			face = null;
			line = null;
		}
		
		public void setFace(Point3d p1, Point3d p2, Point3d p3, int i1, int i2, int i3)
		{
			vertex = null;
			vertexIndex = new int[3];
			vertexIndex[0] = i1;
			vertexIndex[1] = i2;
			vertexIndex[2] = i3;
			
			face = new Point3d[3];
			face[0] = p1; face[1] = p2; face[2] = p3;
			line = null;
		}
		
		public void setLine(Point3d p1, Point3d p2, int i1, int i2)
		{
			vertex = null;
			vertexIndex = new int[2];
			vertexIndex[0] = i1;
			vertexIndex[1] = i2;
			
			face = null;			
			line = new Point3d[2];
			line[0] = p1; line[1] = p2;
		}
		
		public Point3d getPoint()
		{
			
			
				
			Point3d result = new Point3d(0.0,0.0,0.0);
			if(vertex != null)
				result = (Point3d)vertex.clone();
			if(face != null)
			{
				result.add(face[0]);
				result.add(face[1]);
				result.add(face[2]);
				result.scale(1.0/face.length);
			}
			else if(line != null)
			{
				result.add(line[0]);
				result.add(line[1]);
				result.scale(1.0/line.length);
			}
			
			Transform3D t = new Transform3D();
			object3D.getTransform(t);
			t.transform(result);
			return result;
		}
		
		public double distanceTo(Feature f)
		{
			return getPoint().distance(f.getPoint());
		}
		
		public double distanceTo(Vector3d v)
		{
			Point3d p = new Point3d(v);
			return getPoint().distance(p);
		}
		
		
		
		public Feature[] getAdjacent()
		{
		
			Feature [] result = null;
			
			if(vertex != null)
			{
				Point3DAdjacent p3a = (Point3DAdjacent)p3dadj.get(vertexIndex[0]);
				result = new Feature[p3a.key.length / 2 + p3a.key.length];
				for(int i = 0; i < p3a.key.length; i += 2)
				{
					result[i / 2] = new Feature(vertex, ((Point3DAdjacent)p3dadj.get(p3a.key[i])).point, 
												((Point3DAdjacent)p3dadj.get(p3a.key[i + 1])).point, vertexIndex[0], p3a.key[i], p3a.key[i+1]);
				}
				for(int i = p3a.key.length / 2; i < p3a.key.length*1.5; i++)
				{
					result[i] = new Feature(vertex, ((Point3DAdjacent)p3dadj.get(p3a.key[i - p3a.key.length / 2])).point, vertexIndex[0], p3a.key[i - p3a.key.length / 2]);
				}
			}
			else if(face != null)
			{
				result = new Feature[3];
				for(int i = 0; i < 3; i++)
					result[i] = new Feature(((Point3DAdjacent)p3dadj.get(vertexIndex[i])).point, vertexIndex[i]);
			}
			else if(line != null)
			{
				result = new Feature[2];
				for(int i = 0; i < 2; i++)
					result[i] = new Feature(((Point3DAdjacent)p3dadj.get(vertexIndex[i])).point, vertexIndex[i]);
			}
			
			return result;
		}
		
		public Vector3d getNormal()
		{
			Vector3d ret = null;
			if(vertex != null)
			{
				 ret = new Vector3d(getPoint());
				 ret.sub(position);
			}
			else if(face != null)
			{
				ret = new Vector3d(face[0]);
				ret.sub(face[1]);
				
				Vector3d v2 = new Vector3d(face[0]);
				v2.sub(face[2]);
				
				ret.cross(ret, v2);
			}
			else
			{
				ret = new Vector3d(getPoint());
				ret.sub(position);
			}
			
			return ret;
		}
		
		
		
/*		public static Vector3d calculateNormal(Feature f1, Feature f2)
		{
			if(f1.face != null)
				return f1.getNormal();
			else if(f2.face != null)
				return f2.getNormal();
				
			
			
		}*/
		
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -