How do I stop my laser from destroying two items with one shot? (game design)

  • Thread starter Thread starter Darkmisc
  • Start date Start date
  • Tags Tags
    Collision Laser
AI Thread Summary
The discussion revolves around a game development issue where a player's laser is intended to destroy only one target at a time but sometimes destroys multiple targets due to overlapping collision shapes of enemies and enemy lasers. The player laser is programmed to execute a queue_free() command upon entering the collision shape of either an enemy or an enemy laser. However, when these collision shapes overlap closely, the laser can destroy both simultaneously.Attempts to resolve the issue included modifying the instancing of the laser and implementing a variable to track whether the laser has already been used. Suggestions were made to consider the dual destruction as a feature, potentially reducing the laser's strength after hitting the first target or introducing shields that could deflect or reflect the laser. The conversation also touched on the mechanics of collision detection and the need for precise timing to prevent the laser from hitting multiple targets. Despite these efforts, the problem persists, indicating that the collision detection logic may need further refinement to ensure the laser only destroys one target per shot.
Darkmisc
Messages
222
Reaction score
31
TL;DR Summary
When the collision shapes for two on screen items overlap, the player's laser can sometimes destroy both items with one shot. The laser is only supposed to destroy one item per shot.
Hi everyone

I'm making a game in which a player's laser can destroy enemies and cancel out enemy lasers. My code for the player laser executes queue_free() when it enters the collisionshape for the enemy laser and enemy. The player laser is only supposed to destroy one item per shot.

Sometimes, the collision shapes for the enemies and enemy lasers overlap while they are very close to the player. If the player fires the laser at this moment, the laser will destroy both items.

At first, I thought the problem was that the player laser instances inside the collision shapes of the enemy and the enemy laser. This would eliminate both collision shapes with one shot.

I tried fixing this with the following code [from enemy_laser scene]:

[CODE title="enemy_laser"]func _on_enemy_laser_area_entered(area):
if area.is_in_group("enemy"):
ENEMYPOS = Global.enemies_pos
share_space = true

if area.is_in_group("laser"):
if share_space:
if position.x<= Global.enemy_laser_pos:
print("laserdestroyedpos=" + str(position))
Global.enemy_laser_pos=1280
$Sprite.visible=false
$CollisionShape2D.set_deferred("disabled", true)

if share_space == false:
Global.enemy_laser_pos=1280
queue_free()[/CODE]It's supposed to detect when the enemy and enemy laser overlap. When this happens share_space == true.
If the laser enters while share_space==true, the laser is supposed to queue_free the element that is closest to the left of screen (I have corresponding code for this in the enemy scene).

This code didn't fix the problem.

Then I thought if I instanced the player laser further to the left (from within the player), it won't instance into the overlapping collision shapes. Instead, it should only enter a collision shape as the laser moves from left to right. However, the laser will still sometimes destroy two items with one shot (when fired close to the items).

Does anyone know how to fix this problem?

Thanks
 
Last edited by a moderator:
Technology news on Phys.org
It's a feature!
 
  • Like
  • Haha
Likes Darkmisc and jedishrfu
Give the laser_shot object a tag that says if it has been used.
 
  • Like
  • Love
Likes Darkmisc and Tom.G
As dave said why not consider it a feature. Perhaps you could reduce the strength of the laser after it hits the first object so that its not as strong when it hits the next one.

Now you could add shields that deflect some or all of the laser light and allow for reflection hits and all sorts of unpredictable things.

I remember playing laser tag once (corporate team building exercise) and realized you could shoot at the mirrors in the room to get a player and I quickly racked up points until others caught on. I may have shared my secret in the interests of fair play then again maybe not.
 
Baluncore said:
Give the laser_shot object a tag that says if it has been used.
Do you mean something like a global variable used = true/false?
 
jedishrfu said:
As dave said why not consider it a feature. Perhaps you could reduce the strength of the laser after it hits the first object so that its not as strong when it hits the next one.

Now you could add shields that deflect some or all of the laser light and allow for reflection hits and all sorts of unpredictable things.

I remember playing laser tag once (corporate team building exercise) and realized you could shoot at the mirrors in the room to get a player and I quickly racked up points until others caught on. I may have shared my secret in the interests of fair play then again maybe not.
Coincidentally, I have mirrors in my game too. I need the lasers fired to match the number of items destroyed because if they don't, the extra laser will reflect off a mirror and kill the player.
 
Doesn't that make the game more exciting?

Its like the scene in The Hunt for Red October where the Russian captain of the hunter sub shoots a torpedo but removes some distance limit allowing the torpedo to miss Red October and circle around and kill his own sub.
 
I think so. It stops the player from spamming lasers and forces them to be careful.
 
Darkmisc said:
Do you mean something like a global variable used = true/false?
It could be global, but I thought a laser would be an object with parameterised performance, activity status and variables such as location.x and .y, and the direction of fire. That way, you can have an array of lasers and an array of enemies operating at the same time. If the direction of fire was a unit vector, then collision detection between the enemy and the laser beam could be done very quickly by subtracting vectors as complex numbers and comparing ratios.
 
  • #10
How come the laser doesn't get destroyed when it hits the first target? I mean, what prevents the laser from hitting a second enemy or laser, and why isn't it working when "they are close together"?
 
  • #11
jack action said:
How come the laser doesn't get destroyed when it hits the first target? I mean, what prevents the laser from hitting a second enemy or laser, and why isn't it working when "they are close together"?
I'm not sure. The laser should queue_free upon entering the collision shape of an enemy or enemy laser.
When I instanced the laser in front of the player, there was a chance that it would appear inside the overlapping collision shapes of the enemy and enemy laser. It made sense that the player laser might destroy both items before it queued free.

Now, I'm instancing the laser within the player. It should have to travel left to right before it hits something. In theory, it should only hit one item at a time, unless there is a precise moment when the enemy and enemy laser collision shapes perfectly overlap, and the player laser strikes at this moment. However, the player laser is destroying two items with one shot too often for me to think that this is the cause.
 
Back
Top