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 Silent

Hello.

I have immpbile NPCs that I don't want my player to pass through. How can I make the NPCs behave like solid blocks?

My original solution was using collision tiles, but I want to interact with them when I touch them and press space. If I use collision tiles, I can never touch the NPCs.

Can someone please help?


Thanks in advance.

1 decade ago by TommyBs

Hi,

Take a look at http://impactjs.com/documentation/class-reference/entity#collides

I think you'd want to set your NPC entitty's collide property to FIXED

More info is available here http://impactjs.com/documentation/collision

1 decade ago by Silent

I read both pages before and none of them helped me. I tried various combinations, including fixed NPCs and lite/active player. I am still able to pass through the NPCs.

1 decade ago by dominic

FIXED NPCs and ACTIVE players should actually work as you want.

Can you walk straight through, as if the NPCs were air? How do you move your player around? Do you set the .vel.x/y or .accel.x/y (you shouldn&039;t set #.pos.x/y directly).

Show some code, maybe :)

1 decade ago by Silent

Oh right! Sorry. Here's my code.

I have 3 NPCs. Two I am able to walk straight through, and one is a little buggy. If I collide from bottom up sometimes it stops me and sometimes it doesn't. It I walk from top to bottom it would sometimes walk through, sometimes teleport me to right under the NPC. Horizontally, I am able to walk straight through.

ig.module(
	'game.entities.player'
)
.requires(
	'impact.entity'
)
.defines( function () {

	EntityPlayer = ig.Entity.extend({

		size: {x: 8, y: 8},
		
		type: ig.Entity.TYPE.A,
		checkAgainst: ig.Entity.TYPE.B,
		collides: ig.Entity.COLLIDES.LITE,

		maxVel: {x: 60, y: 60},
		friction: {x: 400, y: 400},

		animSheet: new ig.AnimationSheet('media/lofi_chars_raw.png', 8, 8),


		idx: 232,
		flip: false,

		
		init: function (x, y, settings) {
			this.parent(x, y, settings);

			if(typeof this.idx !== "number") {
				this.idx = 0;
			}
			this.addAnim('default', 1, [this.idx]);

			if(typeof this.flip !== "boolean") {
				if(this.flip === "true") {
					this.flip = true;
				} else {
					this.flip = false;
				}
			}
			this.currentAnim.flip.x = this.flip;
		},

		update: function () {
			this.parent();

			var accel = 300;

			if(ig.input.state('left')) {
				this.accel.x = -accel;
				this.flip = true;
			} else if(ig.input.state('right')) {
				this.accel.x = accel;
				this.flip = false;
			} else {
				this.accel.x = 0;
			}
			this.currentAnim.flip.x = this.flip;


			if(ig.input.state('up')) {
				this.accel.y = -accel;
			} else if(ig.input.state('down')) {
				this.accel.y = accel;
			} else {
				this.accel.y = 0;
			}
		}
	});
});

ig.module(
	'game.entities.npc'
)
.requires(
	'game.entities.player'
)
.defines( function () {

	EntityNpc = EntityPlayer.extend({
		
		type: ig.Entity.TYPE.B,
		checkAgainst: ig.Entity.TYPE.NONE,
		collides: ig.Entity.COLLIDES.FIXED,

		idx: 0,

		update: function () {}
	});
});

1 decade ago by TommyBs

Maybe it's something to do with your npc extending your player class. Have the npc class extend Entity directly and include the calls to it's parent init and update methods. Not sure if the code above is complete or not, but the npc class never calls a parent update method, and not sure if this effects it.

1 decade ago by Silent

Inheriting from ig.Entity and calling update.parent() does indeed solve the issue. I am pleased.

However, I still can't see the cause of this problem. I read somewhere that replacing .update() with an empty function for entities that never move should help improve performance. When inheriting from ig.Entity, the issue is no longer present even with an empty .update(). Therefore, the problem was extending EntityPlayer. Why though?

1 decade ago by TommyBs

Ok I've worked out what is going on here. Dynamic collisions (between entities) are detected during a game cycle. ImpactJS tries to determine upfront if there will be a collision (which is why sometimes small entities collide after they've overlapped or don't collide at all). Dynamic Collision detection uses the last.x and last.y positions + the size.x and size.y of an entity to determine when they overlap. You can see this in ig.Entity.js at the ig.Entity.solveCollision() function.

Under the scenes, Entity.init actually sets this.pos.x and this.pos.y to the x,y parameters of the function, but it doesn't set the last.x and last.y. These are 0. When you don't add a function to your NPC class, it still calls the parent() method, hence the x,y positions being set of your npc initially as it calls the parent.init(). Your empty update() method though never updates the last.x and last.y positions as it doesn't call the parent. As such the last.x and last.y positions never get updated from 0,0.

You mentioned this worked for you when you switched the extend to use ig.Entity directly. Did you change the same example, or did you knock up a new example where the npc happened to be at 0,0? If it was 0,0 then this would still work.

To get this working with your example, you would just need to add an init method to your npc class that looks like this.

 init:function(x,y,settings){
        	this.last.x =x;
        	this.last.y = y;
        	this.parent(x,y,settings);
        },

Then just provide your empty version of update();

Alternatively if you're feeling really adventurous, you could update ig.Entity.init to include
this.last.x =x;
this.last.y = y;

I'm going to post this suggestion for Entity regardless, as it might be worth including, or Dominic might have a good reason for doing this for other calculations

1 decade ago by Joncom

Deleted.

1 decade ago by Silent

Thanks, this helps a lot! I really wanted to have NPC inherit from player.
Looks like I under-estimated having the engine's code handy.

You are the man :)

1 decade ago by drhayes

TommyBs: Nice investigative work there. Thanks for the post.
Page 1 of 1
« first « previous next › last »