NobbyVedania Posted August 14, 2014 Share Posted August 14, 2014 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. 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. Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted August 14, 2014 Share Posted August 14, 2014 You are drawing in the update function. You need to draw in PostRender(context) Also note that Sync() might already be called in app.lua. Quote Link to comment Share on other sites More sharing options...
NobbyVedania Posted August 14, 2014 Author Share Posted August 14, 2014 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. Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted August 14, 2014 Share Posted August 14, 2014 Have a look at the raycasting tutorial. There is also a lua variant. I think it does more or less want you want to achieve. Especially the Camera:Project() function. http://www.leadwerks.com/werkspace/page/tutorials_legacy/_/introduction-to-raycasting-r13 Quote Link to comment Share on other sites More sharing options...
macklebee Posted August 14, 2014 Share Posted August 14, 2014 Example script here might help: http://www.leadwerks.com/werkspace/topic/10274-world-pick-collision-type-doesnt-work/#entry75890 1 Quote 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 More sharing options...
NobbyVedania Posted August 14, 2014 Author Share Posted August 14, 2014 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. Quote Link to comment Share on other sites More sharing options...
macklebee Posted August 14, 2014 Share Posted August 14, 2014 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. Quote 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 More sharing options...
AggrorJorn Posted August 14, 2014 Share Posted August 14, 2014 What is the script you currently have? Quote Link to comment Share on other sites More sharing options...
NobbyVedania Posted August 14, 2014 Author Share Posted August 14, 2014 What is the script you currently have? See posting #3. This script I've attached to 'box2' in the sample scene and also dropped the camera to the script camera parameter. Quote Link to comment Share on other sites More sharing options...
shadmar Posted August 14, 2014 Share Posted August 14, 2014 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 Quote HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB Link to comment Share on other sites More sharing options...
NobbyVedania Posted August 14, 2014 Author Share Posted August 14, 2014 That's it, thank you shadmar! Learned to draw on context in the PostRender Function, not to clear the context and to use camera:Project instead of Transform to get the screen space coordinates from an entity. Now I'm happy again. Quote Link to comment Share on other sites More sharing options...
macklebee Posted August 14, 2014 Share Posted August 14, 2014 That's it, thank you shadmar! sure... shadmar just repeats aggror's and my suggestions and he gets all the praise! lol 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 Quote 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 More sharing options...
shadmar Posted August 14, 2014 Share Posted August 14, 2014 Ah sorry, forgot to F5 before posting that (fiddeling in the editor), but macklebee and aggror covered it all, yes. Quote HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB Link to comment Share on other sites More sharing options...
NobbyVedania Posted August 14, 2014 Author Share Posted August 14, 2014 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! 1 Quote Link to comment Share on other sites More sharing options...
Slastraf Posted May 22, 2016 Share Posted May 22, 2016 Old thread but it does work in leadwerks 4 , I will use this in my project Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.