Jump to content

Beam Class


reepblue
 Share

Recommended Posts

This was a quick test, and it worked first try. This is a Beam class derived from the Sprite class. All the beam really is is a sprite that streaches from one point to another. Use Josh's tolua++ generator to expose it.

 

The only way to create a beam is with something like this:

 

Script.EndPoint = nil --Entity
function Script:Start()
self.beam = Beam:Create()
self.beam:SetStartPoint(self.entity)
self.beam:SetEndPoint(self.EndPoint)
self.beam:SetMaterial(self.entity:GetMaterial())
end

post-12469-0-75451600-1487115452_thumb.png

le_beam_class.zip

  • Upvote 6

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

Reepblue we seem to have the same idea all the time! I also have a lua based laser beam script for leadwerks. Though I suspect yours will handle materials better than mine can.

 

These are the notes to the script:

Notes:            If you want your beam to be see through, the material need to be see through.
                   My Recomendation is to set the material to alpha and to set the last diffuse box to 128
                   Since we are streching a quad (thats what a sprite is in Leadwerks) your material will be unrecognizable

 

 

 

Script in spoiler.

 

 


--[[

Title: Laser Beam

Author: Einlander

Version: .01

Start Date: 1-22-2016

Description: Draws a laser beam between 2 pivots

Notes: If you want your beam to be see through, the material need to be see through.

My Recomendation is to set the material to alpha and to set the last diffuse box to 128

Since we are streching a quad (thats what a sprite is in Leadwerks) your material will be unrecognizable

--]]

 

Script.enabled = true --bool "Enabled"

Script.EndPoint = nil --entity "End Point:"

Script.BeamMaterialFile = "" --path "Beam Material:" "Material File (*.mat):mat|Material"

Script.BeamHeight = .2 --float "Beam Height cm:"

Script.PhysicsUpdate = true --bool "Update in Physics:"

 

function Script:Start()

self.BeamHeight = self.BeamHeight / 10

 

-- Check End Point

Debug:Assert(self.EndPoint ~= nil , "End point missing. Was an entity added?")

 

-- Load the Material

self.BeamMaterial = Material:Load(self.BeamMaterialFile)

Debug:Assert(self.BeamMaterial ~= nil , "Error loading the material.")

self.BeamMaterial = tolua.cast(self.BeamMaterial:Copy(),"Material") -- force a copy of the asset because we will need to modify it.

self.BeamMaterial:SetSortMode(true) -- force zsorting (It will be used in a sprite,)

 

-- Create sprite

self.LaserBeam = Sprite:Create()

self.LaserBeam:SetSize(self.BeamHeight,.1)

self.LaserBeam:SetPickMode(0)

self.LaserBeam:SetMaterial(self.BeamMaterial) -- assign material

self.LaserBeam:SetShadowMode(2)

self.LaserBeam:Show()

 

--draw Initial Beam

if self.enabled then

self:DebugRay(self.entity:GetPosition(true), self.EndPoint:GetPosition(true), self.LaserBeam)

end

self.OldStartPosition = self.entity:GetPosition(true)

self.OldEndPosition = self.EndPoint:GetPosition(true)

end

 

 

-- Draws the Beam

function Script:DebugRay(startpos_as_vec3, endpos_as_vec3, spriteEntity_as_sprite)

local startpos = startpos_as_vec3

local endpos = endpos_as_vec3

local spriteEntity = spriteEntity_as_sprite

local midpoint = ((startpos + endpos) / 2)

 

local raydistance = math.sqrt((endpos.x - startpos.x)^2 + (endpos.y - startpos.y)^2 + (endpos.z - startpos.z)^2)

 

spriteEntity:SetPosition(midpoint,true)

local allignvector = startpos - endpos

spriteEntity:SetViewMode(6)

spriteEntity:AlignToVector(allignvector:Normalize(),2)

spriteEntity:SetSize(self.BeamHeight, raydistance)

end

 

 

 

function Script:UpdateWorld()

--check if the positions moved

if (self.PhysicsUpdate == false) and self.enabled then

if ((self.OldStartPosition == self.entity:GetPosition(true)) ~= (self.OldEndPosition == self.EndPoint:GetPosition(true))) then -- Only update if moved

self:DebugRay(self.entity:GetPosition(true), self.EndPoint:GetPosition(true), self.LaserBeam)

self.OldStartPosition = self.entity:GetPosition(true)

self.OldEndPosition = self.EndPoint:GetPosition(true)

end

end

 

end

 

 

 

function Script:UpdatePhysics() -- update in physics to save some processing cycles

--check if the positions moved

if (self.PhysicsUpdate == true) and self.enabled then

if ((self.OldStartPosition == self.entity:GetPosition(true)) ~= (self.OldEndPosition == self.EndPoint:GetPosition(true))) then -- Only update if moved

self:DebugRay(self.entity:GetPosition(true), self.EndPoint:GetPosition(true), self.LaserBeam)

self.OldStartPosition = self.entity:GetPosition(true)

self.OldEndPosition = self.EndPoint:GetPosition(true)

end

end

end

 

function Script:Enable()--in

if self.enabled==false then

self.enabled=true

self.component:CallOutputs("_Enabled")

end

end

 

function Script:Disable()--in

if self.enabled then

self.enabled=false

self.component:CallOutputs("_Disabled")

end

end

 

--This function will be called when the entity is deleted.

function Script:Detach()

self.BeamMaterial:Release()

self.LaserBeam:Release()

end

 

 

 

  • Upvote 1
Link to comment
Share on other sites

This class is actually based off my beam script, but much simpler. This is written in C++, and all it does is stretch and orient the sprite to two points. You can use this class to make lasers like in Portal 2, simple elevator cables or physic ropes with a script. This just makes it so you don't have to do the setup and math each time.

 

Since the beam is derived from the Sprite Class, everything when it comes to loading a material, releasing and such; that's inherited from the base entity/object class. All I really did was did the math in the Draw call.

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

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