[SOLVED] Collision returning true but sometimes not


(Bora Kasap) #1

I’m using this code to movement & check collision in Bullet entity but a few bullets in a several going through without hitting enemy… :?

		override public function update():void
	{
		
		x += Math.cos(targetangle * FP.RAD) * firespeed * FP.elapsed;
		y += Math.sin(targetangle * FP.RAD) * firespeed * FP.elapsed;
		firespeed = firespeed * 0.99;
		
		if (collide("enemy", x, y))
			{
			hit.play();
			var hitanim:Entity = world.add(new HitAnim());
			hitanim.x = x;
			hitanim.y = y;
			world.remove(this);
			}
		
		if (! onCamera) { world.remove(this); }
	}

(Bora Kasap) #2

i’think it’s only about firespeed, it’s about a proportion between bullet’s pixel movement per frame and enemy’s pixel size huh… so, is there anyother way to solve this problem without making enemies bigger? and of course without increasing frame rate to decrease bullet speed


(rostok) #3

It looks like you are using collide() for actual new position of the bullet. So if in one frame the distance travelled by the bullet is larger than the size of the entity it will pass through it. I think you should first use moveTo(new_pos) or moveBy(delta_x,delta_y)and then check for collision again. Sorry for this “I think” uncertainty but I always used line segment intersections for this kind of collision tests.


(Bora Kasap) #4

i was just trying it, also then should i check collision like that?

if (moveCollideX || moveCollideY)

(Bora Kasap) #5

whatever, that doesn’t work, i’m going to try a lower version of your method(because enemy is not so much small, not have to scan all pixels in line)…