generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Changing the way you think about Radians

Previous Thread :: Next Thread 
stickmanx25 is not online. stickmanx25
Joined: 17 Aug 2007
Total Posts: 5829
14 May 2014 05:40 AM
One issue that is oft brought up when dealing with CFrame and rotation is the use of radians. Without getting too into the gritty details of it, I find that most scripters on Roblox are doing radians entirely wrong from a programming standpoint.

In this thread, I will be explaining why you should never, EVER use math.pi/float to do radian calculations.

For starters, math.pi is, while in the math library, NOT a function. It is instead a global constant. This constant being equal to 3.1415926535898.
math.pi is useless, people. In almost every instance, you'll be better off rounding to the third, or SOMETIMES fourth decimal.
In fact, you would need 39 digits of pi to measure the width of the circumference of the universe within the width of a single hydrogen atom. And while math.pi is less than 39 digits, chances are that anything you build on Roblox will not equal a notable fraction the size of the universe, and that in most cases, 3.1416 will do you perfectly, and be easier to calculate.

So if we decided to actually memorize pi instead of using that awful handicap in the math function, we could easily cut down the processing needs of a computer to run that calculation by HALF. Maybe not too big a deal in some scripts, but if you're, say, rotating a model in a while loop, that's a big performance gap.

Next up, we've got the fact that most of y'all been making the fatal mistake of DIVIDING in programming. This is INCREDIBLY UGLY. If you ever have to divide something again, I want you to sit down and ask yourself "why am I dividing?" Because if there's any chance that multiplication can do the job, YOU DO IT. Without going into assembly detail, division problems are handled by any given APU in an iterative fashion, which is several times slower than the linear fashion in which multiplication is calculated. ALWAYS MULTIPLY. NEVER, EVER DIVIDE. Even if it means a little more work for you, just don't use that "/" unless you REALLY need to.

Finally, the way I most often see math.pi being used is in loops, which is ironically, the worst possible offender. Namely because of how people usually use math.pi in loops.

Tell me, does this look familiar to you at all?

for i=1,16 do
gyro.cframe=torso.CFrame*CFrame.new(0,math.pi/8,0)
end

If that does look familiar to you, you need to pay extra attention to what I'm about to say. PI DIVIDED BY 8 IS A CONSTANT. It would be more than 16 times faster to do this

local rad=math.pi/8
for i=1,16 do
gyro.cframe=torso.CFrame*CFrame.new(0,rad,0)
end

See that? ONE LINE of code. Almost no data taken up. Over 16 times faster interpretation. It's even faster if you convert that division by 8 into multiplication(math.pi*.125), or better yet, run the numbers yourself in a calculator which wll take you two seconds to open, and use that to crunch your numbers, rather than forcing Lua to painstakingly do the division itself because you're too lazy to apply the basic logic to know that dividing pi with more digits than you need every frame is going to be inefficient as can possibly be!

/rant

TL;DR: I hate math.pi/float, and for good reason.
Report Abuse
vlekje513 is not online. vlekje513
Joined: 28 Dec 2010
Total Posts: 9057
14 May 2014 05:46 AM
http://upload.wikimedia.org/wikipedia/commons/4/4e/Circle_radians.gif
Report Abuse
stickmanx25 is not online. stickmanx25
Joined: 17 Aug 2007
Total Posts: 5829
14 May 2014 05:50 AM
@vlek
Well, I was talking more in a programming optimization sense and somewhat venting my frustration, but I suppose that's important, too.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image