Bullet hitting an enemy (SOLVED)

I noticed in my space game that when the bullet hits an alien that the explosion doesnt occur at the point where it hits but in the middle of the bullet.

The bullets are quite long so it looks like the explosion happens in front of the enemy.

My current method to get over this is to find the end of the bullet by taking the direction and adding half of the length of the bullet.

if(message.other_group == hashes.ASTEROID) or (message.other_group == hashes.ALIEN) then
		local p = go.get_position()
		p.x = p.x + (math.cos (self.direction.x)* go.get("#sprite", "size.x") * go.get_scale("#sprite").x)/2
		p.y = p.y + (math.sin (self.direction.y)* go.get("#sprite", "size.y") * go.get_scale("#sprite").y)/2
		singleExplode(p)
		go.delete(true)

That works … but it seems quite an expensive way to do this. Has anyone a simple method ?

Make sure you are looking for the message hash("contact_point_response") and not hash("collision_response") if you want the position to be of the point of contact.

function on_message(self, message_id, message, sender)
	if message_id == hash("contact_point_response") then
		collectionfactory.create("/hit#collectionfactory", message.position)
		go.delete()
	end
end

BulletHitExplosion.zip (4.9 KB)

contact_point

There is also immediate collision respond for doing raycasts so if you have super fast moving bullets that may pass through geometry you will want to do a raycast for the distance they are traveling in that frame.

Here is a version with a fast bullet / raycast.

BulletHitExplosion_raycast.zip (5.1 KB)

Another thing to remember about bullets is if you have a ton of them you’ll want to manage them from a central script, not have them all have their own scripts as done in these examples for simplicity.

6 Likes

Thankyou… what a complete answer. :smile:

Most appreciated !

2 Likes