I'm trying to make the game, using LibGDX. I create a Model from *g3db file like this:
public AssetManager assets;
assets = new AssetManager();
assets.load("data/road.g3db", Model.class);
Model road = assets.get("data/road.g3db", Model.class);
ModelInstance roadInstance = new ModelInstance(road);
data/road.g3db is a а long ribbon like road. I make its in blender 3d. Somthing like this:
****
* ***** ****
* * *
*** *******
My question is: how can i create body from this Model? I try this:
ChainShape shape = new ChainShape();
Mesh roadMesh = roadInstance.model.meshes.get(0);
float ar[] = new float[roadMesh.getVerticesBuffer().capacity()];
roadMesh.getVerticesBuffer().get(ar);
shape.createChain(ar);
But there are error:
AL lib: (EE) alc_cleanup: 1 device not closed
Assertion failed!
Program: C:\Program Files (x86)\Java\jre7\bin\javaw.exe
File: /var/lib/jenkins/workspace/libgdx/gdx/jni/Box2D/Collision/Shapes/b2ChainShape.cpp,
Line 63
Expression: b2DistanceSquared(v1, v2) > 0.005f * 0.005f
Is there another way to create body for long road ?
Box2D is, as the name says, for 2D physics. If you have a 3D game, with 2D physics only, you can easily use it under the hood and change the view to 3D. But if you need 3D physics i would suggest to use PhysicsBullet. It is a 3D Collision Detection and Rigid Body Dynamics Library.
To give you something to read: Bullet physics on GitHub
Related
I have a problem with the rendering of a json object created with Box2D Editor .
My problem is that even if the object septum (correctly according to the tutorial found online ) , it is not seen on the stage.
private void createBottle() {
// 0. Create a loader for the file saved from the editor.
BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal("ball.json"));
// 1. Create a BodyDef, as usual.
BodyDef bd = new BodyDef();
bd.position.set(spaceShip.getX(), spaceShip.getY());
bd.type = BodyDef.BodyType.DynamicBody;
// 2. Create a FixtureDef, as usual.
FixtureDef fd = new FixtureDef();
fd.density = 1;
fd.friction = 0.5f;
fd.restitution = 0.3f;
// 3. Create a Body, as usual.
bottleModel = world.createBody(bd);
// 4. Create the body fixture automatically by using the loader.
loader.attachFixture(bottleModel, "test01", fd, 8);
}
In the create method i have this:
createBottle();
in the Render method:
Vector2 bottlePos = bottleModel.getPosition().sub(bottleModelOrigin);
bottleSprite.setPosition(bottlePos.x, bottlePos.y);
bottleSprite.setOrigin(bottleModelOrigin.x, bottleModelOrigin.y);
bottleSprite.setRotation(bottleModel.getAngle() * MathUtils.radiansToDegrees);
bottleSprite.draw(batch);
I do not understand why it is not added , I can not see on the screen .... I have other items assigned with the z position ... but nothing does not want to appear .
My goal is to enter the mazes made with box2d editor and will move into a commanding object with a virtual joystick , this is the game I'm doing ...
if someone wants to enlighten me on how to import successfully , it is the first time you use an external editor with import json .
EDIT
I tried to follow the tutorial also before posting my question here on .
However the error is on Vector2 .... also the tutorial is clear but not working ... even online I saw other examples that they said that did not work ... there is not a resource running around? I have not found yet
You might try as it is stated here here:
LibGDX draws images with a reference to their bottom left corner. Therefore, if you left the reference point at the bottom left corner of your body, you can directly place the image at the location of the reference point (returned by Box2d engine with the getPosition() method).Else, if you changed the reference point location, you need to take it into account to draw the image.
Therefore, you need to store the reference point somewhere for later use:
bottleModelOrigin = loader.getOrigin("test01", BOTTLE_WIDTH).cpy();
Finally, in your render method, you can draw the image at the reference point location, offset by its local coordinates relatively to the image bottom left corner:
public void render() {
Vector2 bottlePos = bottleModel.getPosition().sub(bottleModelOrigin);
bottleSprite.setPosition(bottlePos.x, bottlePos.y);
bottleSprite.setOrigin(bottleModelOrigin.x, bottleModelOrigin.y);
bottleSprite.setRotation(bottleModel.getAngle() * MathUtils.radiansToDegrees);
}
The problem in your code seems to be that you don't have a variable named bottleModelOrigin atleast it is not visible in your first example. Although in that case a NullPointerException should have been thrown.
My problem is that I am trying to import a 3D model from an STL file to a JavaFX application. I followed the code in this link How to create 3d shape from STL in JavaFX 8? and it's only working with the jewel file mentioned there, but I've tried with other STL files and it's not working!
I can't see why it's not working with the other files. Can anyone explain this?
Any help please, as soon as possible!
As you are already using an STL importer from this site, you will find in the same web a 3D model browser you can use to preview your models before importing them to your JavaFX application.
If they can't be imported with this browser, the problem may be related to a non valid STL format in your files.
If they are imported, then the problem may be in your application. Embed the call in a try-catch and post the exception you may enconter.
StlMeshImporter stlImporter = new StlMeshImporter();
try {
stlImporter.read(this.getClass().getResource("<STLfile>.stl"));
}
catch (ImportException e) {
e.printStackTrace();
return;
}
EDIT
If no exception is thrown while reading the model, the next step would be inserting the returned mesh into a MeshView and show it on our scene:
TriangleMesh mesh = stlImporter.getImport();
stlImporter.close();
MeshView mesh=new MeshView(cylinderHeadMesh);
Group root = new Group(mesh);
Scene scene = new Scene(root, 1024, 800, true);
Camera camera = new PerspectiveCamera();
scene.setCamera(camera);
primaryStage.setScene(scene);
primaryStage.show();
Since the model could be too small or too big for our scene (related to the camera and the point of view we are using), we should print the bounding box of our model, and then scale it up or down accordingly:
System.out.println("mesh: "+mesh.getBoundsInLocal().toString());
mesh.setScaleX(1d);
mesh.setScaleY(1d);
mesh.setScaleZ(1d);
Or we could change the camera parameters:
double max = Math.max(mesh.getBoundsInLocal().getWidth(),
Math.max(mesh.getBoundsInLocal().getHeight(),
mesh.getBoundsInLocal().getDepth()));
camera.setTranslateZ(-3*max);
I created a simple model of a barrel (.zip) in Blender 2.69. Then I created a UV map in Blender and made a UV mapped texture out of it (its in the archive, too). Then I imported my texture in Blender, now the mapping matches:
In Blender the model looks fine so far:
By using the Ogre exporter plugin that I installed via the jmonkeyengine SDK, I exported the model. The result of this is my OgreXML format file of the barrel (I did not export material).
Now, I tried to add the barrel to my world like this:
this.barrel = this.assetManager.loadModel("models/barrel/Barrel.mesh.xml");
Material barrelMat = new Material(this.assetManager,
"Common/MatDefs/Light/Lighting.j3md");
barrelMat.setTexture("DiffuseMap",
this.assetManager.loadTexture("models/barrel/Barrel.jpg"));
barrelMat.setBoolean("UseMaterialColors", true);
barrelMat.setColor("Diffuse", ColorRGBA.White);
barrelMat.setColor("Specular", new ColorRGBA(0.3f, 0.1f, 0, 1));
barrelMat.setFloat("Shininess", 4f);
this.barrel.setMaterial(barrelMat);
this.rootNode.attachChild(this.barrel);
The result is this:
Is there something else I have to consider when setting the texture for my UV mapped model?
Often when transferring models from Blender to something like JME, the textures will be upside down. Where you load the texture:
barrelMat.setTexture(“DiffuseMap”,
assetManager.loadTexture(“models/barrel/Barrel.jpg”));
Instead use the TextureKey form of the loadTexture() method and pass yFlip false since true is the default.
assetManager.loadTexture(new TextureKey(“models/barrel/Barrel.jpg”, false));
That should fix your issue.
References:
loadTexture() : http://hub.jmonkeyengine.org/javadoc/com/jme3/asset/AssetManager.html#loadTexture(com.jme3.asset.TextureKey)
TextureKey : http://hub.jmonkeyengine.org/javadoc/com/jme3/asset/TextureKey.html#TextureKey(java.lang.String,%20boolean)
Imagine I have a model of a human with some accessory like sunglasses, a hat, a chain and so on. Is there any way to switch the visibility of those items inside my libGDX application, by writing something like:
modelInstance.getNode("sunglasses").setVisible(false)
You can set a blending attibute to its material:
blendingAttribute = new BlendingAttribute(GL10.GL_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
material = modelinstance.materials.get(0);
material.set(blendingAttribute);
Then you can set its opacity like this:
blendingAttribute.opacity = 0.5F; //0-1
I've been trying to make a surface in Java 3D reflect in a similar manner to a mirror. The API says to use TexCoordGeneration in SPHERE_MAP mode, but it doesn't seem to be making any difference at all.
Can anyone spot what I'm doing wrong?
Appearance reflectAp = new Appearance();
TexCoordGeneration tex = new TexCoordGeneration();
tex.setGenMode(TexCoordGeneration.SPHERE_MAP);
reflectAp.setMaterial(matFact.silver());
reflectAp.setTexCoordGeneration(tex);
flat = new Box(.7f, .9f, .01f, Primitive.GENERATE_NORMALS | Primitive.GENERATE_TEXTURE_COORDS, reflectAp);