Jump to content

Terrain collision and respawn


mihaid
 Share

Recommended Posts

Hello, i need some help with the next issue:

 

I have a flying machine, helycopter, and i want to launch a missile(obuz in my script), with physics to hit terrain. All working fine, but i cant figure this things:

I want to respawn the missile(obuz) back on helycopter

I want to make a big explosion hitting terrain. Emiter should be child of missile ???How to do that in collosion function and where??

 

I created a pivot child to helycopter, also the missile, created in Leadwerks, as child of pivot, not sure what kind of physics collision to set, prop, trigger,... I made a script combined from few others, such us respawnpoint, deadtrigger, collision function, but ....no more ideas....anybody can help???  here is the script, attached to my missile:

 

Script.respawnPoint = "" -- entity "Respawn Point"

Script.soundfile1=""--path "Sound 1" "Wac file (*.wav):wav|Sound"
Script.soundfile2=""--path "Sound 2" "Wac file (*.wav):wav|Sound"
Script.soundfile3=""--path "Sound 3" "Wac file (*.wav):wav|Sound"
Script.soundfile4=""--path "Sound 4" "Wac file (*.wav):wav|Sound"
Script.threshhold=2--float "Threshhold"
Script.maxfrequency=300--minimum delay between sound plays
Script.range=200--float "Range"
Script.window = Window:GetCurrent()


ImpactNoiseLastSoundTime=0-- This will be shared among all instances of this script and ensure we don't play too many sounds

