I have a main character (player), he has 2 collisions.
1 his body that collects coins and reacts to enemy touch
2 circle of his perception, if the enemy touches the circle of perception, then he understands its angle, and creates a bullet flying into it
Coin
Has a collision with trigger type. Removed when the player’s body touches it but not the circle of perception
Enemy
Has body collision
Does not react to coins and the circle of perception, but if it touches with a bullet, it is removed. When it dies, it creates a coin.
How it is possible to implement it and not to mix collisions?
Is it possible to get the distance to an object without the help of collisions? when the distance with the enemy is less than 10 - a bullet is created?
Tell me how to implement a bullet hell game.
Does it make sense to use so many collisions?
How to correctly refer to the created object from the object that did not create it?
I use a helper module with some basics maths functions.
For distance between two points :
function dist_xy(x1,y1, x2,y2)
return ((x2-x1)^2+(y2-y1)^2)^0.5
end
Or you can use vmath.length() API reference (vmath)
You just need to calculate the vector between the two positions you’re interested in then get its length.
I’m not sure which one would be the most efficient. If critical it would be better to do some benchmark.
Hope that helps.
I’m not the greatest neither in Defold, nor in Lua.
It doesn’t have to if you don’t want it to! From your original post I’d create the following collision objects and assign them these groups and masks:
Player
Body - Collects coins and reacts to enemy touch
Group: PLAYER
Mask: ENEMY, COIN
Circle of perception - Detects nearby enemies
Group: ENEMY_DETECTOR
Mask: ENEMY
Enemy
Body - For collisions with player and bullets. Does not react to coins and circle of perception.
Group: ENEMY
Mask: PLAYER, BULLET, ENEMY_DETECTOR
In your player collision code you check if a collision happened with the “body” or “circle of perception”:
function on_message(self, message_id, message, sender)
if message_id == hash("contact_point_response") then
if message.other_group == hash("ENEMY") and message.own_group == hash("PLAYER") then
print("Owww! That hurts!")
elseif message.other_group == hash("ENEMY") and message.own_group == hash("ENEMY_DETECTOR") then
print("Enemy nearby! Fire bullet!")
local enemy_pos = go.get_position(message.other_id)
end
end
end