Moving mounted objects
by Tom Spilman · in Torque Game Builder · 06/13/2005 (11:35 pm) · 14 replies
First off let me say I'm a T2D newbie.
I have a few different things in my scene.
- Some sprites that are supposed to never move (hud stuff).
- A background that scrolls based on mouse movements.
- A bunch of little objects that scroll along with the background as well as move on their own.
The end effect is that it looks like the camera is moving around in a dynamic scene.
I tried three different methods to achieve this:
1. Use setCurrentCameraPosition to move the camera around. Move the little objects around using setlinearvelocity. This works except my hud moves as well... i need the hud elements static.
2. Use a scroller background and mount the little objects on to it. I expected that the mounted objects would scroll along with the background, but it seems they do not. So i would still have to manage the movement of the little objects on their own.
3. Use a normal static object for the background and move it. This moves the little objects i have mounted to it, but setlinearvelocity doesn't work. I expected setlinearvelocity in a mounted object to work relative to the object it's mounted to. So again... i would have to manage the velocity of the little objects thru schedule calls.
Is there something else obvious i'm missing? Ideally i didn't want to have to use GUI elements for my hud... which if i did i could use method 1. I guess if there isn't another good option in t2d i have no choice.
Oh another thought... can i have two fxSceneWindow2D... one over the other?
I have a few different things in my scene.
- Some sprites that are supposed to never move (hud stuff).
- A background that scrolls based on mouse movements.
- A bunch of little objects that scroll along with the background as well as move on their own.
The end effect is that it looks like the camera is moving around in a dynamic scene.
I tried three different methods to achieve this:
1. Use setCurrentCameraPosition to move the camera around. Move the little objects around using setlinearvelocity. This works except my hud moves as well... i need the hud elements static.
2. Use a scroller background and mount the little objects on to it. I expected that the mounted objects would scroll along with the background, but it seems they do not. So i would still have to manage the movement of the little objects on their own.
3. Use a normal static object for the background and move it. This moves the little objects i have mounted to it, but setlinearvelocity doesn't work. I expected setlinearvelocity in a mounted object to work relative to the object it's mounted to. So again... i would have to manage the velocity of the little objects thru schedule calls.
Is there something else obvious i'm missing? Ideally i didn't want to have to use GUI elements for my hud... which if i did i could use method 1. I guess if there isn't another good option in t2d i have no choice.
Oh another thought... can i have two fxSceneWindow2D... one over the other?
About the author
Tom is a programmer and co-owner of Sickhead Games, LLC.
#2
Which gave me the error:
06/14/2005 (1:24 am)
Interesting. How do i mount a sprite to the camera? From the default T2D mod i tried this:$hud_element.mount( sceneWindow2D, "0 0", 0 );
Which gave me the error:
fxSceneObject2D::mount - Couldn't find/Invalid object ''.
#3
I think the best thing might be to render a new scenegraph over the top and place your HUD graphics on that.
Hit F10 to bring up the GUI editor, and a new FXSceneGraph2D control and call it hudSceneGraph, give it the same extents as your current scenegraph. Save the GUI, and add the code to define the scenegraph. For instance:
Then just create your sprites on the appropriate scene and your set.
You can now wobble the default scenegraph about and your hud scene should stay put.
06/14/2005 (2:32 am)
It seems you can do it the other way around:sceneWindow2D.mount($hud_element);But I suspect that would get a bit tedious.
I think the best thing might be to render a new scenegraph over the top and place your HUD graphics on that.
Hit F10 to bring up the GUI editor, and a new FXSceneGraph2D control and call it hudSceneGraph, give it the same extents as your current scenegraph. Save the GUI, and add the code to define the scenegraph. For instance:
new fxSceneGraph2D(hudSceneGraph); hudWindow2D.setSceneGraph( hudSceneGraph ); hudWindow2D.setCurrentCameraPosition( "0 0 100 75" );
Then just create your sprites on the appropriate scene and your set.
You can now wobble the default scenegraph about and your hud scene should stay put.
#4
06/14/2005 (9:03 am)
A good suggestion by Philip... in my T2D Gui System I use a seperate Scenegraph and Scenewindow for the GUI stuff, so it all stays at the same position without interfering (or getting effected) by the rest of what goes on.
#5
The option of having two scene graphs looks better to me and seems to work pretty good. One trick i had to do is capturing the mouse functions (onMouseDown, onMouseMove) with the hud window to control actions on the game window below it.
Thanks for the help.
06/14/2005 (10:43 am)
@Philip - Seems to me that "sceneWindow2D.mount($hud_element)" isn't what i'm looking for (it attaches the movement of the camera an object). The option of having two scene graphs looks better to me and seems to work pretty good. One trick i had to do is capturing the mouse functions (onMouseDown, onMouseMove) with the hud window to control actions on the game window below it.
Thanks for the help.
#6
I ended up attaching commands to fxSceneWindow2D (for mouse over checks, etc) that way I can seperate controls specific to windows (with little true/false toggles to prevent or begin checks for gui objects).
06/14/2005 (11:30 am)
The two scenegraphs and two windows work quite well... If I keep all gui objects in the gui one then I can still resize the game set without effecting the guis (unless I want to). Also since Torque 2D runs off of a logical coordinate system it means auto resizing when your game gets resized :)I ended up attaching commands to fxSceneWindow2D (for mouse over checks, etc) that way I can seperate controls specific to windows (with little true/false toggles to prevent or begin checks for gui objects).
#7
I'm a newbie using tgb1.3 and I have exactly the same problem. seems that create 2 different scenegraphs and 2 windows is the best solution for it, but i can't make it works
the process should be:
1- create a new scenegraph
2- create a new window and attach it to the scenegraph
3- load all my hud elements on the scenegraph
i don't know if all the hud elements must be in another level or in the same of the main game, and if i can do that from inside the builder, or exclusively by code (i mean create the objects and load into the new scenegraph)
could you write some simple line of script, to point me in the right direction?
thanks a lot
02/13/2007 (5:24 pm)
Hi everyone, I'm a newbie using tgb1.3 and I have exactly the same problem. seems that create 2 different scenegraphs and 2 windows is the best solution for it, but i can't make it works
the process should be:
1- create a new scenegraph
2- create a new window and attach it to the scenegraph
3- load all my hud elements on the scenegraph
i don't know if all the hud elements must be in another level or in the same of the main game, and if i can do that from inside the builder, or exclusively by code (i mean create the objects and load into the new scenegraph)
could you write some simple line of script, to point me in the right direction?
thanks a lot
#8
Here is an example of how to test the concept in the console, to give you an idea of what needs to happen. Assume that you press enter in the console after typing each line.
Make sure the second level that you load does not have a solid background, otherwise you obviously won't be able to see the level that's loaded in the main scene window beneath it.
02/13/2007 (6:51 pm)
Your HUD elements should be in a seperate level file. You don't have to create a new scenegraph if you're loading a level because a level file is a scenegraph. You just need to add a second scene window to the canvas and load the level to it. You can add the second scene window to mainScreen.gui or create a duplicate of mainScreen.gui and name it something else. Make sure to set the name of your second scene window to something other than SceneWindow2D so you can distinguish between them.Here is an example of how to test the concept in the console, to give you an idea of what needs to happen. Assume that you press enter in the console after typing each line.
new t2dSceneGraph(Sassafrass);
Canvas.pushDialog(Sassafrass);
Sassafrass.loadLevel("path\to\some\other\level.t2d");Make sure the second level that you load does not have a solid background, otherwise you obviously won't be able to see the level that's loaded in the main scene window beneath it.
#9
02/14/2007 (1:53 pm)
Thank you very much for your quick answer Thomas, now I can sleep again :-)
#10
02/14/2007 (2:35 pm)
Thank you very much for your quick answer Thomas, now I can sleep again :-)
#11
02/14/2007 (5:19 pm)
Heh -- did Tom Spillman start this thread off by saying "I'm a T2D newbie" ? :) *cleans his glasses*
#12
02/15/2007 (9:51 am)
You do realize a 2 year old thread just got revived :)
#13
02/15/2007 (4:51 pm)
*Summons the mighty Thread Killer Beast*
#14
So currently, I have two sceneWindows:
sceneWindow2D - the scenewindow I have loaded my level into
hudWindow - the scenewindow for my HUD elements
I handle mouse events with sceneWindow2D. However, since I've added hudWindow, sceneWindow2D no longer receives mouse events. Is there anything else I should be doing?
07/26/2008 (6:31 am)
I followed the advice listed here, and was able to make a new scenewindow for my HUD.So currently, I have two sceneWindows:
sceneWindow2D - the scenewindow I have loaded my level into
hudWindow - the scenewindow for my HUD elements
I handle mouse events with sceneWindow2D. However, since I've added hudWindow, sceneWindow2D no longer receives mouse events. Is there anything else I should be doing?
Torque Owner Philip Mansfield
Default Studio Name
For the background, use a scrolling tile map as you hae already suggested. Set leyer to a high number so it appears behind everything else.
For your little sprites, move them independently, but use the background movement as a modifier. So if your sprites are moving left, and your background is scrolling up, use the negative of the map movement as a modifier to the sprites movement. This should cause the sprites to appear to move down the map as it scrolls up.
If you want to do the move camera option, then you can mount your HUD sprites to the camera instead, and this will cause them to stay in the right place.