Jump to content
X54321

[Leadwerks 3 Indie Edition] Projectile collision

Recommended Posts

Well here we are again. It's always such a pleasure. So, I have these turrets that shoot out missiles. They do as they're supposed to and sail in the direction they are shot. HOWEVER, it doesn't seem to collide with anything and only collides with the player if the player is moving. I have the collision type for the missile set to 'Prop'. I have tried the 'Projectile' collision type, and it just makes it not collide at all. I have tried detecting collisions from both the player's script and the missile's script. The missile also has Swept collision enabled, a mass of 0, and gravity turned off. I am using Move(x, y, z) to move the missile within the UpdateWorld() function.

Share this post


Link to post

You must put a mass > 0 on missile for it to be a physic objects and have collision working, and yes this is Prop you need to use.

You don't need WeptCollision.

For collision between missile and player i recommend that you use SetType at start and getType in Collision function.

 

How it is done on bullet.lua :

 

f

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

   self:stopbullet()

   local typeEnt = entity:GetKeyValue("type", "")
   if typeEnt == "alien" then

           entity.script:decHealth(1)
           --self.hearts = self.hearts  -1
   end

end

 

So you must set the type on Alien entity at start function like that :

 

function Script:Start()
   --self.entity:SetFriction(0,0)
   self.entity:SetKeyValue("type","alien")

   self.state=self.alive

   self.baseAnimMgr = AnimationManager:Create(self.entity)


   for x=0,App.world:CountEntities()-1 do
       local entity = App.world:GetEntity(x)
       if entity:GetKeyValue("type") == "player" then
           self.player = entity
       end
   end


end

 

Works same for player or other entity, you must work with SetType at start and GetType on Collison(..) function to just take account the entities type you need.

 

-------------------------

 

You can read code for bullet from this tutorials where it shoots missiles :

http://www.leadwerks.com/werkspace/files/file/504-turret-and-missile-scripts/

 

This one don't use projectile but Raycast collision if you also would need it later :

http://www.leadwerks.com/werkspace/files/file/492-bombkiller/

 

------------

 

Tell me if you need it , i could also upload a complete working project with player firing bullets, ennemies, and healt energy pickups ?

Share this post


Link to post

Works same for player or other entity, you must work with SetType at start and GetType on Collison(..) function to just take account the entities type you need.

 

I have already been doing this.

 

I have adjusted the physics of the missile so now it at least collides with the walls & such. HOWEVER, it still won't collide with the player unless the player is moving. If the player stands perfectly still the missile will go right through him.

Share this post


Link to post

I have bullets colliding with my player in example code.

 

Have you set your player Type in Player start function ?

In bullet collision function do you check type and see if it is player ?

Can you post your player and Bullet Physics settings and their Start / Collision code ?

 

It should work.

Share this post


Link to post

Yes, I have set the type for my player in the code. But, the bullet is programmed to remove itself if it collides with anything other than an enemy.

 

Here's some code for ya :

 

function Script:Collision(entity, position, normal, speed)
if entity:GetKeyValue("type") ~= "enemy" then
 if entity:GetKeyValue("type") == "player" then
  entity:SetKeyValue("hit", "missile")
 end
 self.dead = true
end
end

 

function Script:Start()

self.entity:SetGravityMode(false)
self.entity:SetMass(1)
self.entity:SetKeyValue("type", "missile")
self.entity:SetCollisionType(Collision.Prop)
--self.entity:SetSweptCollisionMode(true)
self.dead = false
end

 

function Script:UpdateWorld()
self.entity:Move(-0.25, 0, 0)
if self.dead then self.entity:Release() end
end

 

Anyways, the missile's physics properties are defined in the start code so I don't have to modify the prefab. It's a rigid body, and has a box for collision.

Share this post


Link to post

You should not do this : never change entity type :

if entity:GetKeyValue("type") == "player" then
  entity:SetKeyValue("hit", "missile")
 end

 

 

Have you looked of the turret link example i posted ?

It has bullets and player code.

When you fire a missile, just load a prefab missible entity containing script and putting its type to missile

In it's collision function just look if entity collided type = player

If yes, decrease player health, and Release missile entity.

 

If you post a small minimum player/missile code i can dowload and test in LE3 i will debug it, but it seems it's coming from your code dealing with type not a physc problem settings.

Share this post


Link to post

You should not do this : never change entity type :

if entity:GetKeyValue("type") == "player" then
entity:SetKeyValue("hit", "missile")
end

 

 

Have you looked of the turret link example i posted ?

It has bullets and player code.

When you fire a missile, just load a prefab missible entity containing script and putting its type to missile

In it's collision function just look if entity collided type = player

If yes, decrease player health, and Release missile entity.

 

If you post a small minimum player/missile code i can dowload and test in LE3 i will debug it, but it seems it's coming from your code dealing with type not a physc problem settings.

 

I wasn't changing the entity type. It's rather complicated and messy. I made this code when I was testing to see if it would work if I detected collisions from the missile instead of the player. The

entity:SetKeyValue("hit", "missile")

