Best way to handle this basic Puzzle game feature?
by Matt Troup · in Torque Game Builder · 09/23/2005 (10:24 pm) · 5 replies
In most puzzle games such as Tetris, Puyo Puyo, Columns, etc. when you press left or right your current controlled object moves a whole unit in the direction you press. In my puzzle game, I have a crane hand that drops objects from the sky. This crane hand moves along an X axis line, and does not move in set increments left or right. When moving left or right the crane hand moves continuously as you hold the directional button as a character in a platformer or shooter would. Since the crane hand can be in an infinite amount of positions I am unsure how I should make sure the objects it releases fall in perfect columns.
Currently, my idea is:
Get the X position of the crane hand
Ask if that X position is within a certain range : if (Xpos > 0) && (Xpos <= 10)... if (Xpos > 10) && (Xpos <=20)... etc
Drop the object to the coorelating column within that range using the moveTo function in King Tut's tutorial.
Update the next position in that column something should be placed at (so the next object falls on top of it)
It works, but maybe there's a better way. Something just doesn't seem natural about asking for the X position range. I feel like I'm ignoring something more simple.
Currently, my idea is:
Get the X position of the crane hand
Ask if that X position is within a certain range : if (Xpos > 0) && (Xpos <= 10)... if (Xpos > 10) && (Xpos <=20)... etc
Drop the object to the coorelating column within that range using the moveTo function in King Tut's tutorial.
Update the next position in that column something should be placed at (so the next object falls on top of it)
It works, but maybe there's a better way. Something just doesn't seem natural about asking for the X position range. I feel like I'm ignoring something more simple.
About the author
#2
Even though I hadn't slept in a couple days when I originally read your post, I still can't claim to understand it. Here's what I did, which is similar... maybe you just made a slip of the tongue when you typed it up:
Also, for those interested, in the last line I'm not using setPosition. I'm actually using moveTo so the object doesn't jump directly to that position. Learn how to create a moveTo function in the following resource:
http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=7576
And yes, you can shorten the code like LoTekK did, but I make it a habit to write out code longhand the first time through.
PostScript Edit: If you use the code block above in a function, %ifCode will go out of scope when the function completes so you don't have to worry about it. It wouldn't hurt to set it back to zero, though.
09/25/2005 (10:23 pm)
Thanks, LoTekK. Even though I hadn't slept in a couple days when I originally read your post, I still can't claim to understand it. Here's what I did, which is similar... maybe you just made a slip of the tongue when you typed it up:
%currentPos = getWord($InsertObjectNameHere.getPosition(), 0);
if (%currentPos < 0)
{
%currentPos = %currentPos * -1; //makes %currentPos positive so
//the math below will work properly
%ifCode = 1;
}
%columnWidth = //insert your value here
%x = %currentPos + %columnWidth;
//%x %y and %z have nothing to do with x y and z coordinates.
//My apologies for my poor choice of variable names.
%y = mfloor( %x / %columnWidth );
%z = %y * %columnWidth;
%Xpos = %z - ( %columnWidth / 2 );
if (%ifCode == 1)
{
%Xpos = %Xpos * -1; //sets Xpos to a negative if %currentPos was negative
}
$YourObjectHere.setPosition( %Xpos SPC %insertYvalueHere );Also, for those interested, in the last line I'm not using setPosition. I'm actually using moveTo so the object doesn't jump directly to that position. Learn how to create a moveTo function in the following resource:
http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=7576
And yes, you can shorten the code like LoTekK did, but I make it a habit to write out code longhand the first time through.
PostScript Edit: If you use the code block above in a function, %ifCode will go out of scope when the function completes so you don't have to worry about it. It wouldn't hurt to set it back to zero, though.
#3
09/26/2005 (6:54 am)
Sorry, I didn't think to expand the code terribly. I think I was running on very little sleep at the time so I didnt really have the mental capacity to work out the proper code. What's there was basically a jump off point. Sorry 'bout that.
#4
09/26/2005 (8:03 am)
All your fault *wink*. Nah, you're crazy, really. I wouldn't have understood the math without you. Thanks.
#5
09/26/2005 (7:18 pm)
In that case, glad to be of help. :)
Torque Owner Teck Lee Tan
your target X axis position for the puzzle piece would simply relate to:
Of course, you'll have to tailor this to your own setup, depending on how you've setup your crane, playing field, etc. But the basic idea is to simply divide the field width equally.