[SOLVED] Collision for max 30 pixel size Images


(Bora Kasap) #1
Bullet size: 2px x 2px
Enemy size: 18px x 17px
Frame Rate: 60
Bulletspeed: 500*FP.elapsed
Enemyspeed: 50*FP.elapsed

I have a collision problem because of bullet speed… sometimes bullets passing through enemy because of “enemy size / bullet movement perframe” proportion. So, because of this problem, i want to increase all game’s “update speed” without increasing “render speed”… how can i do that?

and, someone of you said me a method to solve this kind of collision problems before, it was about using a virtual “line” to check bullet collision, but i don’t know what is it, if you suggest it, i can try it too…


(Bora Kasap) #2

I’m gonna try to use for loop to make this for specified functions… i think thats better for performance…


(azrafe7) #3

In your case - fast moving objects - you can use moveBy() on your bullets and set the sweep parameter to true in combination with overriding moveCollideX() and moveCollideY().

Or wisely use World.collideLine() with some adjustments - or roll your own implementation that checks for collisions one pixel (or more) at a time, cause that’s the culprit here. ;D

I don’t have any handy tutorials on this, but maybe this blog post can be of help.

Uh… see this also.


(Bora Kasap) #4

actually, i made it by this way, it works fine… but i’ve looked for methods you said so much… but it is so late now :smiley: if my method makes any problem soon, i’m gonna use methods you said.

so thats what i made

in BULLET override

			if (!removedx){
		for (var i:Number = 0; i < 20; i++) 
		{
		if (!removedx){
		moveBy(Math.cos(targetangle * FP.RAD) * firespeed / 20 * FP.elapsed, Math.sin(targetangle * FP.RAD) * firespeed / 20 * FP.elapsed);
		world.getInstance("playerx").checkCollisions();
		}
		}
		firespeed = firespeed * 0.99;
		}

and in PlayerX entity (i’m using player to check global collisions)

		public function checkCollisions():void
	{
		elist.length = 0;
		pblist.length = 0;
		world.getType("enemy", elist);
		world.getType("pbullet", pblist);

		for each (var e:Enemy in elist)
		{
		if (!e.removedx){
		for each (var pb:pBullet in pblist)
		{
		if (!pb.removedx){

			//BULLET DESTROY
			if (pb.collideWith(e,pb.x,pb.y))
			{
			pb.hit.play();
			var hitanim:Entity = world.add(new HitAnim());
			hitanim.x = pb.x;
			hitanim.y = pb.y;
			pb.removedx = true;
			world.remove(pb);
			
			//ENEMY TAKE DAMAGE
			e.armor -= bulletdamage;
			e.graphicfx.tinting = 0.5;
			e.fxtimer = 3;
			if (e.armor <= 0) { 
				world.remove(e); 
				e.removedx = true;
				}
			
			}
		}
		}
		}
		}
		
		
	}

That works so good on a low netbook hardware if enemy+bullet count no more than 50-60


(Bora Kasap) #5

in here, can i remove for loop and simply make sweep=true on moveBy functions to solve the problem?


(azrafe7) #6

Late here too… but I’m almost sure you’re overcomplicating things here, especially if you’re making your player entity act like a GOD Object (in the sense that it performs tasks that shouldn’t belong to it - like checking for global collisions, which I think could more easily be handled by enemies and bullets directly).

If you could explain in detail (and I’d advise to make a new post for that) what you’re trying to achieve along with the specific problems you’re encountering, then I’m sure the community will be able to help you faster.

Also… don’t forget to fix your code indentation ;D

Read you tomorrow!


(Bora Kasap) #7

i’ve made it like you said first… i’ve used two collision check functions for both entity(bullet & enemy) inside them. but one of these entities wasn’t checking collision because one of them removed first from world.