java shapes_java – Overlaping Shapes – 错误的重叠形状行为

你应该

switch depth buffering on in your scene.

此问题中的代码基本上已过时,部分内容也不正确:

>对于JavaFX 3D work,建议使用Java 8.

>要构建立方体,请使用Box形状.

>对于其他几何类型,请使用Sphere,Cylinder或MeshView.

>使用Point和Ambient灯照亮场景.

>将Materials应用于3D对象以遮挡它们.

>使用Model importers导入复杂网格模型.

> Builders已弃用.

>为了处理动画,不建议生成另一个线程,而是使用JavaFX animation package.

>对于您问题中的特定动画,RotateTransition是适当的动画.

>您的解决方案不是线程安全的.您不应该修改活动场景图中节点的属性(例如,显示节点的转换属性),而不是应用程序线程(请改用Platform.runLater).

>您没有创建将depth buffering设置为true的场景.深度缓冲标志告诉JavaFX它应该对3D对象应用深度排序和剔除.

还要检查您的系统是否支持JavaFX 3D:

System.out.println(

"3D supported? " +

Platform.isSupported(ConditionalFeature.SCENE3D)

);

Java 8用于旋转立方体的3D示例代码

这是一个用Java 8编码的旋转立方体.

import javafx.animation.*;

import javafx.application.Application;

import javafx.scene.*;

import javafx.scene.paint.*;

import javafx.scene.shape.Box;

import javafx.scene.transform.Rotate;

import javafx.stage.Stage;

import javafx.util.Duration;

public class RotatingCube extends Application {

private static final double SCENE_SIZE = 300;

private static final double BOX_EDGE_LENGTH = SCENE_SIZE / 2d;

private static final Color BOX_COLOR = Color.DARKGREEN;

private static final Color AMBIENT_COLOR = Color.rgb(30, 30, 30);

private static final Color LIGHT_COLOR = Color.WHITE;

private static final Duration ROTATION_DURATION = Duration.seconds(4.5);

@Override

public void start(Stage stage) throws Exception {

Scene scene = new Scene(

new Group(

new AmbientLight(AMBIENT_COLOR),

createPointLight(),

createRotatingBox()

),

SCENE_SIZE, SCENE_SIZE,

true,

SceneAntialiasing.BALANCED

);

scene.setFill(Color.MIDNIGHTBLUE.darker().darker().darker());

scene.setCamera(new PerspectiveCamera());

stage.setScene(scene);

stage.show();

}

private PointLight createPointLight() {

PointLight light = new PointLight(LIGHT_COLOR);

light.setTranslateX( SCENE_SIZE / 2d);

light.setTranslateY( SCENE_SIZE / 2d);

light.setTranslateZ(-SCENE_SIZE);

return light;

}

private Box createRotatingBox() {

final Box box = new Box(BOX_EDGE_LENGTH, BOX_EDGE_LENGTH, BOX_EDGE_LENGTH);

box.setTranslateX(SCENE_SIZE / 2d);

box.setTranslateY(SCENE_SIZE / 2d);

box.setTranslateZ(BOX_EDGE_LENGTH / 2d);

box.setMaterial(new PhongMaterial(BOX_COLOR));

rotateAroundYAxis(box);

return box;

}

private void rotateAroundYAxis(Box box) {

RotateTransition rotate = new RotateTransition(ROTATION_DURATION, box);

rotate.setFromAngle(0);

rotate.setToAngle(360);

rotate.setAxis(Rotate.Y_AXIS);

rotate.setCycleCount(RotateTransition.INDEFINITE);

rotate.setInterpolator(Interpolator.LINEAR);

rotate.play();

}

public static void main(String[] args) {

launch(args);

}

}

用于两个旋转矩形的Java 8 3D示例代码

import javafx.animation.*;

import javafx.application.*;

import javafx.scene.*;

import javafx.scene.paint.Color;

import javafx.scene.shape.Rectangle;

import javafx.scene.transform.Rotate;

import javafx.stage.Stage;

import javafx.util.Duration;

