Better method for bullet damage

So I have two methods in mind for a bullet doing damage to an object.

Each bullet having a simple script to add -(somenumber) to the hitObjects health property.

Or the Object having a script that detects what type of bullet is hitting it and takes the appropriate damage.

Which method do you think would be better? Or is there a better third method I haven’t thought of?

if you use a dynamic armature compound assemble (a root object with child shapes that unparent move/rotate and reparent each frame to the core) the objects don’t collide with each other (lower physics cost) and collisions with the child shapes will affect the root.

to find what body part was hit I use

max =root.getDistanceTo(hitPoint)
hit = root
for shape in root.children:
    if D<max:
        D = shape.getDistanceTo(hitPoint)
        hit=shape

hit['health']-=own['Damage']

#hit = who was hit 

     

if the object can affect multiple limbs (explosion)
use sorted, and when the distance is greater than the damage radius I use break.

So your basically suggesting method 2? The object (Or limbs parented to armature) do the logic work in adding the damage to the health property?

yeah, and as a bonus, the collision shape of the actor is accurate.

Alright, thanks! :smiley:

that is a good question!

bullet do damage to hit >> mean … bullet WRITE on another object property
(what is the hit object is a Tank?)

the hit object check alone, mean >> READ the bullet, and WRITE on it self

the second is more correct in my opinion

anyway the OOP has some issue when there conflict with other objects

assuming the case 2 player collide with “ammopick” , who of the 2 player should be grab the ammo? eheh

if i use different types of bullets i let the bullet detect the collision. if it collides with the player, it sends a message and delete itself. the message is recognized by the healthbar in the overlayscene and concerning to message (e.g. light dmg, heavy dmg) it reduces the amount of health. no python necessary. all build with lbs.

i let the bullet detect the collision

This depending on how fast you let a bullet travel is a bad idea.

You can better shoot a ray and see if it hits a target, if hit substract health. (remember normal bullets you dont see them fly, its poof and guy is dead ;). an other way to calculate it would be by again shooting a ray, this time from the bullet but you add the bullets word position + velocity to calculate where it would be the next frame, then when ray hits remove health.

so your are all agree that is better if the bullet remove the health on another object (write on a property of another obj)
this has some advantage, if you have by chance many different enemies , that just “attack or die”

and anyway the “bullet” is a part very central of the game.(so you know perfectly that this is the role)
differently , better avoid to write on other obj when possible… afaik (…few)

for the bullet realistic (350m/s) i use uniquely a ray in a range , if hitobject: added = addObject(“Bullet”, gun,1); added.worldPosition= hitposition, added.setParent(hitobject)

I cast rays forward from the bullet based on speed. meaning you can still have a flight arc, and not miss any collisions.


rayEnd = bullet.worldPosition+(bullet.worldLinearVelocity*multiplier) 

ray = bullet.rayCast(rayEnd,bullet.worldPosition,0,'',0,0,0)
if ray[0]:
    #you_hit_something

As others have said. I wouldnt let the bullet itself do any part of the collision…If you try to shoot your bullets accross the screen and have it detect if it hits something, more than likely, its not going to hit anything even if it does. What if your using a machine gun as well your going to have hundreds of bullets running scripts, besides the bullet(s) is going to move too fast and more than likely go through your object and never detect the collision. This is one thing I know for sure. Make any bullets that you have just be visuals. Use rays off the front of your gun, these will not miss, use maths or dual rays for bullet drops, arcs, winds. Make the bullets your visuals only.

Here is an example of why you dont use the bullets themselves to detect the collision.

badbullet.blend (411 KB)

What about speed? I know the ray instantly hits, but some way to put a delay maybe?

like casting ray in front of the bullet?

you can always add a damage sprite where the ray hits, (the next frame)

Ok, here is the most efficient system I could think of

Attachments

BulletManager.blend (473 KB)

Perfect, exactly what I had in mind. Thanks BPR, :slight_smile:

i have to admit that the idea is goodis a entity system oriented?ie, the bullet even exist.and the “intrusion” (write “health”) is done from a third object "specialized"that surely do all checks necessary…still the need of a centralized object that should not be a gameobject.it should be more an instance or probably a function very easy to access, from a module.import bulletsystembulletsystem.add_bullet(spawner=gun, life=10, speed=350, damage=10)whath about make the position - orientation implicit and axis forward “y”?this remove a lot of argumentsthe list can work to contain all infos , but use “unpack” to make the code more readablerather than L[3]-L[1]

for bullet_data in bullets_data:    position, velocity, life = bullet_data