Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by MobileMark

I've figured out how to detect collisions by using m_userdata in the entity , but I'm still having trouble with consistency

heres the code I'm using
myListenerClass.prototype.Add = function( points ) {
           
            var body1 = points.shape1.GetBody();
            var body2 = points.shape2.GetBody();
            
            var body1val=body1.m_userData!=null?body1.m_userData.indexOf("GHOST||"):null;
            var body2val=body2.m_userData!=null?body2.m_userData.indexOf("GHOST||"):null;
            
            if(body1val>-1&&body2.m_userData==null||body2val>-1&&body1.m_userData==null)
            {
                if(body1val!=null&&body1val>-1)
                    {var valInd=parseInt(body1.m_userData.split("GHOST||")[1])-1;}
                else
                    {var valInd=parseInt(body2.m_userData.split("GHOST||")[1])-1;}
                
                if(ig.game.entities[valInd])
                {    
                
                    if(ig.game.entities[valInd].currentAnim == ig.game.entities[valInd].anims.dead)
                    {}
                    else
                    {ig.game.entities[valInd].currentAnim = ig.game.entities[valInd].anims.dead;}
                
                }
            }
            
        };
    
        var myListener = new myListenerClass(this.body.m_userData);
        ig.world.SetContactListener( myListener );

Basically when the entity hits the ground its animation is supposed to change, but when there are two of the same entity I get an undefined error for ig.game.entities[valInd].currentAnim even though with one entity it works.

I'm very confused as to what the proper way to detect collisions is in Box2D, any help would be greatly appreciated :)

1 decade ago by MobileMark

I found a solution! Though I don't know if it's best practices. I was thinking that all entities were indexed in order of their id. Turns out they aren't :P

Here's my rigged fix

myListenerClass.prototype.Add = function( points ) {
		   
			var body1 = points.shape1.GetBody();
			var body2 = points.shape2.GetBody();
			
			var body1val=body1.m_userData!=null?body1.m_userData.indexOf("GHOST||"):null;
			var body2val=body2.m_userData!=null?body2.m_userData.indexOf("GHOST||"):null;
			
			if(body1val>-1&&body2.m_userData==null||body2val>-1&&body1.m_userData==null)
			{
				if(body1val!=null&&body1val>-1)
				    {var valInd=parseInt(body1.m_userData.split("GHOST||")[1]);}
				else
					{var valInd=parseInt(body2.m_userData.split("GHOST||")[1]);}
				
					    for(var i=0;i<ig.game.entities.length;i++)
						{
							if(ig.game.entities[i].id==valInd)
							{
							   
								if(ig.game.entities[i].currentAnim == ig.game.entities[i].anims.dead)
								{}
								else
								{ig.game.entities[i].currentAnim = ig.game.entities[i].anims.dead;}
								
								break;
							}
						}
					
			}
			
		};
	
		var myListener = new myListenerClass(this.body.m_userData);
		ig.world.SetContactListener( myListener );

Still wondering if this is the best way to go about this or if I'm building a huge bridge for a very small creek.

1 decade ago by MikeH

I've been struggling with this too, and my approach to entity vs entity collisions is to enable the impactjs collisions as normal, and with entity vs world collisions I've been keeping track of velocity in update() and detecting for when a downward velocity suddenly stops.

1 decade ago by MikeH

MobileMark, I have a solution for your hitting the ground problem which doesn't involve registering a listener. In fact, it's done without interacting with Box2D much at all, and still works :)

In your update() method on the entity, you can do this :

if (
this.body.m_linearVelocity.y < 0.00000001 &&
ig.game.collisionMap.getTile(this.pos.x, this.size['y'] + 1 + this.pos.y)
){
// set your animation here
}

So we're checking the linearVelocity and making sure the entity is still. I've found that rarely does the velocity settle at 0, so I check for a suitably small velocity (your gravity and size of entities may dictate you change this value).

Then I check if there's a collision map tile below the entity. Bingo! Using this method I've been able to do wall detection and change direction for entities, implement bounces, exlposions, all quite easily and without having to really get into the internals of Box2D.

1 decade ago by MobileMark

You have made my day! Thanks
Page 1 of 1
« first « previous next › last »