How to center a scaled model on screen?

I have a stupid simple game, and everything is working exactly how I want it to behave:

// This is Groovy but that shouldn't matter; I can intermix lines of
// Java code and it will all work
class MyGame {
  static void main(String[] args) {
    Simulator simulator = new Simulator()
    
    simulator.setShowSettings(false)

    AppSettings settings = new AppSettings(true);
    settings.put("Width", 600)
    settings.put("Height", 900)
    settings.put("Title", "My awesome Game")
    settings.put("VSync", true)
    settings.put("Samples", 4)
    simulator.setSettings(settings)

    simulator.start()
  }
}

class Simulator extends SimpleApplication implements AnimEventListener {
    AnimChannel animationChannel
    AnimControl animationController
    Node avatar

    @Override
    void simpleInitApp() {
        viewPort.setBackgroundColor(ColorRGBA.LightGray)
        DirectionalLight directionalLight = new DirectionalLight()
        rootNode.addLight(directionalLight)

        avatar = assetManager.loadModel('models/Ninja.mesh.xml') as Node
        avatar.localScale = 0.025f

        avatar.rotate(0.0f, 3.14159f, 0.0f)

        rootNode.attachChild(avatar)
        animationController = avatar.getControl(AnimControl)
        animationController.addListener(this)
        animationChannel = animationController.createChannel()

        flyCam.dragToRotate = true

        setPauseOnLostFocus(false)
        setDisplayFps(false)
        setDisplayStatView(false)
    }

    @Override
    void simpleUpdate(float tpf) {
        if(gameSimQueue.peek()) {
            NinjaState ninjaState = determineNinjaState()

            if(ninjaState == ninjaState.Punch) {
                animationChannel.setAnim('Attack3', 0.5f)
                animationChannel.speed = 0.8f
            }
        }
    }

    @Override
    void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
        channel.setAnim('Idle1')
        channel.setLoopMode(LoopMode.DontLoop)
        channel.setSpeed(1f)
    }

    @Override
    void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
        // No-op
    }

    private NinjaState determineNinjaState() {
        // Omitted for brevity; doesn't really matter how this works
    }
}

Except, when this runs, the Ninja model (Ninja.mesh.xml from the test-data.jar) renders on screen like so:

Whereā€™s his head?!? If you look at my code, I had to scale the Ninja down significantly, and am worried that this is somehow throwing off default calculations. So, given his scaling, how can I center him on screen? Changing the height and width of the window has not helped me yet.

Move the object? Move the camera?

You are going to struggle every single second of this if you donā€™t dig in and learn some basic 3D stuff, really.

1 Like

Thanks @pspeed, I understand what you are saying.

In reality though, my spare time is extremely limited and I am just trying to get a very simple demo/proof-of-concept working, and centering the ninja on screen is literally the last tweak I need to make. In a few weeks I will have some major downtime and will be able to do a proper deep dive into the docs, but it was my hope that Iā€™d be able to just finish this PoC, and the answer to this question is the key to that.

Inside of simpleAppInit I added camera.setViewPort(0.0, 0.0, 0.0, 0.0); but that is not working. It appears to me that I need to raise the camera up, but there are so many methods on the camera object, I feel totally overwhelmed.

Iā€™m sure this is just a simple 1- or 2-liner. Any chance you could help me out this one last time, or at least point me to the right javadoc? Once I get this working I will absolutely read up on 3D basics as well as do that deep dive on jME docs. Thanks for any-and-all-help here!

I wonder which of cameraā€™s methods you would use to ā€œsetā€ the ā€œlocationā€?
https://javadoc.jmonkeyengine.org/com/jme3/renderer/Camera.html

I wonder if there is even a method to set the location. Perhaps it is called setLocation()?

I imagine someone who is in so much of a hurry that his eyes are darting all over the page and only reading one character at a time. The advice would be ā€œslow down to go fasterā€.

edit: also, interesting tipā€¦ if you let the fly cam stay enabled you can actually ā€˜flyā€™ the camera around until you find a spot you like. By default there are debug keys like ā€˜cā€™ that will dump the current camera location.

Okā€¦ letā€™s talk.
We also have very limited spare time. Yet weā€™ve spend a fair amount of this time making an engine, with javadoc, and a very thorough documentation with very simple tutorials so that people wouldnā€™t feel overwhelmed.
Youā€™ve asked like 20 questions for the last few day, every single one of which are answered in the documentation or the tutorals, yetā€¦ we still spend some our limited spare time answering and in every answer pointing you toward the documentation.
Now, when you say ā€œI have very little spare time to read the docā€, I read ā€œIā€™d rather waste your time than mineā€. You waste the time we spend writing the docs, and you waste the time we spend answering you, so basicallyā€¦ you waste our time twice.
So tbh, even if I try hard, the only answer that comes to my mind is ā€œfuck offā€.
Soā€¦ READ THE FUCKING DOC.

2 Likes

@hotmeatballsoup Iā€™m just curious. Do you even know Java?
When I started back in the day. I had some Java experience. Not much, but enough. I went through all the tutorials and then I pretty much understood how things work.

Usually try to fix your things yourself. When you think you tried every possible way you know of, then it might be time to post a question on the forums.

EDIT: ctrl + space is also a very good way to explore how JME works.

All of this is written with the best intentions :wink:

@pspeed - thanks for the hint Iā€™ll check it out

@nehon - I literally challenge you to a fist fight

`

Cool down guys.

That was not meant for real right?
Back to topic.

avatar.setLocation(-2, -2, 0);

play with the values and yes the tutorials also will show you how to use setLocation I guess.

If avatar is the spatial then I think you meant setLocalTranslation() and not setLocation().

I elect myself to represent JME in all future fist fights. I will also fight anyone who challenges my representation.

2 Likes

If that needs a team of fist fighters Iā€™m in :smile:

@pspeed: Yes you are right, the IDEā€™s auto-complete helps me in that respect and I mostly take the right one :wink: Just want to give a pointer where OP can move that avatar around.

Ooh, 3D, java AND fist fights! JME has it all!

3 Likes

@hotmeatballsoup do yourself a favor. Read the tutorials ā€¦ ALL OF THEM! If you donā€™t understand a tutorial then you are not ready to write code on that topic. At that point, get some knowledge about that specific topic ā€¦ then re-read the tutorial. Following this process will get you far VERY quickly. Now, you are trying to create a video game ā€¦ basically one of the hardest things to do if you donā€™t have the basic math skills and basic understanding required. That said, when you say you donā€™t have time to read the tutorials I say to you ā€¦ GO PLAY CANDY CRUSH because video game creation is CLEARLY not for you!

2 Likes