Jump to content
Braqoon

Resolved - Sprite collision not always works

Recommended Posts

So I have bullets which are sprites. Bullets are fired from a ship towards enemies and when bullet triggers a collision, enemy will get damaged or destroyed. That all works in principle but only when sprite will collide with vertex on collision mesh of the enemy. That results very often that bullet goes trough the object and don't trigger a collision.

 

My bullet creation script:

function Script:GetBullet(position)
local bullet = Sprite:Create()
bullet:SetPosition(Vec3(position.x,position.y,position.z+3))
--bullet:SetPosition(position)
bullet:SetViewMode(2)
bullet:SetMass(0)
bullet:SetGravityMode(false)
bullet.origin = Vec3(position.x,position.y,position.z)
local mat = Material:Load("Materials/Bullets/bullet1.mat")
if mat then
bullet:SetMaterial(mat)
mat:Release()
end
return bullet
end

 

And my collision detection:

function Script:UpdateWorld()
local bullet,n,dist
local pickinfo=PickInfo()
local travel
local bspeed=25
local window = Window:GetCurrent()
for n,bullet in ipairs(self.bullets) do

travel = bspeed/60.0*Time:GetSpeed()

if self.entity.world:Pick(bullet.position,bullet.position+travel,pickinfo,0,true,Collision.Projectile) then
local enemy = self:FindScriptedParent(pickinfo.entity,"Hurt")
table.remove(self.bullets,n)
bullet:Release()
bullet=nil
if enemy~=nil then
 if enemy.script.health>0 then
 enemy.script:Hurt(self.bulletdamage,self.player)
 end
end
else
if bullet ~=nill then
 dist = (bullet.position-bullet.origin):Length()
travel = bspeed/60.0*Time:GetSpeed()
bullet.position = bullet.position+Vec3(0,0,travel)
bullet:SetPosition(bullet.position)

if dist>self.bulletrange then
table.remove(self.bullets,n)
bullet:Release()
bullet=nil
end
end
end
end

 

This is largely based on FPSGun.lua

Am I missing something ? I'm 99% sure that my sprite bullets only trigger a collision with collision hull vertexes which renders whole idea of using sprites as bullets not usable. Don't think that creating collision hull with big number of vertexes is a good solution.Even Convex Hull type mesh is not 'accurate' enough. Is there a way can I check if bullet is 'inside' of the other object which will trigger a collision ?

 

Thanks

Share this post


Link to post

I only took a brief look at this, but I noticed some things that could be the problem.

1. Collision.Projectile only collides with scene and prop

2. bullet.position + travel... A Vec3 + float doesn't get the forward direction as far as I know.

I may be wrong, but these are some suggestions.

Share this post


Link to post

I only took a brief look at this, but I noticed some things that could be the problem.

1. Collision.Projectile only collides with scene and prop

2. bullet.position + travel... A Vec3 + float doesn't get the forward direction as far as I know.

I may be wrong, but these are some suggestions.

 

1. This is set correctly. Bullet does hit and collision get's triggered, but like I have mentioned only when sprite hit's vertex on collision mesh of the target.

2.

bullet.position = bullet.position+Vec3(0,0,travel)

That works no problem. This is done after collision check and it's needed to move the bullet forward.

Share this post


Link to post

I think Brutile is referring to this line:

if self.entity.world:Pick(bullet.position,bullet.position+travel,pickinfo,0,true,Collision.Projectile) then

 

A vec3 + a float just adds the float to all the components of the vec3

  • Upvote 1

Share this post


Link to post

I think Brutile is referring to this line:

 

 

A vec3 + a float just adds the float to all the components of the vec3

 

Well that's just copy & paste from FPSGun.lua script. I have not altered this.

Share this post


Link to post

Well that's just copy & paste from FPSGun.lua script. I have not altered this.

 

It is possible that Josh has made the Vec3 operators (+, -, *, etc) work this way. It just didn't look right to me. You can always double check by printing the original position, then printing the new position.

  • Upvote 1

Share this post


Link to post

Well that's just copy & paste from FPSGun.lua script. I have not altered this.

 

thats what i thought as well at first... but the FPSGun script sets the travel variable as a Vec3:

travel = bullet.velocity/60.0*Time:GetSpeed()

if self.entity.world:Pick(bullet.position,bullet.position+travel,pickinfo,0,true,Collision.Projectile) then

where bullet.velocity is a vec3 value.

 

whereas you are just calculating 'travel' as a float:

local bspeed=25

...

...

travel = bspeed/60.0*Time:GetSpeed()

if self.entity.world:Pick(bullet.position,bullet.position+travel,pickinfo,0,true,Collision.Projectile) then

 

It is possible that Josh has made the Vec3 operators (+, -, *, etc) work this way. It just didn't look right to me. You can always double check by printing the original position, then printing the new position.

No it works as one would expect.

a = Vec3(1,1,1)

b = 3

c = a + b

System:Print( c )

will display 4,4,4 in the console

  • Upvote 3

Share this post


Link to post

OK issue resolved. As pointed out correctly travel var was here to blame, changing it to Vec3 sort's the problem out.

 

if self.entity.world:Pick(bullet.position,bullet.position+Vec3(0,0,travel),pickinfo,0,true,Collision.Projectile) then

 

Thanks for pointing it out.

Share this post


Link to post

Are you sure this works properly?

It still doesn't look right to me. Isn't the pick just picking forwards in the global Z direction?

Does it still work if you shoot from a different angle?

 

The original was using the bullet velocity, which should be the global direction of the bullet, not just forward in global space if that makes sense.

  • Upvote 1

Share this post


Link to post

So bullet only can travel forward in my game and cannot be fired from an angle so it travels only forward (on one vector) with constant speed until hit or end of life. I have tested so far on only one model and collision so far works as it should.

Share this post


Link to post

Oh, so you're making a shmup? You don't need to worry about it then. Just keep what I said in mind though in case you ever reuse that code.

  • Upvote 1

Share this post


Link to post

Oh, so you're making a shmup? You don't need to worry about it then. Just keep what I said in mind though in case you ever reuse that code.

 

Yes, I understand how it work, thanks.

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