function Script:Start()
    self.sound={}
    for n=1,4 do
        local filepath = self["soundfile"..tostring(n)]
        if filepath~="" then
            local noise = Sound:Load(filepath)
            if noise~=nil then
                table.insert(self.sound,noise)
                --self.sound[#self.sound+1]=noise
            end
        end
    end
end

function Script:Collision(entity, position, normal, speed)

 


    if speed>self.threshhold then
        if #self.sound>0 then
            local collisiontype = entity:GetCollisionType()
            if collisiontype==Collision.Prop or collisiontype==Collision.Scene then
                local t = Time:GetCurrent()
                if t-ImpactNoiseLastSoundTime>self.maxfrequency then
                    ImpactNoiseLastSoundTime=t
                    local n=math.random(#self.sound)
                    local noise = self.sound[n]
                    self.entity:EmitSound(noise,self.range)

--if (entity:GetKeyValue("name") == "obuz") then
  --          spawnPos = self.respawnPoint:GetPosition()
    --       entity:SetPosition (spawnPos)
                          
    --end


                end
            end
        end
    end
end


function Script:UpdatePhysics()
    
self.move = Vec3(0,0,0)

   if (self.window:KeyDown(Key.B))  then

          self.entity:SetMass(1)

         self.entity:SetParent(nil)
        self.entity:AddForce(0,0,200, false)

    end

end

Link to comment
Share on other sites

You are pretty close actually. You create a new instance of an emitter and place it at the collision point.

if (entity:GetKeyValue("name") == "obuz") then
	local explosion = Emitter:Create() 
	explosion:SetPosition(position)			  
end

After that you can do the respawn of the rocket. Alternatively you can also just create a new instance of the rocket and simply destroy the rocket once it explodes. 

Link to comment
Share on other sites

  • 2 weeks later...

I tried several times to make the respawn missile, NOT WORKING......here is the script:

--import "Scripts/Functions/ReleaseTableObjects.lua"

Script.respawnPoint = "" -- entity "Respawn Point"
Script.soundfile1=""--path "Sound 1" "Wac file (*.wav):wav|Sound"
Script.soundfile2=""--path "Sound 2" "Wac file (*.wav):wav|Sound"
Script.soundfile3=""--path "Sound 3" "Wac file (*.wav):wav|Sound"
Script.soundfile4=""--path "Sound 4" "Wac file (*.wav):wav|Sound"
Script.threshhold=2--float "Threshhold"
Script.maxfrequency=300--minimum delay between sound plays
Script.range=200--float "Range"
respawnPoint = nil
Script.pivotobuz = nil
--smoke = LoadMaterial("Materials/smoke.mat")

Script.window = Window:GetCurrent()
--Script.move = 0
--Script.entity = Vec3(0,0,0)
obuzforce = 250

ImpactNoiseLastSoundTime=0-- This will be shared among all instances of this script and ensure we don't play too many sounds

function Script:Start()
--particle1 = Emitter:Create(30) 
--particle1:Paint(smoke)
--particle1:SetRadius(0.5,0.5)


    self.sound={}
    for n=1,4 do
        local filepath = self["soundfile"..tostring(n)]
        if filepath~="" then
            local noise = Sound:Load(filepath)
            if noise~=nil then
                table.insert(self.sound,noise)
                --self.sound[#self.sound+1]=noise
            end
        end
    end
end

function Script:Collision(entity, position, normal, speed)
    if speed>self.threshhold then
        if #self.sound>0 then

            local collisiontype = entity:GetCollisionType()

              if collisiontype==Collision.Prop or collisiontype==Collision.Scene then
                 local t = Time:GetCurrent()

                            if t-ImpactNoiseLastSoundTime>self.maxfrequency then
                                ImpactNoiseLastSoundTime=t
                                local n=math.random(#self.sound)
                                local noise = self.sound[n]
                                self.entity:EmitSound(noise,self.range)

                                --particle1:Play()
                        --self.entity:SetPosition()
                           end      

               end
            
        end
--self.entity:Release()
-- create a missile and set location
--Vec3 = GetPosition(pivotobuz, false)
--local obuz = Prefab:Load("Prefabs/obuz.pfb")
--obuz.script:Start()
--obuz:SetPosition(pivotobuz:GetPosition(false))
--obuz.script:Enable()

if (entity:GetKeyValue("name") == "obuz") then
            spawnPos = self.respawnPoint:GetPosition()
            entity:SetPosition (spawnPos)
              
    
                    
    end

self.entity:SetMass(0)
    end

--spawnPoint = self.respawnPoint:GetPosition()
              --    self.entity:SetPosition (spawnPoint, false)          
                -- self.entity:SetMass(0)
--self.entity:SetParent(self.entity.respawnPoint)

end


function Script:UpdatePhysics()
    
if (self.window:KeyDown(Key.Insert)) then obuzforce = obuzforce + 0.5 end
if (self.window:KeyDown(Key.Delete)) then obuzforce = obuzforce - 0.5 end

self.move = Vec3(0,0,0)


if (self.window:KeyDown(Key.B))  then

self.entity:SetMass(1)

self.entity:SetParent(nil)
self.entity:AddForce(0,0,obuzforce, false)

 
end

end

function Script:PostRender(context)
    context:SetBlendMode(Blend.Alpha)
    

    context:SetBlendMode(1)
    context:SetColor(10,66,110,1)

context:DrawText("Power:", 10,840)
context:DrawText(obuzforce, 110, 840)

end

 

 

So.........i have the physics ..trigger for missile (obuz in my script).....i created a pivot, as respawn point.....anybody can help where is my mistake??? 

 

 

Link to comment
Share on other sites

  1. Please use a code tag for your code.
  2. Can you describe what exactly isn't working? Do you see an error? What behviour are you seeing?
  3. Omit parts of your script that are not relevant for now (make a backup though): for instance the sound and drawing ui. 
  4. So you have this helicopter and by pressing a button, you want to fire a rocket? You want to separate that functionality.
  5. Try something more in this direction. A script for the helicopter and a script for a rocket.

Helicopter

--psuedo
if keypressed then
	local spawnPosition = self.entty:GetPosition(true)
	local obuz = Prefab:Load("Prefabs/obuz.pfb")
	obuz:SetPosition(spawnPosition)
	obuz.script:Start()
end

 

Rocket

function Script:UpdatePhysics()
	self.entity:AddForce(0,0,obuzforce)
end

function Script:Collision(entity, position, normal, speed)
	local collisiontype = entity:GetCollisionType()

	if collisiontype==Collision.Prop or collisiontype==Collision.Scene then
		--Create an explosion emitter here
  	end
end

 

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