Game Development Community

Newb Questions Parallax and updating.

by Delvin Mason · in Torque Game Builder · 08/23/2006 (12:20 pm) · 19 replies

Ok I've finished the Platformer Tutorial and working on my own game but I had a few questions.

-Parallax- I have the background maps update according to the camera but unfortunately I have no way of knowing when the camera reaches its view limits. What is the best way to set up parallax?

-Updating- after awhile the games starts to run funky and clunky. Whats the best way to set up updating? From the tutorial they just use the onSceneUpdate() I've tried using scene time to control it but it doesn't seem to help.

-Collision- It seems to do collision on a new position only. So if the player is falling fast and would collide with the platform next update the player instead collides in the air. Is there anyway around this?

Thanks in advance for any advice.

#1
08/23/2006 (12:29 pm)
For your parallax, are you using a static sprite and varying it's X axis? If so, I recommend you switch to using scrollers. A tutorial on creating parallax can be found in your "documentation\tutorials\feature tutorial\scroller.pdf".

The platformer tutorial is out of date and I haven't read all of it. However, I'm not sure what your updating. For player movement, I'm using the type of movement used in the shooter tutorial. ie, have a function called "updateMovement" and whenever a button is pressed, call that function to move the character.

I haven't encountered your collision problem, as none of my objects fall at a very fast speed. This may be a bug, I'm not sure.
#2
08/23/2006 (1:42 pm)
Im also doing a platformer ;) so we are basically on the same boat, here are some tips.

The platform tutorial is very outdated actually I would recommend to follow the scroller demo tutorial AND then the scroller feature tutorial AND then the tile feature tutorial (which are located in your documentation folder) once you have done those, you can go back to the platform tutorial and disregard what doesnt work any more. (which is a lot)

Also (if you like) recode the state machine code, you can code a lot simpler and easier state machine on your own. I mean for a regular platformer only need 3 states idle, jumping, running. (the tutorial has 12 states or so! )

You can also do an incredible simple camera hack (at least while you get enough knowledge to code a good one) by simply mounting the camera to your character like this in the levelLoaded code

sceneWindow2D.mount($pPlayer, 0, 0, 10, true);

If you see very carefully the background in the scroller you will realize they are using parallax scrolling, which is very easy on tgb. Basically you only need two different tilesets (one without collisions) running at different pan speeds.

I hope you can find those pointers helpful
#3
08/23/2006 (2:17 pm)
Oh I almost forgot, Im not sure but apparentlyphysics didnt existed in the tutorial, so collisions are handled directly, thanks to TGB thats not needed anymore. just set your gravity and a constant force y 100 to the main character and let TGB handle collision and physics.

To know where your screen ends, all you need to do do is set your world limits to the same size of your tilemap (or a little less if you dont want to mess with the mounted camera), then set collision to clamp, if for any reason you want to know when the characters exits (per example if you want to have bottomless pits) set your collision to null and turn on the callbacks, that way you will know if your character exits the screen and where. (for a bottomless pit, kill the character if it hits the "down" world limit, you can also do this with "clamp" but it will look funny because it will stop in mid air)
#4
08/23/2006 (5:41 pm)
Ok I don't think you are getting what my problem is. Its a platformer game with a large map. I don't seem to have the feature tutorial folder (only fish demos). but my understanding is that scrollers just scroll an image and that would work witha static camera but not so much with a moving one. Yes the camera locks with the edge of the map, but that is the problem. I have a second map in the background which I move in conjunction with the camera, however even though the camera stops with the main map, it still thinks its moving with the player and the second map still moves. So the background still moves with the player even though the camera has stopped.

As for not using the update. If I take it out then I don't know how to check and update my state machines (both player and AI enemy). So the player will hit the ground but if you're not check whats happening he will still do a jump animation.
#5
08/24/2006 (1:28 pm)
Im sorry If I dont get the idea, perhaps you could mention a game example that uses a camera such as the one you want?

Anyway the feature tutorials are at : TorqueGameBuilder\documentation\tutorials\Feature Tutorials

They are not projects they are PDF and HTML files. you have to build the project by yourself (just like the platformer tutorial)

I suggested to recode the FSM part since is overcomplicated for a regular platform game(retro style), but you do have to replace that code with your own code, if you just take it off it will stop working.

-Dont get me wrong the tutorial is ok but is pretty outdated, parts of it are useful (per example the code that checks the floor) but most of it is not, you dont need scene updates to check animations because now you can schedule those or use timers to do the same thing, you dont need a bunch of variables to store animations,states, keys etc. since now you can use arrays also you can now store the enemies to a simset and check them all in a single pass, just to mention a few.

Just keep reading the NEW tutorials and the forums follow those instead and you will eventually realize what parts of the OLD tutorials are useful and which ones are not. (thats what Im doing now)

