How to tell sprite collisions apart?

Hi all

Almost total newb here asking what may be a foolish question:

I’ve been working on turning the Side Scroller Tutorial into a top-down vertical scroller, like the 194x series, or dozens of other games I can’t remember the names of.

I’ve got (blocky) clouds floating by, and I’ve turned the stars into two different types of in-game objects. The yellow stars add score, the green ones take away score. In a ‘final’ game the green stars will be replaced by enemy craft that shoot at the player and attempt to crash into the player, costing a life.

I’ve added bullets that fly upward from the player’s plane, basically bullets are just like the stars, just going in the opposite direction.

I want the bullets to destroy the stars if they collide. How do I do that?

The two star scripts currently look like this (pretty much what they were in original)

lua 
local speed = -120
local score = -500

function update(self, dt)
local p = go.get_position()
p.y = p.y + speed * dt
if p.y < -32 then
go.delete()
end
go.set_position(p)
end

function on_message(self, message_id,  message, sender)
if message_id == hash(“collision_response”) then
msg.post(“main#gui”, “add_score”, {amount = score})
go.delete()
end
end

I’m guessing there is something needed to be more precise in the line:

if message_id == hash(“collision_response”) then

My experience with C/C++ suggests that this line would feed into a for loop checking the coords of each star verses each bullet.

My immediate wish is to set things up that collisions between bullets and the green stars add to the score but collisions between the player’s plane and green stars take away points.

Any advice or wisdom is welcome.

Thank you all.

REgards

Frank

The collision between objects is detected by the physics engine and you’re notified about it in the on_message function. There you can verify the group of the colliding object(other_group) and this way you can know the kind of object you collided with.

Hi all,

I’ve explored the options offered and I have had some success.

But my enemy stars are stubbornly refusing to cooperate.

They will be deleted if they collide with either a bullet or the player’s plane but the score manipulation does not happen when they collide with the bullets.

The issue (I think) is that the bullets are ‘manufactured’ by the bullet factory which seems to give them a message.other_id value of /instanceX where X is a number.

The bad_star collision (also produced by a factory) yield similar results.

I use print statements to dump the following to the console:

DEBUG:SCRIPT: bullet collision with : hash: [/instance3]
DEBUG:SCRIPT: star collision with : hash: [/instance4]
DEBUG:SCRIPT: bullet collision with : hash: [/instance5]
DEBUG:SCRIPT: bad star collision with : hash: [/instance6]
DEBUG:SCRIPT: bullet collision with : hash: [/instance7]
DEBUG:SCRIPT: bad star collision with : hash: [/instance9]
DEBUG:SCRIPT: bullet collision with : hash: [/instance10]
DEBUG:SCRIPT: star collision with : hash: [/instance12]
DEBUG:SCRIPT: bad star collision with : hash: [/plane]

For the plane to bad_star collision the only ID taken into account is the plane and the bad_star colliding with it.

The other stars are working fine.

how do I get around the [/instanceX] issue?

Any help will be greatly appreciated.

Thank you to all.

Regards

Frank

IDs don’t matter, you have to base your logic on collision groups.

You must have a group for enemy stars and mask it for bullets and have a groups for bullets and mask it for enemy stars. This way you make sure your bullets can collide with enemy stars. In the script you check for this scenario by verifying the groups of the colliding objects and take the appropriate action(modify score etc.).

Another thing that’s relevant is the type of message you can receive based on the type of collision objects(dynamic, static etc .) and other settings. Make sure you’re checking the right message.

Hi all and a-daniel in particular,

Currently my bullets are masked as “star, bad_stars” . Is the comma doing something wrong?

The bad_stars are masked to “plane, bullets”.

My not-working properly yet on_message is as follows:

lua
function on_message(self, message_id,  message, sender)
	if message_id == hash("collision_response") then
		print("bad star collision with :  ", message.other_id)
		if message.other_id == hash("/bullets") then
			print("bullet ")
			msg.post("main#gui", "add_score", {amount = (-score)})
		elseif message.other_id == hash("/plane") then 
			print("plane ")
			msg.post("main#gui", "add_score", {amount = score})
		end
		go.delete()
	end
end

The print statements are just there to give me an idea of what is going on, and going wrong.

The yellow stars are working fine because they only have one collision situation, or rather one collision message covers all possibilitiies.

I’m unclear of what I need to change to get this to work.

Any help greatly appreciated.

REgards

Frank

Print message.other_id to verify what is coming in. I don’t think you’re looking for “/bullets” and “/plane”, it will be “bullets” and “plane” without the forward slash.

Hi @FrankScrooby !

I believe you may also want to check the other_group/own_group in the collision message, to determin what type of collision it is:

Hi all and Mathias in particular,

OMG! it WORKS, it really WORKS!

Checking other_group to sort out the difference between plane and bullets does the trick rather nicely.

Thank you to every one who helped me overcome this issue.

Regards

Frank