determining player worldLimits with movingCamera
by rennie moffat · in Torque Game Builder · 09/23/2009 (12:07 pm) · 17 replies
function playerClass::onLevelLoaded(%this, %scenegraph)
{
$PlayerShip = %this;
%this.getCurrentCameraPosition(%this.getWord(CPX, 0) SPC (%this.getWord(CPY, 1));
%this.getCurrentCameraArea(%this.getWord(CA1X, 0) SPC (%this.getWord(CA1Y, 1), %this.getWord(CA2X, 3), %this.getWord(CA2Y, 4));
}
function playerClass::setWorldLimit(%mode, %CA1X, %CA2Y, %CA2X, %CA1Y, [%callback = true])
{
%this.left = (%this.CPX + %this.CA1X);
%this.right = (%this.CPX + %this.CA2X);
%this.top = (%this.CPY + %this.CA1Y);
%this.bottom = (%this.CPY + %this.CA2Y);
}
function playerClass::onWorldLimit(%this, %limitMode, %limit)
{
if (%this.left < $PlayerShip.getPositionX())
{
$PlayerShip.setLinearVelocityX(0);
return;
}
if (%this.right < $PlayerShip.getPositionX())
{
$PlayerShip.setLinearVelocityX(0);
return;
}
if (%this.bottom < $PlayerShip.getPositionY())
{
$PlayerShip.setLinearVelocityY(0);
return;
}
if (%this.top < $PlayerShip.getPositionY())
{
$PlayerShip.setLinearVelocityY(0);
return;
}
}I know the logic of what I need to do is right, I am just curious, of how I actually plug it in. I have tried various looks of it but nothing that has worked yet. I am not sure about where I need to plug things in. For instance, onLevelLoaded. Does it only call once, "on Level Loaded"? Also some of the other things like setting the variables left, right, top, bottom. I am unsure if the class is right, and if my complier has any clue of what I am saying to it. Please if you can give a guy a hand. Am I moving in the right direction with this? Am I anywhere near a solution?
Thanks
Ren
About the author
My thanks to Garage Games and the Garage Games Community combined with owned determination I got one game up, Temple Racer and I am looking to build more interesting, fun games for the mass market of the iOS app store.
#2
09/28/2009 (1:02 pm)
...edit, please see first post.
#3
09/28/2009 (11:10 pm)
.L[[P
#4
other than that tho, please look at it and give some tips if you can.
thanks.
my getWord is because i thought i might need to call that in order to get the proper cameraPosition vector.
Where I think i am most lost is which methods and call backs can go where. Am I safe to say a callback is a parent, or more akin to a function while a method would be the functions innards?
for instance,
onWorldLimit is a t2dSceneObject Callback
while,
setWorldLimit is a t2dSceneObject Spatial Method
09/30/2009 (10:24 pm)
this is pretty hacked code. If it is so off it makes you want to puke please tell me, other than that tho, please look at it and give some tips if you can.
thanks.
function playerClass::onWorldLimit(%this, %clamp, %limit)
{
$PlayerShip.getPositionX()
if ($PlayerShip.positionX() < %this.sceneWindow2D.getCurrentCameraPosition.getword(0))
{
%limit;
}
if ($PlayerShip.positionX() > %this.sceneWindow2D.getCurrentCameraPosition.getword(2))
{
%limit;
}
}my getWord is because i thought i might need to call that in order to get the proper cameraPosition vector.
Where I think i am most lost is which methods and call backs can go where. Am I safe to say a callback is a parent, or more akin to a function while a method would be the functions innards?
for instance,
onWorldLimit is a t2dSceneObject Callback
while,
setWorldLimit is a t2dSceneObject Spatial Method
#5
I think you want to keep your ship within the world limit, right?
You can do this very easy in the TGB editor.
Select your ship sprite and press the edit world limit button (the one 5th from the left)
The scene is zooming out and you should see a marked box. This box is the sprite's world limit. Adjust it in desired size and position.
Press the selection tool button when you are finished.
Now, with your sprite still selected, press the edit button and scroll down to World Limits.
Set Limit Mode to CLAMP and now your ship should be inside your world limits.
I would really recommend you to go through the basic tutorials (I know they look boring, but it gives you a great understanding of how basic things work).
10/01/2009 (5:19 pm)
Again, I'm a bit confused of what you are trying to do here.I think you want to keep your ship within the world limit, right?
You can do this very easy in the TGB editor.
Select your ship sprite and press the edit world limit button (the one 5th from the left)
The scene is zooming out and you should see a marked box. This box is the sprite's world limit. Adjust it in desired size and position.
Press the selection tool button when you are finished.
Now, with your sprite still selected, press the edit button and scroll down to World Limits.
Set Limit Mode to CLAMP and now your ship should be inside your world limits.
I would really recommend you to go through the basic tutorials (I know they look boring, but it gives you a great understanding of how basic things work).
#6
Sorry...
10/01/2009 (5:53 pm)
After some more reading I think I understand what you want. This solution will not solve what you want.Sorry...
#7
There's a few things odd in your code, the getCurrentCamera type calls are called from a playerClass (as %this is a playerClass in onLevelLoaded). It should be called from your scene window.
Also your variables CPX etc are missing the '$' in onLevelLoaded.
If your camera is moving these values you're calculating won't work by the way as they're called from the point when the level loads up. You could use schedule() to recalculate the values n times per sec.
I still haven't looked into what getCurrentCameraArea() returns (guessing two sets of world coords) so your math might be a bit off too.
If this is the $CPX distance:
-----------------------
And this the $CA1X and $CA2X distance:
----------------------------
-------------------------------------
This will be how much off screen in X the player is:
-----
So your player is in the camera view as long as:
$CPX - $CA1X is bigger than 0.
$CPX - $CA2X is smaller than 0.
And the same goes for the Y coords of course.
I'm having to run here so have only looked at your code in the first post. And again, I might've missed something as I'm not the hottest coder on the block.
Good luck and hope I've helped!
10/03/2009 (2:45 pm)
Hey Rennie, I replied to your other thread.. you seemed to be getting closer judging from your last post, is this still a problem?There's a few things odd in your code, the getCurrentCamera type calls are called from a playerClass (as %this is a playerClass in onLevelLoaded). It should be called from your scene window.
Also your variables CPX etc are missing the '$' in onLevelLoaded.
If your camera is moving these values you're calculating won't work by the way as they're called from the point when the level loads up. You could use schedule() to recalculate the values n times per sec.
I still haven't looked into what getCurrentCameraArea() returns (guessing two sets of world coords) so your math might be a bit off too.
If this is the $CPX distance:
-----------------------
And this the $CA1X and $CA2X distance:
----------------------------
-------------------------------------
This will be how much off screen in X the player is:
-----
So your player is in the camera view as long as:
$CPX - $CA1X is bigger than 0.
$CPX - $CA2X is smaller than 0.
And the same goes for the Y coords of course.
I'm having to run here so have only looked at your code in the first post. And again, I might've missed something as I'm not the hottest coder on the block.
Good luck and hope I've helped!
#8
re. playerClass call when it should be caled froma sceneWindow2D. I understadn taht but what I was cnfused on is calling from the same .cs file. This is fine right? I could essentially have an infinite number of classes called from the same .cs file? If so, is this a good idea if multiple items need to be global? I ask this seeing that I seem to be thinking I will need many bits (of code) talking to other bits.
Re. CPX mising from onLevelLoaded
Is it possible to declare more than pne global in the onLevelLoaded, for instance...
onLevelLoaded
{
$player = %this
$enemy = %this
$grandma = %this
}
the reason I ask is because I would have multiple globals, but I think there can only be one "%this". If you could, please help me clarify.
I think my math is right, it is just getting the codes called. Yes I have looked at getCurrentCamera, and this is still an issue for me, tho I have other things to keep me busy and other ideas to set up a "moving world limit". Perhaps moving objects that will act as walls placed just out side the view. Just a thought, and yes you have been helpful.
Thanks!
10/03/2009 (3:15 pm)
thanks Backman,re. playerClass call when it should be caled froma sceneWindow2D. I understadn taht but what I was cnfused on is calling from the same .cs file. This is fine right? I could essentially have an infinite number of classes called from the same .cs file? If so, is this a good idea if multiple items need to be global? I ask this seeing that I seem to be thinking I will need many bits (of code) talking to other bits.
Re. CPX mising from onLevelLoaded
Is it possible to declare more than pne global in the onLevelLoaded, for instance...
onLevelLoaded
{
$player = %this
$enemy = %this
$grandma = %this
}
the reason I ask is because I would have multiple globals, but I think there can only be one "%this". If you could, please help me clarify.
I think my math is right, it is just getting the codes called. Yes I have looked at getCurrentCamera, and this is still an issue for me, tho I have other things to keep me busy and other ideas to set up a "moving world limit". Perhaps moving objects that will act as walls placed just out side the view. Just a thought, and yes you have been helpful.
Thanks!
#9
%this is refering to the current object of the current class, so in function playerClass::amazingStuffHappens() %this will be the current player of this playerClass. What you did there was saying that $player, $enemy and $grandma are all the same thing, but your question wasn't really about that so could be just an example.. and to answer that question; yes, you can set as many globals as you wish inside onLevelLoaded.
Have you checked out some of the game tutorials? They give a lot of good insight into how these things work. :)
10/04/2009 (5:15 pm)
The .cs files are really just another way to organize your code so fill them up with whatever you feel like. :)%this is refering to the current object of the current class, so in function playerClass::amazingStuffHappens() %this will be the current player of this playerClass. What you did there was saying that $player, $enemy and $grandma are all the same thing, but your question wasn't really about that so could be just an example.. and to answer that question; yes, you can set as many globals as you wish inside onLevelLoaded.
Have you checked out some of the game tutorials? They give a lot of good insight into how these things work. :)
#10
So since I said
onLevelLoaded
{
$player = %this
$enemy = %this
$grandma = %this
}
since all of these are the same (%this), how do I differentiate them?
$player = %this.player
$enemy = %this.enemy
$grandma = %this.grandma
would do right?
Also you said I can have as many globals inside onLevelLoaded, I am thinking this would be the same for all functions?
10/04/2009 (5:23 pm)
Yes definitely I have read and am reading the tutorials and the documentation too. I just need to/am learning the nuances. So since I said
onLevelLoaded
{
$player = %this
$enemy = %this
$grandma = %this
}
since all of these are the same (%this), how do I differentiate them?
$player = %this.player
$enemy = %this.enemy
$grandma = %this.grandma
would do right?
Also you said I can have as many globals inside onLevelLoaded, I am thinking this would be the same for all functions?
#11
10/05/2009 (12:36 am)
Just name the objects in TGB, and then there'll be no need for any of this.
#12
10/05/2009 (1:26 am)
ok as such then naming an object, giving it a global serves no real purpose when I can simply define it in the editor?
#13
I know these are getting tougher, but at the same time they are getting easier if that makes any sense.
10/06/2009 (2:35 pm)
Ok, so I have just completely modified the code on what I HAD been doing. I have taken the leap and gone with a behavior, It still is off, as it will not run, but I think I am getting closer any suggestions help would be great. I know these are getting tougher, but at the same time they are getting easier if that makes any sense.
if (!isObject(Spacer3000WorldLimits))
{
%template = new BehaviorTemplate(Spacer3000WorldLimits);
%template.friendlyName = "Spacer3000 World Limits";
%template.behaviorType = "World Limit Behavior: Player";
%template.description = "Spacer3000WorldLimits";
}
function Spacer3000WorldLimits::onBehaviorAdd(%this)
{
%this.owner.enableUpdateCallback();
sceneWindow2D.startCameraMove();
%this.schedule(100, "setWorldLimit");
}
function Spacer3000::onUpdate(%this)
{
%this.getWorldLImits();
%this.player.getPositionX();
%this.player.getPositionY();
}
function Spacer3000WorldLimits::setWorldLimit(%mode, %minX, %minY, %maxX, %maxY, [%callback = false])
{
%mode = clamp;
%minX = getWord(sceneWindow2D, 0);
%maxX = getWord(sceneWindow2D, 2);
%minY = getWord(sceneWindow2D, 3);
%maxY = getWord(sceneWindow2D, 1);
}
}
function Spacer3000WorldLimit::onWorldLimit(%this, %limitMode, %limit)
{
%this.player.getPosition();
if (%this.player.positionX() = %this.minX()
{
%this.owner.clamp;
}
if (%this.player.positionX() = %this.maxX()
{
%this.clamp;
}
if (%this.player.positionY() = %this.minY()
{
%this.clamp;
}
if (%this.player.positionY() = %this.maxY()
{
%this.clamp;
}
}
#14
I've posted my comments in your other thread.
10/06/2009 (2:38 pm)
You really shouldn't double post.I've posted my comments in your other thread.
#15
Ps. Im thinking life might be easier with Torsion.
10/06/2009 (3:02 pm)
Ok so i have run through one more edit and can not get it working yet tho i know I am closer. Please breeze through it if you have a minute a point an error for a brother.Ps. Im thinking life might be easier with Torsion.
if (!isObject(Spacer3000WorldLimits))
{
%template = new BehaviorTemplate(Spacer3000WorldLimits);
%template.friendlyName = "Spacer3000 World Limits";
%template.behaviorType = "World Limit Behavior: Player";
%template.description = "Spacer3000WorldLimits";
}
function Spacer3000WorldLimits::onBehaviorAdd(%this)
{
%this.owner.enableUpdateCallback();
}
function Spacer3000::onUpdate(%this)
{
%this.player.getWorldLImits();
%this.getCurrentCameraPosition();
%this.player.getPositionX();
%this.player.getPositionY();
%this.sceneWindow2D.setWorldLimits(%mode, %minX, %minY, %maxX, %maxY, [%callback = true]);
}
function Spacer3000WorldLimits::setWorldLimit(%mode, %minX, %minY, %maxX, %maxY, [%callback = true])
{
%mode = clamp;
%minX = getWord(sceneWindow2D, 0);
%maxX = getWord(sceneWindow2D, 2);
%minY = getWord(sceneWindow2D, 3);
%maxY = getWord(sceneWindow2D, 1);
}
}
function Spacer3000WorldLimit::onWorldLimit(%this, %limitMode, %limit)
{
%limitMode = clamp;
if (%this.player.getPositionX() == %this.minX.getCurrentCameraPosition(getWord(0))
{ %limit = true; }
if (%this.player.getPositionX() == %this.maxX.getCurrentCameraPosition(getWord(2))
{ %limit = true; }
if (%this.player.getPositionX() == %this.minY.getCurrentCameraPosition(getWord(1))
{ %limit = true; }
if (%this.player.getPosition() == %this.maxY.getCurrentCameraPosition(getWord(3))
{ %limit = true; }
}
#16
10/06/2009 (3:04 pm)
Yes the threads were related but slightly modified, and me in my excitement posted in the other thread. my apologies. if you do have a minute, please look over this one, I am having trouble with some of it. Its is just not 100 percent tho I know I am getting there.
#17
Ren
http://www.garagegames.com/community/forums/viewthread/103187
10/06/2009 (5:24 pm)
This thread links to this one. the more recent teachings are found here. Please use it for more knowledge.Ren
http://www.garagegames.com/community/forums/viewthread/103187
Torque Owner rennie moffat
Renman3000