LibGDX 3DSphere is changing shape while moving on the screen with mouse - java

I have created a sphere using Texture by calling createSphere() method on modelBuilder using libGDX. As it is 3D, I used Perspective Camera. I am trying to move the sphere on the screen with the mouse. Its working fine, but the sphere had been changing its shape while moving on the screen.
I tried by changing the position and lookAt of the Perspective Camera. Still, shape is changing. Can anyone suggest the solution.
Here is my part of the code:
modelBatch = new ModelBatch();
ModelBuilder modelBuilder = new ModelBuilder();
Texture texture = new Texture(Gdx.files.internal("ball.png"));
Material material = new
Material(TextureAttribute.createDiffuse(texture));
final long attributes = VertexAttributes.Usage.Position |
VertexAttributes.Usage.TextureCoordinates;
sphere = modelBuilder.createSphere(0.5f, 0.5f, 0.5f, 24, 24, material, attributes);
sphereInstance = new ModelInstance(sphere);
perCamera = new PerspectiveCamera(67, Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
perCamera.position.set(0f, 0f, 3f);
perCamera.lookAt(0f,0f,0f);
perCamera.near = 1f;
perCamera.far = 300f;
perCamera.update();
[Here I have attached screenshot for the reference, in which the regular sphere changes its shape to ellipse as it is moving away from the camera.][1]

Related

Move a shape to place where my finger is touched

#Override
public void create()
{
batch = new SpriteBatch();
shape = new ShapeRenderer();
velocity = new Vector2(100, 0);
position = new Rectangle(0, 5, 100, 100);
font = new BitmapFont();
font.setColor(Color.BLACK);
font.getData().scale(3f);
camera = new OrthographicCamera();
confCamera();
}
#Override
public void render()
{
if(Gdx.input.isTouched())
position.x = Gdx.input.getX() - position.width/2;
position.y = (Gdx.input.getY() - position.height/2);
shape.begin(ShapeRenderer.ShapeType.Filled);
shape.setColor(Color.BLACK);
shape.rect(position.x, position.y, position.width, position.height);
shape.end();
}
It's a simple code, but I'm not undestanding Y axis, my shape moves like a mirror. If I touch on top, my shape goes to bottom. If I touch on bottom, my shape goes to top. How to fix it?
LibGDX (by default) for rendering uses coordinate system where 0 is at bottom of the screen and the more you go up Y coordinate grows.
Also, when you read input coordinates (touches, moves...) you get screen coordinates, but when you render your graphics you are using "world" coordinates. They are in 2 different coordinate system so to convert from screen to world you have to use camera.unproject() call. Should be like:
Vector3 touchPos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);
and then use touchPos.x and touchPos.y.
The similar question is asked here so you can find more answers there:
Using unProject correctly in Java Libgdx

ARCore – Model rotation

I am working in ARCore Android and I want to rotate 3D model on button click.
I have done rotation but the problem is that it would not rotate on current position, i.e. if model is moved somewhere else on plane it come back to its initial position where model is projected and here we can see rotation. I want to rotate my model anywhere on plane and it remains there.
Here is my code
private void rotateRight(TransformableNode node, AxisClass objAxis, Vector3 pos) {
node.getRotationController().setEnabled(true);
node.getScaleController().setEnabled(true);
node.getTranslationController().setEnabled(false);
Quaternion rightMoveVector = new Quaternion();
rightMoveVector = tNode.getLocalRotation();
Quaternion orientations = new Quaternion();
Quaternion orientation = Quaternion.axisAngle(new Vector3(pos.x, 1.0f, pos.z), rotateAngle);
node.setLocalRotation(Quaternion.axisAngle(new Vector3(0.0f, 1.0f, 0.0f), rotateAngle));
rotateAngle = rotateAngle + 30;
objAxis.setRotateRight(String.valueOf(rotateAngle));
objAxis.setY_axis(String.valueOf(Math.toRadians(rotateAngle)));
}
i am answering my own question, it would be helpful for someone.
for model rotation you have to override onupdate method in sceneform android.
after performing rotation you have to set model position so that it remains on its position.
Quaternion q1 = tNode.getLocalRotation();
Quaternion q2 = Quaternion.axisAngle(new Vector3(0f, 1f, 0f), -2f);
tNode.setLocalRotation(Quaternion.multiply(q1, q2));
tNode.setLocalPosition(localPosition);

Box2D Android Studio Drawing inverted shapes

I'm creating a crash test game using Android Studio and LibGDX, I have everything I need on the game set, however I would like to create an upside down triangle like this in Box2D, Obviously, It's not going to look like this but this image is to give you an example
However when I run it it looks like this:
I've tried changing the order of the code in Box2D but it doesn't create an inverted triangle, like this :
private Fixture createcollisionFixture(Body collisionBody) {
Vector2[] vertices = new Vector2[3];
vertices[0] = new Vector2(-0.5f, -0.5f);
vertices[1] = new Vector2(0.5f, -0.5f);
vertices[2] = new Vector2(0, 0.5f);
PolygonShape shape = new PolygonShape();
shape.set(vertices);
Fixture fix = collisionBodyBody.createFixture(shape, 1);
shape.dispose();
return fix;
}
I change it to:
private Fixture createcollision2Fixture(Body collision2Body) {
Vector2[] vertices = new Vector2[3];
vertices[2] = new Vector2(0, 0.5f);
vertices[1] = new Vector2(0.5f, -0.5f);
vertices[0] = new Vector2(-0.5f, -0.5f);
PolygonShape shape = new PolygonShape();
shape.set(vertices);
Fixture fix = collision2Body.createFixture(shape, 1);
shape.dispose();
return fix;
}
But it keeps loading as the second picture, not like an inverted triangle, how do I fix this? Help would be greatly appreciated.
The problem is not in Box2D but in coordinates that you are using to draw triangle. No matter in what order you will be passing them to the Fixture they will produce triangle like this:
If you want to rotate the triangle you have to pass another set of coordinates like this:
So finally your code should look like:
...
Vector2[] vertices = new Vector2[3];
vertices[2] = new Vector2(-0.5, 0.5f);
vertices[1] = new Vector2(0.5f, 0.5f);
vertices[0] = new Vector2(0.5f, -0.5f);
...

Drawing font inside moving sprite libgdx

I have some problems with font drawing in the center of the sprite. The sprite is moving along the screen. I set my orthographic camera to:
w = Gdx.graphics.getWidth();
h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1, h/w);
and:
Sprite spr = new Sprite(starTx);
spr.setSize(0.3f, 0.3f);
spr.setOrigin(0.15f, 0.15f);
spr.setPosition(0.2f, 0.25f*(i+1));
Inside render I have the code:
batch.setProjectionMatrix(camera.combined);
batch.begin();
spr.draw(batch);
font.setScale(1, (w/h)*3);
font.draw(batchFont, mDate, valueX, valueY);
batch.end();
I must draw the font in the center of the sprite. Could anyone tell me how to calculate valueX and ValueY?
Not exactly what you posted, the screen width is 1, so multiplying for it is useless :p More general approach:
valueX = spr.getX()+spr.getWidth()/2F;
valueY = spr.getY()+spr.getHeight()/2F;

Model texturing error with libgdx

I am having an issue with libgdx where when I load a model in any format the texture is not appearing correctly. I'm not sure what is causing this issue.
Example: In 3ds max or blender it looks like this
but when I put it into a libgdx project it looks like this
My code is based on http://blog.xoppa.com/loading-models-using-libgdx/
And I have tried it with and without the lights with no luck
Code:
//Loading the model
assetManager = new AssetManager();
assetManager.load("data/models/M4A1 SOPMOD/M4A1 SOPMOD.g3db", Model.class);
//..................................................
//Calling to an object and drawing it
public void create()
modelBatch = new ModelBatch();
box = Assets.assetManager.get("data/models/M4A1 SOPMOD/M4A1 SOPMOD.g3db", Model.class);
//box = Assets.getModelByName("M4");
//box = modelBuilder.createBox(1f, 1f, 1f, new Material(ColorAttribute.createDiffuse(Color.WHITE), ColorAttribute.createSpecular(Color.WHITE), FloatAttribute.createShininess(64f)), Usage.Position | Usage.Normal);
boxInstance = new ModelInstance(box);
lights = new Lights();
lights.ambientLight.set(1f, 1f, 1f, 1f);
lights.add(new DirectionalLight().set(1f, 1f, 1f, -1f, -0.8f, -0.2f));
}
public void Update(float deltaTime)
{
dotd.camera.rotateAround(Vector3.Zero, new Vector3(0, 1, 0), deltaTime* 50.0f);
dotd.camera.update();
}
public void Render()
{
modelBatch.begin(dotd.camera);
modelBatch.render(boxInstance, lights);
modelBatch.end();
}
My first guess would be that the texture coordinates need to be flipped, try adding (or removing) the -f command line option of fbx-conv (.e.g fbx-conv -f model.fbx).
You also might want to check your normals. Add the following line to your create method: DefaultShader.defaultCullFace = 0; and don't apply lights. If this renders your model better, you probably need to recalculate the normals in 3ds max or blender. Don't forget to remove the defaultCullFace line afterwards.
Finally, obviously, make sure the correct texture is loaded and don't use high poly models.

Categories

Resources