Jump to content

LUA Draw a Line on Context


NobbyVedania
 Share

Recommended Posts

Have made lots of test with C++ and testing LUA now because this seams to be quite easier to work with.

 

I have made a new lua project and loaded the start map. In "box2" I added this script:

 

Script.camera = nil -- entity
Script.window = nil
Script.context = nil

function Script:Start()
 self.window=Window:GetCurrent()
 self.context=Context:GetCurrent()
end

function Script:UpdateWorld()
--Draw a line from box position to current mouse position
 local p0 = Transform:Point(self.entity:GetPosition(),self.camera,nil)
 local p1 = Transform:Point(self.window:GetMousePosition(),self.camera,nil)
 self.context:Clear()
 self.context:SetBlendMode(Blend.Alpha)
 self.context:SetColor(1,0,0)
 self.context:DrawLine(p0.x,p0.y,p1.x,p1.y)
 self.context:SetBlendMode(Blend.Solid)
 self.context:SetColor(0,0,0)
 self.context:Sync(false)
end

 

I want to draw a line from box2 position to current mouse position. But the line starts not at the box2 position and the screen keeps black. The scene camera has been already attached to the script parameter. Searched documentation on context drawing with LUA, but could not solve this issue yet.

 

But I'm sure it take you just a minute for the experienced members here to see what's wrong with this code. smile.png

 

The idea is to fire missiles along the line which represents a kind of laser pointer. The targets gets focused with the laser pointer and on mouse click a pick function will raycast the route to the target and fire the missile. It's a different map where I'm working on with more scripts than this snipped from the launcher script. I reconstructed my issue on the start map so it's easier for you to check it in your environment.

 

btw ... I'm using the windows version of Leadwerks 3.2.

Link to comment
Share on other sites

Thanks for quick answer Aggor and btw thanks for all you great tutorials.

 

Unfortunately the screen still keeps black. This is the new code:

 

Script.camera = nil -- entity
Script.window = nil

function Script:Start()
 self.window=Window:GetCurrent()
end

function Script:UpdateWorld()
end

function Script:PostRender(context)
 --Draw a line from box position to current mouse position
 local p0 = Transform:Point(self.entity:GetPosition(),self.camera,nil)
 local p1 = Transform:Point(self.window:GetMousePosition(),self.camera,nil)
 context:Clear()
 context:SetBlendMode(Blend.Alpha)
 context:SetColor(1,0,0)
 context:DrawLine(p0.x,p0.y,p1.x,p1.y)
 context:SetBlendMode(Blend.Solid)
 context:SetColor(0,0,0)
end

 

Also the line does not start at box 2 position.

Link to comment
Share on other sites

Thanks Aggror. This tutorial is very helpful for learning raycasting. I've already seen it and played around with raycasting in C++.

 

But in LUA the screen keeps black in my object script. This is the main issue currently. In App.lua everything would be work fine drawing on the context.

 

Thanks also to macklebee. But this is also an App.lua sample code.

Link to comment
Share on other sites

Well you are clearing the context - why? And typically you would set the color back to 1,1,1 when done using draw commands. And the example I was showing you can be just cut and pasted basically into a entity script... granted you will have issues anytime the entity itself is not rendered.

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Remove context:clear() and use Project to get the box position in screenspace :

 


function Script:PostRender(context)

--Draw a line from box position to current mouse position

local p0 = self.camera:Project(self.entity:GetPosition())

local p1 = Transform:Point(self.window:GetMousePosition(),self.camera,nil)

context:SetBlendMode(Blend.Alpha)

context:SetColor(1,0,0)

context:DrawLine(p0.x,p0.y,p1.x,p1.y)

context:SetBlendMode(Blend.Solid)

context:SetColor(0,0,0)

end

HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB

Link to comment
Share on other sites

That's it, thank you shadmar!

sure... shadmar just repeats aggror's and my suggestions and he gets all the praise! lol tongue.png

 

and btw, the mouse position is already in screen space coordinates, so no need to transform if you are just using them to draw a line

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

btw, the mouse position is already in screen space coordinates, so no need to transform if you are just using them to draw a line

you're right, thank's for this hint

 

changed the code ...

 --Draw a line from box position to current mouse position

   local p0 = self.camera:Project(self.entity:GetPosition())
   local p1 = self.window:GetMousePosition()

   context:SetBlendMode(Blend.Alpha)
   context:SetColor(1,0,0)
   context:DrawLine(p0.x,p0.y,p1.x,p1.y)
   context:SetBlendMode(Blend.Solid)
   context:SetColor(0,0,0)

 

THANKS AGAIN to everyone for your help!

  • Upvote 1
Link to comment
Share on other sites

  • 1 year later...

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...