Scripts / Distance optimisations

Author
Message
Mouaa
User Banned
Posted: 25th Jan 2017 13:20 Edited at: 25th Jan 2017 13:22
Hi,


I read some good tips about some optimisation distance check, i share it here.


When you check distances this is how it looks in GG mainly

PlayerDX = g_Entity[e]['x'] - g_PlayerPosX

PlayerDY = g_Entity[e]['y'] - g_PlayerPosY

PlayerDZ = g_Entity[e]['z'] - g_PlayerPosZ

PlayerDist=math.sqrt(math.abs(PlayerDX*PlayerDX)+math.abs(PlayerDY*PlayerDY)+math.abs(PlayerDZ*PlayerDZ))


(There is no need to call abs() as a squarre is always positive)



You can optimize it in many ways.

1) Check the square box distance on X,Z plane only
If player and objects are not in the same box square there is no need to make a detailled distance check

isOnRange = abs( player.x - object.x ) < distanceRequired or abs( player.z - object.z ) < distanceRequired



2) Check Square distance to avoid the expensive root square calculation

local distanceRequired
local distanceCheck = distanceRequired * distanceRequired

dx = player.x - object.x
dz = player.z - object.z
deltaY = abs( player.y - object.y )


--check X,Z plane

if ( dx*dx + dy*dy ) < distanceCheck then

-- Check Y

if(deltaY < distanceRequired) then

--objects distance is lower than minimum distance required

end

end



I am not sure the performance impact gain will be visible, but there are good i used a lot.
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 25th Jan 2017 20:02
There are a lot of places in Lua where performance can be greatly improved, I suggest getting the official Lua guide and spending some time on the Lua site where all is revealed.

As for handy GG functions just look at my LEM demo, I pretty much use every trick in the book.
Been there, done that, got all the T-Shirts!
PM
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 25th Jan 2017 20:17
When you say "how it looks in GG mainly... " are you talking about in a script? or what the engine is doing back end?

Because generally for player distance we use:

PlayerDist = GetPlayerDistance(e)
if (PlayerDist < 100 ) then

or to display the distance:

PlayerDist = GetPlayerDistance(e)
Prompt( ""..PlayerDist )

and so on.

Which seems streamlined enough for me

i5, NV960 2GB, 16GB memory, 2x 2TB Hybrid, Win10.
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 25th Jan 2017 21:47
The problem with using GetPlayerDistance is that it uses the rather expensive square root function and if you are doing it every frame then it is a performance hog.

Two fixes are: Don't do it every frame, i.e. only do the check every 1/10 of a second or so, the other is to avoid square root by using a more efficient algorithm OR do what I do in my scripts which is both.
Been there, done that, got all the T-Shirts!
PM
Mouaa
User Banned
Posted: 26th Jan 2017 12:11
Quote: "Two fixes are: Don't do it every frame, i.e. only do the check every 1/10 of a second or so, the other is to avoid square root by using a more efficient algorithm OR do what I do in my scripts which is both."

Used not every frame is a great tip also, i used it a lot to avoid too many calls to Raycast for example.

Corno_1
GameGuru Tool Maker
13
Years of Service
User Offline
Joined: 3rd Nov 2010
Location:
Posted: 26th Jan 2017 16:14 Edited at: 26th Jan 2017 20:28
Quote: "Because generally for player distance we use:
PlayerDist = GetPlayerDistance(e)"

@Belidos
here is the function he mean in global.lua:

You see it is the same Mouaa talk about.

@Mouaa
Nice and interesting terms, even I am not sure how much performance math.sqrt use, but of course you way is faster.

@AmenMoses
Not sure if you jump over a frame calling the os time is faster as a calculation. But a switch like if(var=0) do elseif(var<3) var++ else var = 0 could save some perormance. Never tested it......

I will follow this thread to see where it goes
My dream is to develope games, which makes fun when I create it and fun when other people play it.
PM
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 26th Jan 2017 16:26 Edited at: 26th Jan 2017 20:00
Quote: "Sry to prove you wrong, but here is the function you mean in global.lua:"


@Corno_1
Not entirely sure exactly what you are trying to prove or disprove, i wrote nothing that warrants proof or disproof. I never said he was wrong or not, i never stated anything as right or wrong, I was asking a question.

The question being "what does he mean?" ie whether he meant what we type into lua (which is what my example was), or whether he meant what happens behind the scenes, as in your global quote.

I was generally interested because i wanted to know if there was a better way of putting it in lua.

And now i know which he meant, so thank you.

i5, NV960 2GB, 16GB memory, 2x 2TB Hybrid, Win10.
Mouaa
User Banned
Posted: 26th Jan 2017 18:27
Quote: "The question being "what does he mean?" ie whether he meant what we type into lua (which is what my example was), or whether he meant what happens behind the scenes, as in your global quote. "

There is nothing to proove, only showing some optimisations could be done in the function called GetDistance (check box distance first, check Y, then check squarre distance without root squarre call).

Until you don't need the distance value to make some more computations , you could create optimized functions like this one :
CheckDistanceInferior(obj1,obj2, distanceCheck)
It would return true when the distance between obj1 and obj2 is inferior to distanceCheck.

Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 26th Jan 2017 19:59
Quote: "There is nothing to prove"


Exactly, I was asking because I wasn't sure whether you were talking about the checks on the engine side of things or if you were talking about the lua scripts, because if you had found an easier way to write the distance check in the scripts then I would have been very interested

i5, NV960 2GB, 16GB memory, 2x 2TB Hybrid, Win10.
Mouaa
User Banned
Posted: 26th Jan 2017 20:07
Quote: "Exactly, I was asking because I wasn't sure whether you were talking about the checks on the engine side"

