📄 physics3dthread.java
字号:
back.setGeometry(bgGeometry);
backgroundGroup.addChild(back);
return backgroundGroup;
}
////////////////////////////////////////////////
//Get the bounding sphere for the scene
public static BoundingSphere getBoundingSphere()
{
return new BoundingSphere(new Point3d(0.0,0.0,0.0), 1E13);
}
//Add a new particle to the system at the default position etc.
public void addParticle()
{
//Unselect current selected particle
if(lastShape != null)
lastShape.setAppearance((Appearance)app.cloneNodeComponent(true));
//////////////////////////
//Create the new sphere
Sphere sphere = new Sphere(0.1f, Primitive.GENERATE_NORMALS, SPHERE_DETAIL, (Appearance)app.cloneNodeComponent(true));
sphere.getShape().setBoundsAutoCompute(true);
//Set all the capability bits
sphere.getShape().setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
sphere.getShape().setCapability(Shape3D.ALLOW_APPEARANCE_READ);
sphere.getShape().setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
sphere.getShape().setCapability(Shape3D.ALLOW_GEOMETRY_READ);
sphere.getShape().setCapability(Shape3D.ALLOW_BOUNDS_READ);
//Set the sphere geometry to be unique (for exploding purposes, otherwise everything explodes...)
try
{
sphere.getShape().setGeometry((Geometry)sphere.getShape().getGeometry().cloneNodeComponent(true));
sphere.getShape().getGeometry().setCapability( GeometryArray.ALLOW_COORDINATE_READ );
sphere.getShape().getGeometry().setCapability( GeometryArray.ALLOW_COORDINATE_WRITE );
sphere.getShape().getGeometry().setCapability( GeometryArray.ALLOW_COUNT_READ );
}
catch(Exception e)
{
e.printStackTrace();
}
////////////////////////////////////////////////
//Set the appearance and selectedShape
sphere.getShape().setAppearance((Appearance)selectedApp.cloneNodeComponent(true));
lastShape = sphere.getShape();
//Create main transform group
TransformGroup tg = new TransformGroup();
Transform3D t3d = new Transform3D();
tg.setTransform(t3d);
tg.addChild(sphere);
tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
tg.setCapability(TransformGroup.ALLOW_BOUNDS_READ);
//////////////////////////////////////
//Create branch group
BranchGroup bg = new BranchGroup();
bg.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
bg.setCapability(BranchGroup.ALLOW_DETACH);
bg.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
bg.addChild(tg);
////////////////////////
//Finally create the particle3D
Particle3D p3d = new Particle3D("Unamed Particle", tg, new Vector3d(0.0,0.0,0.0), 100.0, new Vector3d(0.0,0.0,0.0), bg, sphere.getShape(), 1);
////////////////////////////////
//Set the userdata to that p3d (for picking purposes)
sphere.getShape().setUserData(p3d);
//Add the new particle to the bgroot
bgRoot.addChild(bg);
//Set the selected object in the properties frame
pframe.setObject(p3d);
}
//Add a new particle to the system with specified positon etc.
public void addParticle(String name, Vector3d position, Vector3d velocity, double mass, double radius, double elasticity)
{
//Unselect current selected particle
if(lastShape != null)
lastShape.setAppearance((Appearance)app.cloneNodeComponent(true));
//////////////////////////
//Create the new sphere
Sphere sphere = new Sphere(0.1f, Primitive.GENERATE_NORMALS, SPHERE_DETAIL, (Appearance)app.cloneNodeComponent(true));
sphere.getShape().setBoundsAutoCompute(true);
//Set all the capability bits
sphere.getShape().setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
sphere.getShape().setCapability(Shape3D.ALLOW_APPEARANCE_READ);
sphere.getShape().setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
sphere.getShape().setCapability(Shape3D.ALLOW_GEOMETRY_READ);
sphere.getShape().setCapability(Shape3D.ALLOW_BOUNDS_READ);
//Set the sphere geometry to be unique (for exploding purposes, otherwise everything explodes...)
try
{
sphere.getShape().setGeometry((Geometry)sphere.getShape().getGeometry().cloneNodeComponent(true));
sphere.getShape().getGeometry().setCapability( GeometryArray.ALLOW_COORDINATE_READ );
sphere.getShape().getGeometry().setCapability( GeometryArray.ALLOW_COORDINATE_WRITE );
sphere.getShape().getGeometry().setCapability( GeometryArray.ALLOW_COUNT_READ );
}
catch(Exception e)
{
e.printStackTrace();
}
////////////////////////////////////////////////
//Set the appearance and selectedShape
sphere.getShape().setAppearance((Appearance)selectedApp.cloneNodeComponent(true));
lastShape = sphere.getShape();
//Create main transform group
TransformGroup tg = new TransformGroup();
Transform3D t3d = new Transform3D();
tg.setTransform(t3d);
tg.addChild(sphere);
tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
tg.setCapability(TransformGroup.ALLOW_BOUNDS_READ);
//////////////////////////////////////
//Create branch group
BranchGroup bg = new BranchGroup();
bg.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
bg.setCapability(BranchGroup.ALLOW_DETACH);
bg.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
bg.addChild(tg);
////////////////////////
//Finally create the particle3D
Particle3D p3d = new Particle3D("Unamed Particle", tg, new Vector3d(0.0,0.0,0.0), 100.0, new Vector3d(0.0,0.0,0.0), bg, sphere.getShape(), 1);
p3d.setMass(mass);
p3d.setRadius(radius);
p3d.setSpeed(velocity);
p3d.setPosition(position);
////////////////////////////////
//Set the userdata to that p3d (for picking purposes)
sphere.getShape().setUserData(p3d);
//Add the new particle to the bgroot
bgRoot.addChild(bg);
//Set the selected object in the properties frame
pframe.setObject(p3d);
}
//Ste the view to zoomDistance away from 0.0,0.0,0.0 looking down the z axis
public void setView(double zoomDistance)
{
TransformGroup tg = u.getViewingPlatform().getViewPlatformTransform();
Transform3D t3d = new Transform3D();
t3d.setTranslation(new Vector3d(0.0, 0.0, zoomDistance));
tg.setTransform(t3d);
}
//Mouse Listeners for the Canvas 3D
//Shape selection
public void mouseClicked(MouseEvent e)
{
//Set last shape appearance back to normal
if(lastShape != null)
lastShape.setAppearance((Appearance)app.cloneNodeComponent(true));
//////////////////////////////////////////
//Tell the pick canvas where the click happened
pickCanvas.setShapeLocation(e);
//Get the result
PickResult result = pickCanvas.pickClosest();
//if the result exists..
if(result != null && (Particle3D)result.getObject().getUserData() != null)
{
//Get the shape
lastShape = (Shape3D)result.getObject();
System.out.println("We have got a match..");
System.out.flush();
lastShape.setAppearance((Appearance)selectedApp.cloneNodeComponent(true));
//Get the particle3d from the user data
Particle3D currentParticle = ((Particle3D)result.getObject().getUserData());
//Set the object in the pframe
pframe.setObject(currentParticle);
}
else
{
//Clear selected object
lastShape = null;
//Clear Properties Frame
pframe.setObject(null);
}
}
//Sets the last shape
public void setLastShape(Shape3D shape)
{
lastShape = shape;
}
/////////////////////////
//Remove the selected object from the system
public void removeSelectedObject()
{
pframe.setObject(null);
if(lastShape != null)
Particle3D.removeParticle((Particle3D)lastShape.getUserData());
}
////////////////////////////////
//Step forward/back one step
public void step()
{
Particle3D.updateAll((rewinding ? -1 : 1) * timeMultiplier * accuracy / 1000.0f);
}
/////////////////////////////////
//Accesor functions///////////////
public static Appearance getNormalAppearance()
{
return (Appearance)app.cloneNodeComponent(true);
}
public static Appearance getSelectedAppearance()
{
return (Appearance)selectedApp.cloneNodeComponent(true);
}
public static void setAccuracy(int newAccuracy)
{
accuracy = newAccuracy;
}
public static int getAccuracy()
{
return accuracy;
}
public static void setTimeMultiplier(float newTimeMultiplier)
{
timeMultiplier = newTimeMultiplier;
}
public static float getTimeMultiplier()
{
return timeMultiplier;
}
////////////////////////////////////
//Unused Mouse Events
public void mousePressed(MouseEvent e){ }
public void mouseEntered(MouseEvent e){ }
public void mouseExited(MouseEvent e){ }
public void mouseReleased(MouseEvent e){ }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -