• UtilX -- Extending GLua -- Need community input
    371 replies, posted
[QUOTE=Lexic;17880002]An hatstand? [editline]06:33PM[/editline] An horse? An house? An headcrab? An hose? Why is there an H there, I can't think of any words that begin with h that use 'an' [editline]06:37PM[/editline] An hole, an hand, an homing missile. [editline]06:37PM[/editline] You ain't nothing but an hound dog.[/QUOTE] This is an historic failure. (^ correct ^) [editline]01:44AM[/editline] I'm guessing 'historic' is the only word that needs this... [editline]01:45AM[/editline] [lua]function util.AOrAn(s) return string.match(s, "^[AaEeIiOoUu]") and "an" or "a" end[/lua]
[QUOTE=Deco Da Man;17879745][lua]function CapitaliseFirstLetter(str) return string.gsub(str, "%w", string.upper, 1) end[/lua][/QUOTE] Sorry for bothering you again, but would it be possible to capitalize the first letter of every word in a string instead of only the first word?
[QUOTE=Dlaor;17880319]Sorry for bothering you again, but would it be possible to capitalize the first letter of every word in a string instead of only the first word?[/QUOTE] [lua] function Capitalize( x ) return string.gsub( x, "(%a)(%a+)", function( first, after ) return string.upper( first ) .. string.lower( after ); end ) end [/lua] Learn patterns, they're very useful. [url]http://www.lua.org/pil/20.2.html[/url]
[QUOTE=Salads;17880917][lua] function Capitalize( x ) return string.gsub( x, "(%a)(%a+)", function( first, after ) return string.upper( first ) .. string.lower( after ); end ) end [/lua] Learn patterns, they're very useful. [url]http://www.lua.org/pil/20.2.html[/url][/QUOTE] Thanks! [editline]10:20PM[/editline] Hmm... I'm starting to get the hang of it. [lua]function TurnIntoBytes( str ) return string.gsub( str, ".", function(t) return string.byte(t) .. " " end) end[/lua] Will turn every character in a string into a byte :v:
[QUOTE=Lexic;17873999]What? Neither of those would do anything useful what so ever. The first one would return the first entity found by ents.FindByClass() and the second one would throw an error about 'ent' not being defined. what were you trying to do? [editline]08:30AM[/editline] Also GetMetaTable isn't even a function.[/QUOTE] oh wow i just noticed that. :saddowns: EDIT: just to let you know, i just popped that right out of my head... i think the other stuff i've coded is much better than that...
a university [editline]01:53AM[/editline] a united ... an unitemized ... that's 5 letters in common. i'd say you can't solve this little problem without a dictionary :)
This should get the points where the line defined by the 2 points pt1, pt2 intersect the circle defined by the circle structure ring. (ring is a table like this: { Origin = Vector, Radius = Number }) It accepts 3d vectors but only works in 2d. I'll probably be using it for my ring math in Population. It probably isn't too well optimized, and there's a chance it won't work. (I figured out all the math myself, but I tested it and the formula works) [lua]function utilx.LineIntersectCircle( pt1, pt2, ring ) // Translate the points to match the origin local pointA = Vector( pt1.x-(ring.Origin.x), pt1.y-(ring.Origin.y), 0 ) local pointB = Vector( pt2.x-(ring.Origin.x), pt2.y-(ring.Origin.y), 0 ) // Get all required variables local m = (pointB.y-pointA.y)/(pointB.x-pointA.x) local c = pt1.y - (pt1.x*m) local r = ring.Radius // Calculate common terms local m2 = m^2 local r2 = r^2 local c2 = c^2 // Calculate the discriminant of the formula local d = (4*m2*c2) - (4*(m2+1)*(r2-c2)) // The discriminant is negative, there are no solutions if (d < 0) then return end // Calculate the positive square root of the discriminant local sqrt = 0 -- If the discriminant is 0, there is only 1 solution if (d > 0) then sqrt = d ^ 0.5 end // Complete the formula for both solutions local x1 = ((-2*m*c) + sqrt)/(2*(m2 + 1)) local x2 = ((-2*m*c) - sqrt)/(2*(m2 + 1)) // Calculate y for both solutions // y = mx + c local y1 = (m*x1) + c local y2 = (m*x2) + c // Construct the vectors local a = Vector( x1 + ring.Origin.x, y1 + ring.Origin.y, ring.Origin.z ) local b = Vector( x2 + ring.Origin.x, y2 + ring.Origin.y, ring.Origin.z ) // If the discriminant is 0, only return 1 solution (a == b) if (d == 0) then return a end // Return the solutins return a, b end[/lua]
[QUOTE=thomasfn;17916963]This should get the points where the line defined by the 2 points pt1, pt2 intersect the circle defined by the circle structure ring. (ring is a table like this: { Origin = Vector, Radius = Number }) It accepts 3d vectors but only works in 2d. I'll probably be using it for my ring math in Population. It probably isn't too well optimized, and there's a chance it won't work. (I figured out all the math myself, but I tested it and the formula works)[/QUOTE] I made some optimizations and made it easier to read in some places: (although I doubt the usefulness of this function in general) [lua]function utilx.LineIntersectCircle( pt1, pt2, ring ) -- Get a direction vector between the points local diff = pt2-pt1 -- Get all required variables local m = diff.y/diff.x local c = pt1.y - pt1.x*m local r = ring.Radius -- Calculate common terms local m2_1 = m*m+1 local term = -m*c/m2_1 -- Calculate the discriminant of the formula local d = m2_1*c*c-r*r -- The discriminant is negative, there are no solutions if (d < 0) then return end -- Calculate the positive square root of the discriminant local sqrt = 0 -- If the discriminant is positive, there are 2 solutions if (d > 0) then sqrt = (d ^ 0.5)/m2_1 end -- Complete the formula for both solutions local x1 = term + sqrt local x2 = term - sqrt -- Calculate y for both solutions -- y = mx + c local y1 = (m*x1) + c local y2 = (m*x2) + c -- Construct the vectors local a = Vector(x1, y1, 0) + ring.Origin local b = Vector(x2, y2, 0) + ring.Origin -- If the discriminant is 0, only return 1 solution (a == b) if (d == 0) then return a end -- Return the solutions return a, b end[/lua]
[QUOTE=TomyLobo;17925606]I made some optimizations and made it easier to read in some places: (although I doubt the usefulness of this function in general) [lua]stuff[/lua][/QUOTE] This is one awesome function!
[QUOTE=Dlaor;17929554]This could be done easier with traces. Just create a ring mesh for use with PhysicsInitMesh, then just do a trace from p1 to p2.[/QUOTE] Can this be done 100% clientside?
[QUOTE=Dlaor;17929554]This could be done easier with traces. Just create a ring mesh for use with PhysicsInitMesh, then just do a trace from p1 to p2.[/QUOTE] rated you dumb, because this would take like 1000 times more cpu time
-snip-
[QUOTE=Dlaor;17942142]It's still a worthless function.[/QUOTE] You're an idiot. Traces involve: - Calculating the visleaves that the ray intersects. - Iterating each entity in these visleaves and running conditions on them to see if they would collide with the ray. - Calculating the collisions with each entity. An entity with PhysicsInitSphere requires: - Initialising the entity - this means allocating all the needed memory and constructing and loading the physics model into the engine. - Running various hooks throughout the engine that checks whether the entity meets certain parameters (e.g: Lua hooks) TomyLobo's function involves: - Simple 2D maths. [b]It is not a worthless function.[/b] Though it's use will be rare in GMod, I believe.
-snip-
[QUOTE=Dlaor;17942768]Traces are still faster.[/QUOTE] You've tested this?
-snip-
[QUOTE=Dlaor;17942828]No, but trigonometric functions are extremely slow in Lua, while traces are one of the most optimized things in Source. And creating entities is pretty damn fast too.[/QUOTE] I see no trigonometric functions in that code.
-snip-
[QUOTE=Dlaor;17942909]Division math then, whatever. [editline]04:17PM[/editline] And addition math and subtraction math and multiplication math. [editline]04:17PM[/editline] And square root math.[/QUOTE] Math functions take all of 0.003 of a tick (Roughly). A trace will take a lot more than that.
-snip-
[QUOTE=Dlaor;17944905]Who cares about CPU calculate time anyways, it won't make any difference. Just use traces, it's the best solution. (It can even do 3D calculation!)[/QUOTE] Yes, but can you do it 100% clientside, which was the intended purpose for my function anyway?
[QUOTE=Dlaor;17944905]Who cares about CPU calculate time anyways, it won't make any difference. Just use traces, it's the best solution. (It can even do 3D calculation!)[/QUOTE] Bam, Sphere. [lua]/*------------------------------------ RayIntersectSphere() ------------------------------------*/ function util.RayIntersectSphere( startPos, rayDir, spherePos, sphereRadius ) local dst = startPos - spherePos; local b = dst:Dot( rayDir ); local c = dst:Dot( dst ) - ( sphereRadius * sphereRadius ); local d = b * b - c; if( d > 0 ) then return -b - math.sqrt( d ); end end [/lua] Using a trace is the worst way imaginable. You have no idea how many more calculations a trace has to do. Refer to Deco's post.
[QUOTE=Jinto;17945226]Bam, Sphere. [lua]/*------------------------------------ RayIntersectSphere() ------------------------------------*/ function util.RayIntersectSphere( startPos, rayDir, spherePos, sphereRadius ) local dst = startPos - spherePos; local b = dst:Dot( rayDir ); local c = dst:Dot( dst ) - ( sphereRadius * sphereRadius ); local d = b * b - c; if( d > 0 ) then return -b - math.sqrt( d ); end end [/lua][/QUOTE] Where's the Thanks rating when you need it. Just what I was looking for.
Math > Traces
[QUOTE=Dlaor;17945688]You are all idiots. Math functions will NEVER reach the speed of traces. According to testing, traces are 43 ms faster than using math. Anyways, I'll stop this discussion since nobody seems to believe me.[/QUOTE] Traces use math functions as well :3
[QUOTE=Dlaor;17945688]You are all idiots. Math functions will NEVER reach the speed of traces. According to testing, traces are 43 ms faster than using math. Anyways, I'll stop this discussion since nobody seems to believe me.[/QUOTE] [url]http://www.devmaster.net/articles/quake3collision/[/url] Please read how they work.
[QUOTE=Dlaor;17945688]You are all idiots. Math functions will NEVER reach the speed of traces. According to testing, traces are 43 ms faster than using math. Anyways, I'll stop this discussion since nobody seems to believe me.[/QUOTE] I would like to see these tests.
Math functions are performed directly inside the CPU, via the ALU (Arithmetic Logic Unit). The required numbers are fetched from RAM, stored inside registers and processed in the ALU and the result is sent back into RAM. A common math operation (such as multiplying or powers) will take under 25 "beats" of the system clock to perform. For very basic stuff like adding 3 and 2, it would take about 3 beats. As for traces, they are not a native procedure within the CPU - they are compiled instructions stored in RAM loaded from the HL2 binaries. The CPU has to do alot more work just to execute the traceline instructions than to do a simple math operation, let alone do all the actual math to perform the traceline anyway. (I take computing at A level, so I know this sort of stuff). But if you prove us wrong with these apparent "tests", so be it.
People trolled: 14 [editline]09:46PM[/editline] Of course I know that traces are wrong, only after learning it from you guys :3:
5 people opposing you should be quite a good hint, dlaor especially if you have no basis, evidence or even links supporting your claim
Sorry, you need to Log In to post a reply to this thread.