I’m really new to Libgdx, and decided to try out some codes to familiarise myself with it. I’m trying to write a program such that a sphere is rendered at the position of the user’s click.

Here’s my code:

package com.mygdx.game;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g3d.Environment;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.FPSLogger;

public class Render implements ApplicationListener {
    public Environment environment;
    public PerspectiveCamera cam;
    public CameraInputController camController;
    public ModelBatch modelBatch;
    private FPSLogger fps;

    public Model model;

    public void create() {

        environment = new Environment();
        environment.set(new ColorAttribute(ColorAttribute.Specular, 0.4f, 0.4f, 0.4f, 0.6f));
        environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, 0f, 0f, -1f));

        modelBatch = new ModelBatch();

        cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        cam.position.set(0f, 0f, 10f);
        cam.near = 1f;
        cam.far = 300f;

        ModelBuilder modelBuilder = new ModelBuilder();
        model = modelBuilder.createSphere(1f, 1f, 1f, 30, 30,
                new Material(), Usage.Position | Usage.Normal)
        camController = new CameraInputController(cam);
        fps = new FPSLogger();

    public void render() {

        if (Gdx.input.justTouched()){
            ModelInstance Instance = new ModelInstance (model);
             Instance.transform.setToTranslation(Gdx.input.getX(),Gdx.input.getY(), 0);

        Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

        for (ModelInstance instance: DataBase.array) {
            modelBatch.render(instance, environment);


    public void dispose() {

    public void resize(int width, int height) {

    public void pause() {

    public void resume() {

The DataBase.array is simply an ArrayList.

What am I doing wrong? Any help would be really appreciated; thanks!

  • \$\begingroup\$ I'm not very familiar with LibGDX but shouldn't you check for touch input from the user every frame, instead of only checking for it once, at the start-up of the application (which you do at the moment, in the create() function)? \$\endgroup\$ Jan 18 '20 at 7:50
  • \$\begingroup\$ Right! Unfortunately, it still doesn’t work after that :( \$\endgroup\$ Jan 18 '20 at 13:21
  • \$\begingroup\$ You do realize that you did not tell us what the issue is, right? \$\endgroup\$
    – Vaillancourt
    Jan 24 '20 at 3:27

I see few mistakes in your code :

environment.set(new ColorAttribute(ColorAttribute.Specular, 0.4f, 0.4f, 0.4f, 0.6f));

It has no effect since specular should only used with materials. Maybe you wanted ambiant light. Then replace it by ColorAttribute.AmbientLight.

The main problem in your case is coordinates conversion from screen space to world space. What you can do is :

// get screen space Z from world space zero
float z = cam.project(new Vector3(0,0,0)).z;
// get world space position from screen space pointer
Vector3 v = cam.unproject(new Vector3(Gdx.input.getX(),Gdx.input.getY(), z));
// set your model position in world space
Instance.transform.setToTranslation(v.x, v.y, v.z);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.