The checks of the engine works for anyone and are ok.
For experienced people there is ways to optimize the distance checks, for beginners they should stay simple and better stay with engine function instead.
Corno_1
GameGuru Tool Maker
13
Years of Service
User Offline
Joined: 3rd Nov 2010
Location:
Posted: 26th Jan 2017 20:37 Edited at: 26th Jan 2017 20:43
Sry wrong word. I changed it. My mistake.
My dream is to develope games, which makes fun when I create it and fun when other people play it.
PM
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 26th Jan 2017 21:26
Quote: "Sry wrong word. I changed it. My mistake."


No problem, I still luv ya

i5, NV960 2GB, 16GB memory, 2x 2TB Hybrid, Win10.
3com
9
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 26th Jan 2017 21:43
I think



is going to avoid floats, but absolute numbers.

And sometimes you can need to calculate deltaY, for example when you are underwater.

3com
Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 10 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics

PM
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 26th Jan 2017 21:44
Quote: "for example when you are underwater"


But the water kills!

i5, NV960 2GB, 16GB memory, 2x 2TB Hybrid, Win10.
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 26th Jan 2017 21:47


Been there, done that, got all the T-Shirts!
PM
Mouaa
User Banned
Posted: 26th Jan 2017 22:04
@3Com
You don't need to call Abs
math.abs(tPlayerDX^2)

A squarre of a number is always positive.
3com
9
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 27th Jan 2017 19:27
Quote: "A squarre of a number is always positive."

A simple matter of convention.

What is square root of 4?
a) 2 or -2,
b) 2.
The answer is (a) because there are two square roots of a positive number, not just one.

There is a standard notation with a radical sign which is used to distinguish between the two square roots of a positive number like 4.
It always refers to the positive square root.
If you want to refer to the negative square root, you have to explicitly include the negative sign.

Extracted from LUA Math lib tuto

Quote: "math.sqrt

Return the square root of a given number. Only non-negative arguments are allowed.

math.abs

Return the absolute, or non-negative value, of a given value."


I think Lee is using abs to avoid passing 'non-negative arguments' as params, to the sqrt function. Just my thought

3com
Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 10 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics

PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 27th Jan 2017 23:26
   if scheduler(e, 100) and g_text_lib then
        if g_ml_cutscene_complete and g_carl_mode == 0 and
           CloserThan(500, g_Entity[e]) then
 ....
 
 
function CloserThan(dist, Ent)
    dist = dist or 100
     
    local DX = g_PlayerPosX - Ent.x
    local DY = g_PlayerPosY - Ent.y
    local DZ = g_PlayerPosZ - Ent.z
     
    return (DX*DX + DY*DY + DZ*DZ) <= (dist * dist)
end

As I said you can get all this sort of stuff from my scripts, you will also find functions to rotate vectors, generate quaternions from Euler angles, generate Euler angles from quaternions, multiply quaternions etc etc

Wherever possible I've optimised all the code to give the best performance.


Been there, done that, got all the T-Shirts!
PM
Mouaa
User Banned
Posted: 28th Jan 2017 11:43 Edited at: 28th Jan 2017 12:18
@AmenMoses
What scripts i don't see them pinned on the forum ?

I have the code below moves and rotate an object on XZ plane when the player is carrying it.
I would like to rotate and move the object on YZ plane , how can i do it ?
The function RotateToCamera seems to only rotate the object on XZ plane and i would need a full rotation with YX plane also.

AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 28th Jan 2017 14:47
In the LEM thread, which I know you have posted to in the past.

Use my quaternion library to rotate objects, see the drop ship script for a simple example of how to do it.

I also posted a pickuppable script many moons ago which allows entities to be picked up, stacked on top of each other and then carried around, I've been meaning to go back and change it to use quaternions now I have figured out how to do it properly but got side tracked a little with the LEM demo and EBE testing.

Been there, done that, got all the T-Shirts!
PM
Mouaa
User Banned
Posted: 28th Jan 2017 16:56 Edited at: 28th Jan 2017 16:57
I searched in LEM files, but i don't understand how to use it.
The goal was to have some weapon or object facing player camera and following camera rotations.
Perhaps GG will get some AddChild(object) functions and will add more camera controls functions.
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 28th Jan 2017 17:09



If you have downloaded the LEM pack just look through the script bank stuff all the scripts are in the moonlander directory.
Been there, done that, got all the T-Shirts!
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 28th Jan 2017 17:16
Btw, if you don't know how to use quaternions there are plenty of tutorials online, you don't need to understand all the math to use them but obviously it helps to understand what is going on if you do.

If you only need to rotate in Y then you can do it without quaternions but if you rotate in X or Z and then try to rotate in Y it will not turn out as you expect it to.

Search the forums for "pickuppable" to find my old script.
Been there, done that, got all the T-Shirts!
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 28th Jan 2017 17:34


This video shows rotations using quaternions.
Been there, done that, got all the T-Shirts!
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 28th Jan 2017 19:52
As the search doesn't seem to search thread content here is the pickuppable script that was used in the video, it attempts rotation in Euler angles and demonstrates quite neatly why you need to use quaternions, I'll get around to updating it at some point.
Been there, done that, got all the T-Shirts!

Attachments

Login to view attachments
PM
Mouaa
User Banned
Posted: 5th Feb 2017 00:24
Another tip i used when there is lot of items in a big level

- check distance each 5 second interval as player can't reach items below this treashold when the game starts
- When an item detects the player distance becomes inferior to a first treshold , then it checks distance each 1 second
- If the player distance becomes inferior to a second treshold it checks distance each 0.5 second

You can expand this for many different things not only distance, for example this could be used to control effects details or AI detail calculation.

Login to post a reply

Server time is: 2024-03-28 14:05:02
Your offset time is: 2024-03-28 14:05:02