Besides Ive heard theres an update coming to the platform tutorials and other tutorials in the TDN however since it is a wiki if you have a good idea you can even modify it yourself.
#6
08/25/2006 (6:51 am)
Quote:I have a second map in the background which I move in conjunction with the camera, however even though the camera stops with the main map, it still thinks its moving with the player and the second map still moves. So the background still moves with the player even though the camera has stopped.
Same here, but it seems it dont really exist any solution to that, so you'd have to consider this point in your level design (thats I will do, with things to hide the scrolling on the edge of the level).

I wasn't crazy enough to do all the state machine thing, you can pretty much have the same result without all these headache.

We really need an updated tutorial, (though it quickly help us to improve thinking and coding our own way, it is a harsh way to welcome the noobs).
#7
08/25/2006 (8:17 am)
So am I right in thinking that you want it to move when the player moves and want it to stop when the player stands still? If so, couldn't you hold a variable like $playerIsMoving and set it to true when the player holds a direction key. Then, do a check on your scroller such that :
if($playerIsMoving == true){
  $myScroller.setScrollX(50);
}else{
  $myScroller.setScrollX(0);
}

Or is there something I've missed.
#8
08/25/2006 (9:47 am)
Apurva - The reason that wouldn't work is that I would want the scrolling to stop when the camera stops (thus the problem). What I really need is some better control over what the camera is doing when clamped.

Ben - Thanks for the feed back. Not really the answer I wanted but I guess I'll have to figure something out, such as you are suggesting hiding it and not going to the edge. Yeah the state machine I just reworked into what I need.
#9
08/25/2006 (10:51 am)
How are you moving your camera? Are you panning the tileset, physically moving the camera or have you mounted it to the main player?
#10
09/02/2006 (8:59 am)
Sorry for the long delay.

The camera is mounted to a script object which follows the player. As per set up in the platformer tutorial.
#11
09/02/2006 (11:41 am)
Ok I've solved my problem. I used WorldLimit callback with the script object.
Thanks.
#12
09/02/2006 (1:41 pm)
Can I ask you how did you get it to work more precisly ?
#13
09/03/2006 (10:53 am)
Hah... someone needs to make a bounty for that poor outdated platformer tutorial. =P

-Unk
#14
09/05/2006 (12:22 pm)
Ben,

Maybe someday if I'm feeling ambitious I may try to make a new platform tutorial but since I'm not an expert by any means it may not be best if I did. I've learned off of the old tutorial but whose to say thats the bes way

Its not exactly pretty but this is sort of the gist of it.

Camera is a basic ScriptOjbect ($camera in the tutorial). In the tutorial it also creates a t2dSceneObject and stores it in the camera variable (%this.camera or $camera.camera). So in the setLimits function I added a line

%this.camera.setWorldLimit( CLAMP, %parallaxLimit, true);

Where I have set up %parallaxLimit as an area slightly less than the map area.

If unfortunately a mounted object doesn't seem to be bound by its own WorldLimit so unfortunately thats not enough. Then I used the onWorldLimit and used that to set variables as to when I could move the background map layers.
#15
09/05/2006 (12:22 pm)
Wait on second thought there may be an easier way let me get back to you.
#16
09/09/2006 (1:48 am)
If you set the mount force to a positive number a mounted object should clamp at its world limits.
#17
09/10/2006 (1:52 pm)
Thomas- Unfortunately in the Platformer Tutorial the camera just follows the player, it's not actually mounted. This is both good and bad in the fact that you can't handle it that way but you can do some control of the camera.

The easier solution than what I did before is just when you call the SetLimit Function the camera, store its area and then when you go to update your parallax just check to make sure the player is in the bounds.
#18
09/10/2006 (2:51 pm)
We're talking about the old platformer tutorial, not the new mini one, right? I believe it's mounted to a scene object that follows the player, so you can just set the mount force of the camera on the scene object.

Yea, after reading back through it, I realize there is even a function to change the mount force of the camera to the scene object called "setTrackingForce". Unfortunately, I just realized that t2dSceneWindow doesn't have world limits. What you could do is set up world limits on the scene object and then when you update the player set its world limits to the camera area so he can never escape the screen.

And as for getting an image to stay in the background and only scroll relative to the camera, you can just mount the scroller to the camera scene object, or base its scrolling on the movement of the camera rather than the player.
#19
05/17/2009 (12:15 am)
I did something else.

1. Create a tile layer
2. Create a class (say "starFieldScrollClass" - actually create a .cs file to store this under gameScripts) and add that name to the Script "class" property for the layer.

Here's the code...

in game.cs ...
exec("./starFieldScrollClass.cs");
   
   $Camera = $player;  // to keep it in context

..in starFieldScrollClass.cs (the new class)
function starfieldScrollClass::onLevelLoaded(%this, %scenegraph)
{
   %this.enableUpdateCallback();
}

function starfieldScrollClass::onUpdate(%this)
{
   %camPosX = $player.getPositionX();
   %camPosY = $player.getPositionY();
   // the 150 offsets the centre of the tilemap back to where it was
   // you will need to tweak this to match your set up.
   // the denominator (3) is personal preference.  3 means it scrolls
   // 3 times slower than the player walks
   %this.setPosition(150+%camPosX/3,150+%camPosY/3);
}