Jump to content

Drawing Lines


Rekindled Phoenix
 Share

Recommended Posts

How would I go about 'unproject'ing a particular type of object (let's say AI_Node), and draw a line between all of the same types of objects? for a demonstration I need to show a nav-mesh, written in Lua, viewed within the Sandbox editor.

 

Is it possible to only execute certain lua functions when viewed within the sandbox editor?

As an alternate solution, checking whether in debug mode might work.

Link to comment
Share on other sites

You should be able to do your line drawing after you hook your script to the drawing hook and using http://www.leadwerks.com/wiki/index.php?title=Cameras#CameraUnproject to project lines.

 

 

Here is the main game loop I used since I controlled everything from entity scripts and not the main game loop. You set a global variable and check against that variable inside your scripts. Note that I also allow each script to have an Initialize method which only gets called once when entering game mode. This allowed my scripts to init some values that only needed to be initialized when entering game mode instead of design mode.

 

require("Scripts/constants/collision_const")
require("Scripts/constants/engine_const")
require("Scripts/LinkedList")
require("Scripts/filesystem")
require("Scripts/math/math")



if fw==nil then --we are not in Editor
       RegisterAbstractPath("")
       Graphics(800,600)		-- need a better way to get screen res
       fw=CreateFramework()
       scene=LoadScene("")		-- need a way to get a scene to load
       scene:SetCollisionType(COLLISION_SCENE)
       TFilter(1)
       AFilter(4)
end


-- set globals
SetGlobalString("mode", "GAME_MODE")
SetGlobalString("quit", "false")

FlushKeys()
FlushMouse()

for k,v in pairs(objecttable) do
if v.Initialize ~= nil then
	v:Initialize()
end
end

--main function
while (KeyHit(KEY_ESCAPE) == 0) and (GetGlobalString("quit") == "false") do

       fw:Update()
       fw:Render()
       Flip(0)
end

-- reset values
SetGlobalString("mode", "DESIGN_MODE")
SetGlobalString("quit", "false")

Link to comment
Share on other sites

Thanks Rick for the awesome code sample.

 

I guess my question was more related to individual item scripts interacting on their own.

Can you help? I still can't get it to work...

 

require("scripts/class")
local class=CreateClass(...)

function class:InitDialog(propertygrid)
self.super:InitDialog(propertygrid)
local group=propertygrid:AddGroup("Pathing")
group:AddProperty("Avoidance Radius",PROPERTY_STRING)
end

function class:CreateObject(model)
local object=self.super:CreateObject(model)

function object:Update()
	-- self.model:Turn(Vec3(0,1,0)) -- Used to verify script is valid

local cpos = EntityPosition(self.model)
local Pos = CameraUnproject(fw.main.camera, cpos)
DrawText("I'm located here!", Pos.x, Pos.x)
end
end

 

I know I'm somewhat close to the solution, but I keep getting exceptions which crash the Sandbox.

I'm still getting used to the Lua syntax...

Link to comment
Share on other sites

Your above code doesn't crash my editor but it doesn't draw the text either. Try this as a quick and dirty way to draw text. Note that when hooking into the "Flip" hook it's a global function and not specifically a script function, so not sure what happens when you put more than one of these on screen if the DrawMe() just gets added to the hook and called multiple times or if it overwrites the "Flip" hook and so calling it within an entity script is kind of pointless.

 

What I'm doing with my test GUI is having a controller script which is an invisible model. The idea is to only put one instance of this controller script in your scene and it'll do this "Flip" hook and it'll loop through all objects like I am below and look for specific properties of the things I care about.

 

In my GUI example I have a GuiDraw script that is looking for entities that have a property called type which is set to "gui.control" and then it knows to call the Draw function of that control which is a function I define as part of the object, it's not an LE specific function.

 

You could do the same with your pathfinding scripts. Having a master script which there is only meant to be 1 placed in the scene can act as the central controller for other scripts as long as the master script has a way to find the other scripts.

 

require("scripts/class")
require("scripts/hooks")
local class=CreateClass(...)

function class:InitDialog(propertygrid)
       self.super:InitDialog(propertygrid)
       local group=propertygrid:AddGroup("Pathing")
       group:AddProperty("Avoidance Radius",PROPERTY_STRING)
end

function DrawMe()
for k,v in pairs(objecttable) do
               if v.type ~= nil then
		if v.type == "test" then
			local cpos = EntityPosition(v.model)
			local Pos = CameraUnproject(fw.main.camera, cpos)
			DrawText("I'm located here!", Pos.x, Pos.y)
		end
	end
end
end

function class:CreateObject(model)
       local object=self.super:CreateObject(model)

object.model = model
object.type = "test"
AddHook("Flip", DrawMe)

       function object:Update()
               self.model:Turn(Vec3(0,1,0)) -- Used to verify script is valid
       end
end

Link to comment
Share on other sites

This is exactly what I was looking for!

 

I will probably modify your script to scan through all entities, checking for "NavMesh " types. This will give me a collection of entities to attach the DrawLine() method too.

 

Checking 'fw' will allow me to only execute the script within the Sandbox for demonstration.

 

Thank you Rick!

Link to comment
Share on other sites

I'm currently working on a pathfinding module that I can attach to any NPC within C#. Once that is completed, I'm going to try to port as much as possible to Lua for others to use. Users will be able to choose different types of pathfinding defined in the dropdown that will be available within the Editor property menu.

 

I'm taking the method applied in this blog post, and adapting it to the standard Leadwerks AI_Node objects for others to use. Using a combination of avoidance objects to define the blacklist where not to walk, and (completely optional) NavMesh nodes to use for guidance, will create a flexible and easy to use pathing system.

Link to comment
Share on other sites

I'm coming across an odd issue related to drawing labels on objects using the recently posted script.

 

As shown in the screenshot, if I am facing the opposite direction, by moving the camera forward a couple of units, and the unproject function is printing the label as if the rock was on the other side of the map. Do you know what's going on??

post-214-0-79132800-1307598145_thumb.jpg

Link to comment
Share on other sites

Interesting. I guess one way to get around this is to see if the model is visible or not (EntityVisible) to the fw.main.camera and if it's not skip drawing the name. That could cause other issues if you have smaller objects behind bigger objects but not sure if that would matter to you or not. I don't remember ever running into that issue.

Link to comment
Share on other sites

I'm coming across an odd issue related to drawing labels on objects using the recently posted script.

 

As shown in the screenshot, if I am facing the opposite direction, by moving the camera forward a couple of units, and the unproject function is printing the label as if the rock was on the other side of the map. Do you know what's going on??

I found when using CameraUnproject that if the returned z value was less than 0, that is the object is behind you, then I needed to inhibit the display of the label or it would display in odd places.

Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...