- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 3d 中创建弹跳球程序 uisng libgdx。我是完全的初学者,刚刚开始接触 libgdx。我能够检测到球棒和球之间的碰撞,但无法让它弹回
Here is my complete code
public class Main implements ApplicationListener {
final static short GROUND_FLAG = 1 << 8;
final static short OBJECT_FLAG = 1 << 9;
final static short ALL_FLAG = -1;
class MyContactListener extends ContactListener {
@Override
public boolean onContactAdded(int userValue0, int partId0, int index0,
int userValue1, int partId1, int index1) {
//instances.get(userValue0).moving = false;
//instances.get(userValue1).moving = false;
return true;
}
}
static class MyMotionState extends btMotionState {
Matrix4 transform;
@Override
public void getWorldTransform(Matrix4 worldTrans) {
worldTrans.set(transform);
}
@Override
public void setWorldTransform(Matrix4 worldTrans) {
transform.set(worldTrans);
}
}
static class GameObject extends ModelInstance implements Disposable {
public final btRigidBody body;
public boolean moving;
public final MyMotionState motionState;
public GameObject(Model model, String node,
btRigidBody.btRigidBodyConstructionInfo constructionInfo) {
super(model, node);
motionState = new MyMotionState();
motionState.transform = transform;
body = new btRigidBody(constructionInfo);
body.setMotionState(motionState);
}
@Override
public void dispose() {
// TODO Auto-generated method stub
body.dispose();
motionState.dispose();
}
static class Constructor implements Disposable {
public final Model model;
public final String node;
public final btCollisionShape shape;
public final btRigidBody.btRigidBodyConstructionInfo constructionInfo;
public static Vector3 inertia = new Vector3();
public Constructor(Model model, String node,
btCollisionShape shape, float mass) {
this.model = model;
this.node = node;
this.shape = shape;
if (mass > 0f) {
shape.calculateLocalInertia(mass, inertia);
} else {
inertia.set(0, 0, 0);
}
this.constructionInfo = new btRigidBody.btRigidBodyConstructionInfo(
mass, null, shape, inertia);
}
public GameObject construct() {
return new GameObject(model, node, constructionInfo);
}
@Override
public void dispose() {
shape.dispose();
constructionInfo.dispose();
}
}
}
PerspectiveCamera camera;
Environment environment;
ModelBatch modelBatch;
Model model;
Array<GameObject> instances;
ArrayMap<String, GameObject.Constructor> constructors;
btCollisionConfiguration configuration;
btDispatcher dispatcher;
btBroadphaseInterface broadphaseInterface;
btDynamicsWorld dynamicWorld;
btConstraintSolver solver;
MyContactListener contactListener;
float spawTimer;
@Override
public void create() {
// TODO Auto-generated method stub
Bullet.init();
modelBatch = new ModelBatch();
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f,
0.4f, 0.4f, 1f));
environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f,
-0.8f, -0.2f));
camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
camera.position.set(3f, 7f, 10f);
camera.lookAt(0, 4f, 0);
camera.near = 1f;
camera.far = 300f;
camera.update();
ModelBuilder builder = new ModelBuilder();
builder.begin();
builder.node().id = "ground";
builder.part("ground", GL20.GL_TRIANGLES,
Usage.Position | Usage.Normal,
new Material(ColorAttribute.createDiffuse(Color.RED))).box(5f,
1f, 5f);
builder.node().id = "sphere";
builder.part("sphere", GL20.GL_TRIANGLES,
Usage.Position | Usage.Normal,
new Material(ColorAttribute.createDiffuse(Color.GREEN)))
.sphere(1f, 1f, 1f, 10, 10);
model = builder.end();
constructors = new ArrayMap<String, Main.GameObject.Constructor>(
String.class, GameObject.Constructor.class);
constructors.put("ground", new GameObject.Constructor(model, "ground",
new btBoxShape(new Vector3(2.5f, .5f, 2.5f)), 0f));
constructors.put("ball", new GameObject.Constructor(model, "sphere",
new btSphereShape(0.5f), 3f));
configuration = new btDefaultCollisionConfiguration();
dispatcher = new btCollisionDispatcher(configuration);
solver = new btSequentialImpulseConstraintSolver();
broadphaseInterface = new btDbvtBroadphase();
dynamicWorld = new btDiscreteDynamicsWorld(dispatcher,
broadphaseInterface, solver, configuration);
dynamicWorld.setGravity(new Vector3(0, -10, 0));
contactListener = new MyContactListener();
instances = new Array<Main.GameObject>();
GameObject object = constructors.get("ground").construct();
object.body.setCollisionFlags(object.body.getCollisionFlags()
| btCollisionObject.CollisionFlags.CF_KINEMATIC_OBJECT);
instances.add(object);
dynamicWorld.addRigidBody(object.body);
object.body.setActivationState(Collision.DISABLE_DEACTIVATION);
createBall();
}
public void createBall() {
GameObject obj = constructors.get("ball").construct();
//obj.moving = true;
obj.transform.setFromEulerAngles(MathUtils.random(360f),
MathUtils.random(360f), MathUtils.random(360f));
obj.transform.trn(0f, 9f, 0f);
//obj.body.proceedToTransform(obj.transform);
obj.body.setRestitution(1.0f);
obj.body.setFriction(1.0f);
obj.body.setWorldTransform(obj.transform);
obj.body.setUserValue(instances.size);
obj.body.setCollisionFlags(obj.body.getCollisionFlags()
| btCollisionObject.CollisionFlags.CF_CUSTOM_MATERIAL_CALLBACK);
instances.add(obj);
// dynamicWorld.addCollisionObject(obj.body, OBJECT_FLAG, GROUND_FLAG);
dynamicWorld.addRigidBody(obj.body);
}
@Override
public void resize(int width, int height) {
}
float angle, speed = 10f;
@Override
public void render() {
// TODO Auto-generated method stub
final float delta = Gdx.graphics.getDeltaTime();
angle = (angle + delta+speed) % 360;
instances.get(0).transform.setTranslation(0f,MathUtils.sinDeg(angle),0f);
//instances.get(0).body.setWorldTransform(instances.get(0).transform);
dynamicWorld.stepSimulation(delta, 5, 1 / 60f);
Gdx.gl.glClearColor(0.3f, 0.3f, 0.3f, 1.f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
modelBatch.begin(camera);
modelBatch.render(instances, environment);
modelBatch.end();
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
// TODO Auto-generated method stub
for (GameObject obj : instances)
obj.dispose();
instances.clear();
for (GameObject.Constructor ctor : constructors.values())
ctor.dispose();
constructors.clear();
dynamicWorld.dispose();
solver.dispose();
broadphaseInterface.dispose();
dispatcher.dispose();
configuration.dispose();
contactListener.dispose();
modelBatch.dispose();
model.dispose();
}
}
}
这是我更新后的代码,现在我可以反弹地面,这不是我的目标,但我的目标是在球击中时反弹球,用于地面移动的代码无法应用于球。请帮忙。
最佳答案
这里是代码。最后球反弹
public class Main implements ApplicationListener {
final static short GROUND_FLAG = 1 << 8;
final static short OBJECT_FLAG = 1 << 9;
final static short ALL_FLAG = -1;
public float delta;
class MyContactListener extends ContactListener {
@Override
public boolean onContactAdded(int userValue0, int partId0, int index0,
int userValue1, int partId1, int index1) {
// Gdx.app.log("onContact", "onContact Added");
instances.get(1).body.setLinearVelocity(new Vector3(0, 10f, 0f));
return true;
}
}
static class MyMotionState extends btMotionState {
Matrix4 transform;
@Override
public void getWorldTransform(Matrix4 worldTrans) {
worldTrans.set(transform);
}
@Override
public void setWorldTransform(Matrix4 worldTrans) {
transform.set(worldTrans);
}
}
static class GameObject extends ModelInstance implements Disposable {
public final btRigidBody body;
public boolean moving;
public final MyMotionState motionState;
public GameObject(Model model, String node,
btRigidBody.btRigidBodyConstructionInfo constructionInfo) {
super(model, node);
motionState = new MyMotionState();
motionState.transform = transform;
body = new btRigidBody(constructionInfo);
body.setMotionState(motionState);
}
@Override
public void dispose() {
// TODO Auto-generated method stub
body.dispose();
motionState.dispose();
}
static class Constructor implements Disposable {
public final Model model;
public final String node;
public final btCollisionShape shape;
public final btRigidBody.btRigidBodyConstructionInfo constructionInfo;
public static Vector3 inertia = new Vector3();
public Constructor(Model model, String node,
btCollisionShape shape, float mass) {
this.model = model;
this.node = node;
this.shape = shape;
if (mass > 0f) {
shape.calculateLocalInertia(mass, inertia);
} else {
inertia.set(0, 0, 0);
}
this.constructionInfo = new btRigidBody.btRigidBodyConstructionInfo(
mass, null, shape, inertia);
}
public GameObject construct() {
return new GameObject(model, node, constructionInfo);
}
@Override
public void dispose() {
shape.dispose();
constructionInfo.dispose();
}
}
}
PerspectiveCamera camera;
Environment environment;
ModelBatch modelBatch;
Model model;
Array<GameObject> instances;
ArrayMap<String, GameObject.Constructor> constructors;
btCollisionConfiguration configuration;
btDispatcher dispatcher;
btBroadphaseInterface broadphaseInterface;
btDynamicsWorld dynamicWorld;
btConstraintSolver solver;
MyContactListener contactListener;
float spawTimer;
GameObject obj;
@Override
public void create() {
// TODO Auto-generated method stub
Bullet.init();
modelBatch = new ModelBatch();
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f,
0.4f, 0.4f, 1f));
environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f,
-0.8f, -0.2f));
camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
camera.position.set(3f, 7f, 10f);
camera.lookAt(0, 4f, 0);
camera.near = 1f;
camera.far = 300f;
camera.update();
ModelBuilder builder = new ModelBuilder();
builder.begin();
builder.node().id = "ground";
builder.part("ground", GL20.GL_TRIANGLES,
Usage.Position | Usage.Normal,
new Material(ColorAttribute.createDiffuse(Color.RED))).box(5f,
1f, 5f);
builder.node().id = "sphere";
builder.part("sphere", GL20.GL_TRIANGLES,
Usage.Position | Usage.Normal,
new Material(ColorAttribute.createDiffuse(Color.GREEN)))
.sphere(1f, 1f, 1f, 10, 10);
model = builder.end();
constructors = new ArrayMap<String, Main.GameObject.Constructor>(
String.class, GameObject.Constructor.class);
constructors.put("ground", new GameObject.Constructor(model, "ground",
new btBoxShape(new Vector3(2.5f, .5f, 2.5f)), 0f));
constructors.put("ball", new GameObject.Constructor(model, "sphere",
new btSphereShape(0.5f), 0.5f));
configuration = new btDefaultCollisionConfiguration();
dispatcher = new btCollisionDispatcher(configuration);
solver = new btSequentialImpulseConstraintSolver();
broadphaseInterface = new btDbvtBroadphase();
dynamicWorld = new btDiscreteDynamicsWorld(dispatcher,
broadphaseInterface, solver, configuration);
dynamicWorld.setGravity(new Vector3(0, -8f, 0));
contactListener = new MyContactListener();
instances = new Array<Main.GameObject>();
GameObject object = constructors.get("ground").construct();
object.body.setCollisionFlags(object.body.getCollisionFlags()
| btCollisionObject.CollisionFlags.CF_KINEMATIC_OBJECT);
instances.add(object);
dynamicWorld.addRigidBody(object.body);
object.body.setActivationState(Collision.DISABLE_DEACTIVATION);
createBall();
}
public void createBall() {
obj = constructors.get("ball").construct();
obj.transform.trn(0f, 11f, 0f);
obj.body.setWorldTransform(obj.transform);
obj.body.setUserValue(instances.size);
obj.body.setCollisionFlags(obj.body.getCollisionFlags()
| btCollisionObject.CollisionFlags.CF_CUSTOM_MATERIAL_CALLBACK);
obj.body.setFriction(1.0f);
obj.body.setRestitution(3.0f);
instances.add(obj);
dynamicWorld.addRigidBody(obj.body);
}
@Override
public void resize(int width, int height) {
}
float angle, speed = 10f, yTranslate = 0f, zAngle = 0f;
@Override
public void render() {
// TODO Auto-generated method stub
delta = Gdx.graphics.getDeltaTime();
angle = (angle + delta + speed) % 360;
if (Gdx.input.isTouched()) {
if (yTranslate > -60f) {
zAngle -= 0.2f;
} else {
// yTranslate = 30f;
}
instances.get(0).transform.setToRotation(0f, 0f, 2f, zAngle);
} else {
instances.get(0).transform.setToRotation(0f, 0f, 1f, 30f);
instances.get(0).transform.setTranslation(0f, 0f, 0f);
}
//instances.get(0).transform.setTranslation(0f,0f,0f);
dynamicWorld.stepSimulation(delta, 5, 1 / 60f);
Gdx.gl.glClearColor(0.3f, 0.3f, 0.3f, 1.f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
modelBatch.begin(camera);
modelBatch.render(instances, environment);
modelBatch.end();
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
// TODO Auto-generated method stub
for (GameObject obj : instances)
obj.dispose();
instances.clear();
for (GameObject.Constructor ctor : constructors.values())
ctor.dispose();
constructors.clear();
dynamicWorld.dispose();
solver.dispose();
broadphaseInterface.dispose();
dispatcher.dispose();
configuration.dispose();
contactListener.dispose();
modelBatch.dispose();
model.dispose();
}
}
关于android - 使用 bullet3d 物理在 libgdx 中弹跳球,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24429249/
我试图让球弹起,或者在它们碰撞时反转方向。我让它检查移动方法中的碰撞。它检查两个球之间是否发生碰撞,如果发生碰撞,它将反转速度。问题是,有时球会互相穿过,主要是较小的球。球要么弹跳得早,要么弹得晚,要
我一直在努力根据 2 个弹跳圆/球获得正确的矢量值。我知道他们什么时候反弹;使用毕达哥拉斯,但后来我迷路了。我知道我可能不得不使用三 Angular 函数 cos/sin/tan2。 情况,以我的形象
using UnityEngine; using System.Collections; public class audio : MonoBehaviour { public AudioClip
我正在尝试创建 3d 球体的动画,该动画是由该球体表面上的随机点制作的。这是我的代码,我在其中创建 500 个随机极点,然后将这些极坐标转换为笛卡尔坐标,然后将 X 和 Y 坐标映射到屏幕。这就是我得
我创建的这个程序应该基本上使用公式 V=Pi*h^2(3r-h)/3 但我的最终答案并没有相加。 例如:如果我用 1 代替半径,用 2 代替高度,我应该得到 4.18,但通过程序我得到 -1。 #in
我正在尝试制作一个简单的游戏,但如果我需要弹跳球的图像,我该怎么做呢?我正在做这个- function draw() { ctx.clearRect(0, 0, 300, 300);
我只是提出一个有可能结束的想法。我需要画一个 Crystal 球,红色和蓝色粒子随机分布在其中。我想我必须使用 photoshop,甚至尝试在图像中制作球,但由于这是用于研究论文并且不必很花哨,我想知
我有圆与圆相交的代码。但我需要将其扩展到 3-D。你能帮我写函数吗? static class Point{ double x, y, z; int dimension; Po
目标:我有一个三 Angular 形的球。球具有初始位置和速度。我试图弄清楚球会击中三 Angular 形的哪一边。 我试过的: I derived a formula通过参数化球的路径和三 Angu
由于我是 cocos2d 的新手,而且我很挣扎。任何人都可以建议我如何解决这个问题。 我有 3 个盒子(它们是运动体) 还有多个球(它们是动态物体),每个球都有一个标签值(盒子编号)。 我在射球位置和
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 7 年前。 Improve this qu
我正在为 rpm 编写一个 .spec 文件,它只是将一个 tar 球解压到文件系统上的某个目录中 那么我把原来的 tar 球放在哪里呢?我看到的所有示例都是从互联网上下载原始 tar 球的。但就我而
我是 THREE.js 的新手,对物理知识知之甚少 - 但我正在尝试构建一个足球游戏引擎(从顶部看),现在我正在为球的运动而苦苦挣扎。 当尝试将球从一侧移动到另一侧时,旋转始终朝向一个方向,我不明白如
我必须在 Android 中开发一个在屏幕上 move 球的应用程序。我需要用加速度计 move 球。我有这段代码,但球绕过边界并且没有反弹。 package com.example.test
我正在尝试创建一个 Roomba 程序,其中有一个球在屏幕上弹跳,以清洁它经过的瓷砖。该程序应该从所有灰色瓷砖开始,当球经过它们时,瓷砖就会变成白色。目前我有一个可以到处弹跳的球和一个创建 5x5 网
我正在尝试为我当前的作业创建一个加载屏幕效果。 它需要我们创建一个与 position: fixed .以此资金为背景。使用这个 div,有 4 个 与 position: absolute . 我们
我想将一个球(带有图像)扔到一个二维场景中,并在它到达一定距离时检查它是否发生碰撞。但我无法让它正确地“飞”。似乎这个问题已经被问过一百万次了,但随着我发现的越多,我就越困惑..现在我关注了this
这是我的代码 import java.util.*; import javafx.application.Application; import javafx.scene.Scene; import
我是一名优秀的程序员,十分优秀!