Collision problems [solved]


(John Andersson) #1

Okay. So I have a character who you can attack with. The attack is an explosion which is spawned on your character). I don’t want the character you play as to get hurt, but I want other characters of the same entity type to get hurt.

The explosion never hurts your player thanks to the code I wrote, and it does hurt other players, but only if the explosion doesn’t touch you at all! (I tried this by displacing the explosion). Oh, I also added (alreadyHitThis) to prevent the explosion from damaging every entity continuously.

The code I use is this:

Variable of importance:

public var creator:Player;

In the update:

	var collide_p:Player = collide("player", x, y) as Player;
		if (collide_p && collide_p.player != creator && collide_p != alreadyHitThis)
		{
			collide_p.takeDamage(damage, creator);
			alreadyHitThis= collide_p;
		}

The player who spawns the explosion sets itself as the creator of the explosion.

Now, my theory is that if both the explosion hits you and your enemy, collide_p does in fact = player, which prevents the function from running… But I literally have no idea how to fix this logically…


(Martí Angelats i Ribera) #2

I feel like the entire project isn’t well structured. You are having a lot of trouble doing thinks that should be obvoius. In this case (like some of the others) is almost impossible to know what to do becouse we don’t have the code to work with.

If you want i can take a look at your entire project (just send me a PM).


(John Andersson) #3

Hmmm. I’d really like to send it but I’m too scared :stuck_out_tongue: I have bad memories of getting things stolen (no disrespect, seriously).

Let me explain the code in further detail.

This code:

var collide_p:Player = collide("player", x, y) as Player;
if (collide_p && collide_p.player != player && collide_p != alreadyHitThis)
{
    collide_p.takeDamage(damage, +player);
    alreadyHitThis= collide_p;
}

needs no explanation

var collide_p:Player = collide("player", x, y) as Player;

If (collide with a player && the collided player isn’t the player/creator that created this attack && didn’t already hit this collided player)

if (collide_p && collide_p.player != creator && collide_p != alreadyHitThis)

Deal damage to the collided player with the parameters(damage, creator), creator is used to track who attack who. Set alreadyHitThis to the player damaged by this explosion, so the same explosion doesn’t hurt the same player more than once

collide_p.takeDamage(damage, creator);
alreadyHitThis= collide_p;

(Martí Angelats i Ribera) #4

I understand that you are trying to detect the collision from an explosion to a single or multiple players / entities.

I only need to know this:

  1. Do the explosion damage multiple Entities (multiple Players or multiple Entity types)?
  2. Can more than a single explosion damage the same Player / Entity?
  3. Is the explosion calculed mathematically or using a mask?
  4. Does this explosions have a delay before dealing the damage?

PS: If you don’t want to show me your project it’s alraight. It’s the fastest and simplest way but I understand that someone may stole the idea.


(John Andersson) #5
  1. Yes the explosion can damage as many entities (Players only) as possible
  2. More than a single explosion can damage the same player entity. It’s just that explosion A can’t damage player B more than once (since the explosion is a few frames, that would mean player B would be hurt around 10 times from one explosion). But it can damage player a, b, c, d etc at the same time
  3. The explosion is simply a fast animated entity with a hitbox
  4. There is no delay to the explosion

Thank you for understanding. I know you wouldn’t do it as you’re a great asset to this community (I’ve seen you here a lot, helping people, including me), but I’d rather be overly sure than overly trusting :stuck_out_tongue:


(Martí Angelats i Ribera) #6

Here you have two options:

  1. Detecting the collision from the player
  2. Detecting the collision from the explosion

(Choose one to continue XD)

PS: I feel like collideInto is the function that you are looking for.


(John Andersson) #7

Right, I am currently detecting the collision inside the explosions code… I could check for it in the players code but I’d like to learn how to do it from inside the explosion :stuck_out_tongue:

What is the difference between collideinto and collide?


(Jean) #8

collide gets the first Entity you collided, and collideInto fills an array with all the Entity you have collided.

So, if your explosion can hit more than one player, you’ll surely use collideInto.


(Martí Angelats i Ribera) #9

Ok so the schematic of what you want to do is:

  1. Get the array of all the players that the explosion is colliding into.
  2. For each of them, check that they are not the one that created the explosion (use the strict equality [===] to check this).
  3. If collided apply the damage.

In code it would look something like this:

public var creator:Player; // this is set at the contructor

override public function update():void
{
	var collided:Array;
	var temp:Player;
	
	collideInto("player", x, y, collided); //get the colliding players
	
	for each (var e:Entity in collided)
	{
		temp = (e as Player);
		
		//strict inequality (even if all the parameters are the same, if it's
		//  not the same player this will trigger).
		if (temp !== creator) 
		{
			temp.takeDamage(damage, creator); //apply the damage
		}
	}
}

I hope this is what you were looking for.


(Jean) #10

The only thing that this is missing is a control since @John_Andersson1 said that the explosion stays there for a few frames.

Your code needs a bit of changes like:

  • Create a private var alreadyHit:Array = new Array();
  • After the line on temp.takeDamage, add this line: alreadyHit.push(temp);
  • On the if (temp !== creator), make it if((temp !== creator) && (alreadyHit.indexOf(temp) == -1))

This should avoid multiple hits.


(John Andersson) #11

Thank you both. Excellent help!! :slight_smile:


(Martí Angelats i Ribera) #12

@DarkHyudrA Oh my bad. Forgot about it. Thanks for pointing it puy.

@John_Andersson1 Add a “[Solved]” to the title of a solved help thread. It’s easier to identify the solved ones :wink: