Reputation: 246
I am using libgdx.
So far I have two cylinders and am able to rotate them
My goal is to add a button to this view, so I am able to go to other view.
Currently I have camera, cameracontroller, environment, and two models.
code I wrote so far:
public class FirstOfTheAndroid extends ApplicationAdapter {
@Override
public void create() {
// adding a new Renderer
modelBatch = new ModelBatch();
// adding a new environment
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));
// adding a camera
cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.position.set(10f, 10f, 10f);
cam.lookAt(0, 0, 0);
cam.near = 1f;
cam.far = 300f;
cam.update();
// adding a new model
ModelBuilder modelBuilder = new ModelBuilder();
model = modelBuilder.createCylinder(4f, 6f, 4f, 16,
new Material(
ColorAttribute.createDiffuse(Color.GOLD),
ColorAttribute.createSpecular(1, 1, 1, 1),
FloatAttribute.createShininess(8f)),
VertexAttributes.Usage.Position
| VertexAttributes.Usage.Normal
| VertexAttributes.Usage.TextureCoordinates);
model2 = modelBuilder.createCylinder(5f, 5f, 5f, 16,
new Material(
ColorAttribute.createDiffuse(Color.GOLD),
ColorAttribute.createSpecular(1, 1, 1, 1),
FloatAttribute.createShininess(8f)),
VertexAttributes.Usage.Position
| VertexAttributes.Usage.Normal
| VertexAttributes.Usage.TextureCoordinates);
instance = new ModelInstance(model);
instance2 = new ModelInstance(model2);
// adding a camera control
camController = new CameraInputController(cam);
Gdx.input.setInputProcessor(camController);
}
@Override
public void render() {
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClearColor(84/255f, 84/255f, 84/255f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
modelBatch.begin(cam);
modelBatch.render(instance, environment);
modelBatch.render(instance2, environment);
modelBatch.end();
camController.update();
}
}
Upvotes: 2
Views: 554
Reputation: 99
As a commenter mentioned, Scene2D is LibGDX's library for this.
I am rolling my own button system and just figured out how to do it well, here's the link: https://stackoverflow.com/a/43792814/1159741
Finally, here's a simple method - brute force, only effective if you have just a few buttons in the whole game.
Create a handleInput()
method in your game class. In render():
if(Gdx.input.justTouched()) handleInput();
and handleInput()
:
Vector3 touchCoords = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0); // use a Vector2 if you switch from Camera to Viewport
camera.unproject(touchCoords); // converts screen coordinates to game coordinates
if(touchCoords.x > [button left side x] &&
touchCoords.x < [button right side x] &&
touchCoords.y > [button bottom y] &&
touchCoords.y < [button top y])
// do something here
else if(... check coordinates for the other button)
// then do this
// and so on for additional buttons
Upvotes: 1