ENET
|
  |
| Joined: 01 Jan 2010 |
| Total Posts: 4820 |
|
|
| 20 Nov 2011 07:40 PM |
Here lately I was pondering how I should make any gui move at any reasonable speed without making it look like it was jumping. This meant for loops were out. I got to thinking about loading bars and how they work. I thought what if I could get the percentile of time passed between a frame of a second. Then whatever time has passed I can just take a fraction of the speed per second and get how much it would have moved in that time. For this demonstration I used the function below. Where obj is a gui object and pos2 is a UDim2 and spd is a int value. I found that the given code smoothly moves a gui around using a while loop until it gets to its destination. Please comment and tell me how I could make it more efficient.
--Note getDistanceAndDirection simply returns the unit vector for dir and the magnitude for distance.
function move(obj, pos2, spd) spd = spd or 16; local dist,dir = getDistanceAndDirection(obj.Position, pos2); local distTraveled = 0; local time = tick(); local cTime = 0; local orig = obj.Position; Spawn(function() while distTraveled < dist do wait(); cTime = tick()-time; time = tick(); distTraveled = distTraveled + (spd*cTime); if(distTraveled > dist)then distTraveled = dist; end obj.Position = UDim2.new(0,orig.X.Offset+(distTraveled*dir.X),0,orig.Y.Offset+(distTraveled*dir.Y)); end end); end |
|
|
| Report Abuse |
|
|
pwnedu46
|
  |
| Joined: 23 May 2009 |
| Total Posts: 7534 |
|
|
| 20 Nov 2011 08:20 PM |
You could just use TweenPosition. You can change the amount of time it takes to move from the starting position to the ending position.
---------- ~ pwendu46 ~ |
|
|
| Report Abuse |
|
|
|
| 20 Nov 2011 08:28 PM |
| gui:TweenPosition(UDim2.new(0,0,0,0)) -- more efficient |
|
|
| Report Abuse |
|
|
ENET
|
  |
| Joined: 01 Jan 2010 |
| Total Posts: 4820 |
|
|
| 20 Nov 2011 08:50 PM |
| Im talking about using my own code. Not something of roblox's that is possible to break at any time. Or malfunction. |
|
|
| Report Abuse |
|
|
pwnedu46
|
  |
| Joined: 23 May 2009 |
| Total Posts: 7534 |
|
|
| 20 Nov 2011 09:00 PM |
" Not something of roblox's that is possible to break at any time."
Even if you did make it, it's still liable to break. Roblox loves to mess with us like that... :\ |
|
|
| Report Abuse |
|
|
blocco
|
  |
| Joined: 14 Aug 2008 |
| Total Posts: 29474 |
|
|
| 20 Nov 2011 09:00 PM |
| If you use it right it won't break. |
|
|
| Report Abuse |
|
|
ENET
|
  |
| Joined: 01 Jan 2010 |
| Total Posts: 4820 |
|
|
| 20 Nov 2011 09:17 PM |
TweeningDoesn't give cool effects like this... This moves obj to pos2 at spd. When its halfway their its size is the biggest. This makes it look like the object is thrown into the air and as it goes to its position it rises then falls. I am using these functions in my gui game btw. It's all top-down. I am thinking you will have boxes trying to get to the castle in the center. To win you have to hold them off.
function launch(obj, pos2, spd, maxHeight, onFinished) spd = spd or 16; maxHeight = maxHeight or 3; local dist,dir = getDistanceAndDirection(obj.Position, pos2); local distTraveled = 0; local time = tick(); local cTime = 0; local orig = obj.Position; local origSize = obj.Size; local half_dist = dist/2; Spawn(function() while distTraveled < dist do wait(); cTime = tick()-time; time = tick(); distTraveled = distTraveled + (spd*cTime); if(distTraveled > dist)then distTraveled = dist; end height = 1+(maxHeight * ( 1 - (distance(distTraveled, half_dist)/half_dist) )); obj.Position = UDim2.new(0,orig.X.Offset+(distTraveled*dir.X),0,orig.Y.Offset+(distTraveled*dir.Y)); obj.Size = UDim2.new(0,origSize.X.Offset*height,0,origSize.Y.Offset*height); end if(onFinished)then onFinished(); end end); end |
|
|
| Report Abuse |
|
|
Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 20 Nov 2011 09:26 PM |
| TweenSize/Position. Just call it a few times. There's also one that you can use to change the size and position in 1 call. There's no way it can go wrong. |
|
|
| Report Abuse |
|
|
ENET
|
  |