public class RotatingRectangles extends Application {

private static final double SCENE_SIZE = 300;

private static final double EDGE_LENGTH = SCENE_SIZE / 2d;

private static final Duration ROTATION_DURATION = Duration.seconds(4.5);

@Override

public void start(Stage stage) throws Exception {

System.out.println(

"3D supported? " +

Platform.isSupported(ConditionalFeature.SCENE3D)

);

Scene scene = new Scene(

createRotatingShapes(),

SCENE_SIZE, SCENE_SIZE,

true,

SceneAntialiasing.BALANCED

);

scene.setFill(Color.MIDNIGHTBLUE.darker().darker().darker());

scene.setCamera(new PerspectiveCamera());

stage.setScene(scene);

stage.show();

}

private Group createRotatingShapes() {

final Rectangle rect1 = new Rectangle(

EDGE_LENGTH, EDGE_LENGTH,

Color.LIGHTGREEN

);

rect1.setTranslateX(-EDGE_LENGTH / 2d);

rect1.setTranslateY(-EDGE_LENGTH / 2d);

rect1.setTranslateZ( EDGE_LENGTH / 2d);

final Rectangle rect2 = new Rectangle(

EDGE_LENGTH, EDGE_LENGTH,

Color.DARKGREEN

);

rect2.setTranslateX(-EDGE_LENGTH / 2d);

rect2.setTranslateY(-EDGE_LENGTH / 2d);

rect2.setTranslateZ(-EDGE_LENGTH / 2d);

final Group shapes = new Group(

rect1, rect2

);

shapes.setTranslateX(SCENE_SIZE / 2d);

shapes.setTranslateY(SCENE_SIZE / 2d);

shapes.setTranslateZ(EDGE_LENGTH / 2d);

rotateAroundYAxis(shapes);

return shapes;

}

private void rotateAroundYAxis(Node node) {

RotateTransition rotate = new RotateTransition(ROTATION_DURATION, node);

rotate.setFromAngle(0);

rotate.setToAngle(360);

rotate.setAxis(Rotate.Y_AXIS);

rotate.setCycleCount(RotateTransition.INDEFINITE);

rotate.setInterpolator(Interpolator.LINEAR);

rotate.play();

}

public static void main(String[] args) {

launch(args);

}

}

在第一张图片中,暗矩形已在浅色矩形的前面旋转.

在第一张图片中,浅色矩形已在黑色矩形的前面旋转.

因此,您可以看到JavaFX系统正确显示场景中的深度排序形状.

jBullet是一个Java实现的开源物理引擎,可以用于模拟2D和3D物理场景。以下是一个简单的jBullet示例,演示如何创建一个简单的物理场景并模拟物体的运动。 首先,我们需要导入jBullet库: ```java import com.bulletphysics.collision.shapes.*; import com.bulletphysics.dynamics.*; import com.bulletphysics.linearmath.*; import javax.vecmath.Vector3f; ``` 然后,我们可以创建一个物理场景: ```java DynamicsWorld world = new DiscreteDynamicsWorld(new CollisionDispatcher(), new DbvtBroadphase(), new SequentialImpulseConstraintSolver(), new DefaultCollisionConfiguration()); ``` 这里我们使用了一个离散的动力学世界,它包含了一个碰撞检测器、一个宽相检测器、一个顺序脉冲约束求解器和一个默认的碰撞配置。 接下来,我们可以创建一个平面作为地面: ```java StaticPlaneShape groundShape = new StaticPlaneShape(new Vector3f(0, 1, 0), 0); DefaultMotionState groundMotionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f(0, -1, 0), 1.0f)))); RigidBodyConstructionInfo groundRigidBodyCI = new RigidBodyConstructionInfo(0, groundMotionState, groundShape, new Vector3f(0, 0, 0)); RigidBody groundRigidBody = new RigidBody(groundRigidBodyCI); world.addRigidBody(groundRigidBody); ``` 这里我们创建了一个静态平面,沿着y轴为1,位于y轴为0处。我们还创建了一个默认的运动状态,并使用它来创建一个刚体。最后,我们将刚体添加到物理世界中。 现在,我们可以创建一个球体,并给它一个随机的初速度: ```java float ballRadius = 1.0f; SphereShape ballShape = new SphereShape(ballRadius); Vector3f ballInertia = new Vector3f(0, 0, 0); ballShape.calculateLocalInertia(1, ballInertia); DefaultMotionState ballMotionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f((float) (Math.random() * 10), 20, (float) (Math.random() * 10)), 1.0f)))); RigidBodyConstructionInfo ballRigidBodyCI = new RigidBodyConstructionInfo(1, ballMotionState, ballShape, ballInertia); RigidBody ballRigidBody = new RigidBody(ballRigidBodyCI); ballRigidBody.setLinearVelocity(new Vector3f((float) (Math.random() * 10), -10, (float) (Math.random() * 10))); world.addRigidBody(ballRigidBody); ``` 这里我们创建了一个球体,并使用calculateLocalInertia方法计算了它的惯性。我们还创建了一个随机的运动状态,并使用它来创建一个刚体。我们将刚体添加到物理世界中,并为球体设置一个随机的初速度,以便它开始运动。 最后,我们可以模拟物理场景并更新球体的位置: ```java for (int i = 0; i < 100; i++) { world.stepSimulation(1 / 60f, 10); Vector3f ballPos = new Vector3f(); ballRigidBody.getMotionState().getWorldTransform(new Transform()).getTranslation(ballPos); System.out.println("Ball position: " + ballPos.x + ", " + ballPos.y + ", " + ballPos.z); } ``` 这里我们循环100次,每次调用stepSimulation方法模拟物理场景,并使用getWorldTransform方法获取球体的位置并输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值