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 anthony

Hi,

I'm trying to move a collision box so that it is in the centre of an Entity - the reason for this is that I want a projectile to pass into the Entity before changing direction.

I have an Entity with an image size of 40 x 44 and the collision box size is set at {x:20, y:22}. Using the debug mode I can see that the collision box is at the correct dimensions but I can't find a way to move it in relation to the Entity.

I tried to use offset but that also moves the image and not just the collision box itself. I'm hoping there's a way to do this and I don't have to write my own code into the Entity's check() function!

Any help is greatly appreciated!

Thanks,

Anthony

1 decade ago by Joncom

Could you just make the collision box smaller?

That way it would look like the projectile penetrates further into the entity before it hits the collision box...

1 decade ago by dominic

You can use the .offset.x/y properties to position the collision box relative to the graphics.

1 decade ago by anthony

@Joncom Making it smaller wouldn't work because the projectiles can come from 4 directions so as I can't move the collision box anything that comes from the top would collide sooner than anything from below for example.

@dominic I have already tried using offset but that resulted in moved the image/graphic also being moved on the screen too.

The code I'm using is this and I don't make any changes to size or position after these 3 lines.

size : {x:20, y:22},
offset : {x:0, y:20},
animSheet: new ig.AnimationSheet('media/img/object/deflector.png', 40, 44),

1 decade ago by dominic

I don't understand. You can't move the collision box to the center of the image, without moving the collision box to the center of the image 0_o

If your image is 40x44 and your collision box is 20x22, you have to specify an offset of 10x11 to center the image on the collision box.

1 decade ago by anthony

No, what I was saying is that when I tried to use an offset, it moved the IMAGE too so that it was rendering 20 pixels up from the actual position which is what is causing confusion. So, does it mean that it's not possible to move a collision box via an offset without also moving the image itself?

These screenshots might explain better (ignore the bomb and key, they are positioned at random) my problem.

Without an offset, the red 'arrow heads' render correctly, inside the grid square.
/><br />
<br />
WITH an offset (i.e. to move the collision box) they render out of position.<br />
<img src=

1 decade ago by stillen

Can you post more of your code, b/c offset shouldn't move the image only the collision box. I have used this on a lot of my games.

Perhaps you somewhere the pos.x and the offset.x are getting set incorrectly?

1 decade ago by anthony

Yeah sure. I've gone through all the code related to this and here it is.

Here's how I spawn the objects:

// this is in main...
// TILE_WIDTH = 40 
// TILE_HEIGHT = 44
// ...
this.spawnEntity(EntityDeflector, 2 * this.TILE_WIDTH, 5 * this.TILE_HEIGHT, {direction:this.FACING.UP});
this.spawnEntity(EntityDeflector, 2 * this.TILE_WIDTH, 6 * this.TILE_HEIGHT, {direction:this.FACING.RIGHT});

And here's the variables and init function:

// the init function of the object
EntityDeflector = ig.Entity.extend({
	size : {x:20, y:22},
        offset: {x:10, y:11}, // as per dominic's suggestion
	animSheet: new ig.AnimationSheet('media/img/object/deflector.png', 40, 44),
        // .. chopped out non-relevant variables...

	init: function(x, y, settings) {
		this.parent(x, y, settings);
	        this.addAnim('idle', 0.5, [0]);
		this.currentAnim = this.anims['idle'];
		this.deflectSound = new ig.Sound('media/snd/deflector.*');
		this.direction = settings.direction;  
		this.collided = false;

		// faces up by default.
		if (this.direction === ig.game.FACING.DOWN) {
			// DOWN
			this.currentAnim.angle = (-1.5707 * 2);
		}
		else if (this.direction === ig.game.FACING.LEFT) {
			// LEFT
			this.currentAnim.angle = -1.5707;
		}
		else if (this.direction === ig.game.FACING.RIGHT) {
			// RIGHT
			this.currentAnim.angle = 1.5707;
		}
	},

       //... there are no other changes to pos, size, offset, etc made 
       // in the rest of the code as these are static objects.

      draw: function() {
         this.parent();
      }

1 decade ago by stillen

I've never used the currentAnim.angle. Does the offset work properly without the angle adjustment?

1 decade ago by anthony

The upward facing deflector does not have any angle of rotation applied to it as the image faces up by default and it is still affected by applying the offset.

I've been able to get around the problem by writing specific x and y checks in the EntityDeflector's check() function and not using any offset as I was getting quite bogged down with this issue.

I'll come back to this at a later date to tackle it and if there's any update I'll post a reply here.

Thanks Joncom, dominic and stillen for your replies :)
Page 1 of 1
« first « previous next › last »