"Grid" based movement..
by Jason LaChapelle · in Torque X 2D · 08/22/2009 (5:07 pm) · 1 replies
Hi All,
I'm running into a slight issue here trying to implement a "grid" based movement system. I put "grid" in quotes because it's not really a grid.. What I'm trying to do is move the player one block at a time in a sides-crolling type environment. The ultimate goal is to have it so when the player pressed the right arrow, he moves one block the right. If he holds down the right arrow, he moves rapidly, one block at a time.
I played around a little with the tutorial in the TorqueX 2D documentation (SciFi strategy game, or whatever it's called) to get an idea of how to move one block at a time. It was extremely helpful and got me started in the right direction. Here's my slightly modified version of that code:
Here's the issues I'm running into -
#1) When I go ahead and press a movement button (left or right) my character get's reset to the middle point of the screen. This makes sense, considering the first value for _iAnalogX is going to be either NaN or 0. I would expect the player to be centered. But, just because I expect it, doesn't mean I want it to work that way :)
#2) The starting point for my character is -32.500. If I set _iAnalogX to that as the initial value, I will no longer get recentered when I press a movement key. However, if I go ahead and move the character I get moved only .5 to put me at an even -33 or -32, depending on which way I'm moving. This is happening, I'm pretty sure, because I'm not rounding to any decimal places, so the function will always move me to whole numbers. This will not work for me, because the starting point of the player is -32.500 (which would be the center of a tile). The next move the player makes should be either -37.500, or -28.500.
#3) If I go ahead and try to round to a decimal place (1, 2, 3, even 4) then I'm no longer moving by blocks but instead am back to the smooth movement you see with the default movement component.
Any ideas on how I can get this to work as I'd like it to? Any help would be greatly appreciated. And I apologize if this doesn't make the most sense in the world - I'm not really the best at putting stuff into words, lol.
Thanks in advance!
I'm running into a slight issue here trying to implement a "grid" based movement system. I put "grid" in quotes because it's not really a grid.. What I'm trying to do is move the player one block at a time in a sides-crolling type environment. The ultimate goal is to have it so when the player pressed the right arrow, he moves one block the right. If he holds down the right arrow, he moves rapidly, one block at a time.
I played around a little with the tutorial in the TorqueX 2D documentation (SciFi strategy game, or whatever it's called) to get an idea of how to move one block at a time. It was extremely helpful and got me started in the right direction. Here's my slightly modified version of that code:
float _iAnalogX; float _iStepX = 5.0f; _iAnalogX += move.Sticks[0].X * 35.0f * elapsed; float iNewX = (float)Math.Round(_iAnalogX / _iStepX, 0) * _iStepX; _sceneObject.Position = new Vector2(iNewX, _sceneObject.Position.Y);
Here's the issues I'm running into -
#1) When I go ahead and press a movement button (left or right) my character get's reset to the middle point of the screen. This makes sense, considering the first value for _iAnalogX is going to be either NaN or 0. I would expect the player to be centered. But, just because I expect it, doesn't mean I want it to work that way :)
#2) The starting point for my character is -32.500. If I set _iAnalogX to that as the initial value, I will no longer get recentered when I press a movement key. However, if I go ahead and move the character I get moved only .5 to put me at an even -33 or -32, depending on which way I'm moving. This is happening, I'm pretty sure, because I'm not rounding to any decimal places, so the function will always move me to whole numbers. This will not work for me, because the starting point of the player is -32.500 (which would be the center of a tile). The next move the player makes should be either -37.500, or -28.500.
#3) If I go ahead and try to round to a decimal place (1, 2, 3, even 4) then I'm no longer moving by blocks but instead am back to the smooth movement you see with the default movement component.
Any ideas on how I can get this to work as I'd like it to? Any help would be greatly appreciated. And I apologize if this doesn't make the most sense in the world - I'm not really the best at putting stuff into words, lol.
Thanks in advance!
Torque Owner Duncan Colvin
(where _iTotalMovedAnalogX starts at a value of 0 when the level starts)