is actually a sort of relay. This was before I found out you could directly access an entities' script. I will post some more code shortly, I need to eat lunch.

Share this post


Link to post

I copied the code exactly into my game; It just crashes. No error. No warning. No message. It just crashes.

Share this post


Link to post

Do you understand it's Lua code ? Perhaps you need to first learn the Lua basics and how all works ?

All necessary code is on the Turret example i posted.

 

I can help you, so i just need something simple :

Post in a Zip file :

- A simple turret and script your use

- The missile and script for it

- The player ans script it uses

I'll take that modify it in a very simple working solution, try that.

 

Because if i post even a complete working example, i'm not sure you should able to re use it ?

Share this post


Link to post

Do you understand it's Lua code ? Perhaps you need to first learn the Lua basics and how all works ?

All necessary code is on the Turret example i posted.

 

I can help you, so i just need something simple :

Post in a Zip file :

- A simple turret and script your use

- The missile and script for it

- The player ans script it uses

I'll take that modify it in a very simple working solution, try that.

 

Because if i post even a complete working example, i'm not sure you should able to re use it ?

 

Wow, that was mildly insulting. Good thing I already had the zip file prepared. https://drive.google.com/file/d/0BzyMN5S2kRTHb1VQOWRGQ0J1VVk/edit?usp=sharing

Share this post


Link to post

Ok, i didn't wanted that sorry unsure.png

 

I can help you and make your code work, but i need a level map from you.

All i told you and example code in on the turret example , it has all you need :

Bullet.lua and turret.lua

Bullet.lua just need a position and a direction to fire a bullet, and the bullet prefab containing : model bullet and it's attached Lua script, all is in that example, i can't do more if you don't post a map i could correct.

Share this post


Link to post

Bah, it's ok. I'm just in a bad mood. Well, I'm just going to post the entire project once it's done uploading. It'll be easier to set up I guess.

Share this post


Link to post

ok i see the problem :

You took my bullet code that has lot of specific code.

For example fixed values like Y=2 , this was for my level with a fixed Y floor height on all level.

It's not protable code.

My code was just to inspire people, but ok, i think i'll have to re upload the turret scripts and make a very generic that will work with other situations withiut fixed values like that.

 

I'm correcting your code to be more general smile.png

Share this post


Link to post

if self.entity:GetKeyValue("hit") == "missile" then
       self.entity:SetKeyValue("hit", "nothing")
       self.health = self.health - 25
   end

 

As advice, don't change Key vakues, they should be used to "design" once for all the type of entity.

type Player always remain Player

type missile always remain missile

etc ...

The type belongs to entity, to recogniez the type when colliding, never change it.

 

For player hit or not , use a boolean, and use type== missile on Collision function.

Share this post


Link to post

Some example :

 

if pick.entity ~= nil then
if pick.entity:GetKeyValue("type") == "enemy" then
pick.entity:SetKeyValue("hurt", "true")
end
end

 

Instead of that , use a boolean variable named hurt that belong to ennemy script.

 

ennemy.lua :

 

Script.hurt = false

 

Player.lua :

 

if pick.entity ~= nil then
           if pick.entity  == "enemy" then
               pick.entity.script.hurt = true
           end
       end

Share this post


Link to post

Your turret pivot faces the wall :

If you launch it in that direction it will just collide with all

If you make it in backward direction it will collide with robot

pivot.jpg

So you need to change the place of Pivot named "fire" or rotate your turret 180°

Share this post


Link to post

Thanks, I copied your code because my implementation of it wasn't doing anything. I knew the turret faced the wall, I just wanted to get the code working before I worried about it.

Share this post


Link to post

Your problem seems the missile, i couldn't step in in Debug so i just placed player in front of turret and the problem is that missile has first some orientation problem :

problem.jpg

Share this post


Link to post

Ok i solved the crashing problem :

A missile or bullet must have a MAss > 0 , if not physicSetposition function will crash when called.

And you must choose a Physic shape collider for your missile in Physic tab.

 

Your settings for missile in Physic view mode :

physic1.jpg

 

After correction (working now) before saving missile and script as Prefab to replace old wrong prefab :

Read carefully what is on Physic Tab :

physic2.jpg

So never forget to click on button "Fit Shape" also once you selected "Prop" for a missile.

 

There is some code problem, but i'll give you the corrected Lua files.

Share this post


Link to post

Man, I could have sworn I had did that. Anyways, right now I'm going to mess with my implementation (I have it commented out after your code).

Share this post


Link to post

Here is the working scripts.

One thing you could do later is make missile script independant of frame rate using in the equation some Time:GetSpeed().

Keep learning.

 

Tell me if it works also happy.png

 

Yes, don't take my code as portable script , just learn from it.

(I will post some better clean and portable version of the turret example)

TurretScript.lua

TurretScript.lua

Share this post


Link to post

Well, the script worked, but it did absolutely nothing to solve the problem. I already had missiles that did exactly what I wanted them to do. They just happened to go through the player if he stood perfectly still. With this new script, the missile still goes through the player if he stood perfectly still. So, this didn't solve the problem at all, but thanks anyways.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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.

×
×
  • Create New...