Interacting with the game environment

Hi all,

I’m new here and new to programming. I’m working on a project and I am in need of fairly simple help. I’m wandering how to put a button in the game and then interact with that button to have an action happen. Same with doors sorta. I want to be able to open doors or push buttons and have those buttons move things etc. I’ve done the tutorials and have a fairly good understanding of them. I’m assuming that I want to do some picking and use collision detection to determine if I’ve collided with a particular object. From here, I’m not sure how to go about instructing the game to provide the functions that I want the button to perform. Thanks in advance.

you would need some kind of a mapping like “id to action”. as soon you did a raytrace (picking example I guess) you have the object you can look up the action. with user data you can add maybe that id which makes that button/door unique to the many you have.
That is one idea, but there will be many possibilities.


I advise you to be familiar with the engine first by following some tutorials. You will find it all on the website.

If not to try to answer your question.

You can, for example, use a control that you add to your space, this controler will take care of moving the space (movement of the door)

For the switch, you have several possibilities.

You can use the GhostControl (JBullet) to detect a collision with a zone, which will determine that you are close to your switch. Then check if you look in the direction of the interrupter, you have a scalar products between the direction vector and that of the switch.

In A AppState, you execute the code that performs these searches, when conditions are met, it is from the AppState that you call a method of your Controller attached to your door.
Then your Controler will move your door, play a sound …

Another possibility, without the use of Jbullet, is to first test the distance between your switch. If you are close, then you test if you look at the switch. If both conditions are met, then you call a method of your door controller.


With JBullet, for collision detection, you implement the PhysicsCollisionListener interface.

You add the listener:

bulletAppState.getPhysicsSpace () addCollisionListener (this).

And then you implement the method:

public void collision (PhysicsCollisionEvent event)

Each time a collision is produced, you will be able to determine which objects come into contact with each other.

Thanks for the prompt reply. I am more than happy to go over more tutorials. I have also been scouring the internet, but I haven’t come up with any results remotely similar to what I’m looking to do. I know this is a bit advanced for my current ability and don’t necessarily need to incorporate this at the moment, but once I think of and add one thing, I have a drive to just go crazy with it. I have been able to fix a lot of this issues that I’ve come across already, but this is one thing that is a bit perplexing to me. I will certainly look into the appstates and controllers, thank you.

Ah, @thoced, I see. Under the collision event, I’ll have the various if statements for what can be collided with and what should happen when they are. As for now, my plan was to simply have a button move a node from one point to another with an attached child. I can add the collision listener to check for what the ray is colliding with, then use the collision result to make the collision method/collision event which will contain the code to tell the node, and its attached child to move along …vector to …point at …speed. I think I’m understanding you correctly. Thank you for the assistance.

If you don’t want to roll your own picking, the Lemur GUI library supports scene picking as well as GUI picking.

Edit: and note, you don’t have to use the GUI part if you just want picking… but putting a button on the screen is really easy with the GUI part if you decide to try it.

1 Like

@pspeed Thanks for the great info! I was looking at Lemur and Tonegodgui a little. These, and the GUI stuff kept coming up when doing research into how to fix this issue. I wasn’t looking for GUI stuff, so I looked on. I want to start putting together some nice libraries and addons for JME. I’m still brand new at this, so I need to spend more time mastering the tutorials and what not. I’m sure I’ll want to add buttons and stuff to the GUI sometime, so this will certainly come in handy. Thank you very much.

Like in the scene picking example, Lemur can be used to interact with any JME Spatials whether you use the GUI part or not. It has some other nice utilities like InputMapper, etc. also… even if you never use the Buttons, Labels, etc. that are provided.

…then those are just a convenient bonus. :slight_smile:

Great to know. Thank you very much. I’ll definitely be getting Lemur. It sounds like it’ll help me a lot in the future.

yep. And alongside that you could get good with textures and materials. You’d be surprised what kind of work comes with GUI, textures, and materials alone. Totally should beat down those kinds of things.

@mack0929 Agreed. I really want to know what I’m doing and do things properly. I was surprised what all it took to do the few things I did for the HUD alone. I have to admit, it’s fun though!

1 Like