| Joined: 01 Jan 2010 |
| Total Posts: 4820 |
|
|
| 20 Nov 2011 09:35 PM |
| Except for the fact that you can't have full control over every aspect of it as you can in mine. And as I showed with the launch function. |
|
|
| Report Abuse |
|
|
Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 20 Nov 2011 09:51 PM |
| I'll trade that option for not wasting hours of time on my game. I'm not THAT much of a perfectionist. |
|
|
| Report Abuse |
|
|
ENET
|
  |
| Joined: 01 Jan 2010 |
| Total Posts: 4820 |
|
|
| 20 Nov 2011 09:54 PM |
| Neither am I, I just want to win that contest. Don't care for the money, just want to win it. |
|
|
| Report Abuse |
|
|
Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 20 Nov 2011 10:02 PM |
| Win the current contest - the official one. spend 1 hour of your time. :) |
|
|
| Report Abuse |
|
|
ENET
|
  |
| Joined: 01 Jan 2010 |
| Total Posts: 4820 |
|
|
| 20 Nov 2011 10:03 PM |
| Not the official one, the one in the scripters category for best gui game. |
|
|
| Report Abuse |
|
|
Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 20 Nov 2011 10:10 PM |
Yeah. You're the only one entering. -______-
You win, |
|
|
| Report Abuse |
|
|
ENET
|
  |
| Joined: 01 Jan 2010 |
| Total Posts: 4820 |
|
|
| 20 Nov 2011 10:19 PM |
| Actually I haven't even entered yet. Nor will I until my project is done. |
|
|
| Report Abuse |
|
|
Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 20 Nov 2011 10:26 PM |
| There's a timelimit you know. You've probably surpassed it. |
|
|
| Report Abuse |
|
|
ENET
|
  |
| Joined: 01 Jan 2010 |
| Total Posts: 4820 |
|
|
| 21 Nov 2011 01:34 AM |
| He extended it to wednesday. |
|
|
| Report Abuse |
|
|
zars15
|
  |
| Joined: 10 Nov 2008 |
| Total Posts: 9999 |
|
|
| 21 Nov 2011 04:16 AM |
| Why u no use tweening!? You can fully control it. Just get your timing right. |
|
|
| Report Abuse |
|
|
|
| 21 Nov 2011 06:22 AM |
You just learnt to use the basic math operators?
awesome. |
|
|
| Report Abuse |
|
|
HotThoth
|
  |
 |
| Joined: 24 Aug 2010 |
| Total Posts: 1176 |
|
|
| 21 Nov 2011 12:47 PM |
It's rather inefficient to have a wait() call and then afterwards poll for the elapsed time. A rather elegant way to make an object travel as smoothly at a particular velocity as possible would be to do something like this (this is what I did for my gui physics engine):
function mainLoop() while notFinishedWithGame do local frameRate = wait() local adjustedVelocity = objectVelocity*frameRate objectPosition = objectPosition + adjustedVelocity
-- and then I actually display the gui object here end end
In this way, you can use the necessary wait() to do your polling for you, and adjust your calculations according to how fast your game is running to keep it moving as smoothly as possible, no matter what the fps would be. |
|
|
| Report Abuse |
|
|
ENET
|
  |
| Joined: 01 Jan 2010 |
| Total Posts: 4820 |
|
|
| 21 Nov 2011 03:13 PM |
@HotThoth The wait accounts for how fast the framerate. It doesn't account for the other functions time to be called. |
|
|
| Report Abuse |
|
|
Oysi
|
  |
| Joined: 06 Jul 2009 |
| Total Posts: 9058 |
|
| |
|
ENET
|
  |
| Joined: 01 Jan 2010 |
| Total Posts: 4820 |
|
|
| 21 Nov 2011 04:05 PM |
| Guess you're gonnah have to murder me. |
|
|
| Report Abuse |
|
|