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 JamieRushworth

Hi there, I am currently trying to make a simple top down view game that features two different types of entity. I'm trying to make a mirror that doesn't react to physics aside from allowing the laser to bounce off of it and me being able to rotate it directly as though using this.currentAnim.angle. The other entity being the laser which needs to bounce off the mirror with no loss in velocity. This is a screenshot of it at the moment http://imgur.com/UzXntHP

Having never used Box2d and there not being a lot of documentation with it in Impact, I'm finding myself a bit lost. I can spawn and get the laser to fire at the mirror and I can get the mirror to rotate, despite the fact it acts with mass meaning letting go of the arrow key allows it to continue rotating, but then the laser hits the mirror and they both act as though they are in space.

So I basically need to know how to rotate the mirror without ApplyTorque, how to make the mirror stay in place when it is hit and how to make the laser bounce off the mirror with no loss in velocity.

I should point out that I am using Joncom's very awesome Box2d plugin https://github.com/Joncom/impact-box2d-sugar

Any help will be very appreciated, thanks!

1 decade ago by JamieRushworth

Here is the code for my laser entity

ig.module(
	'game.entities.laser'
)
.requires(
	'impact.entity',
	'plugins.box2d.entity'
	//'plugins.box2d.collision'
)
.defines(function() {
	EntityLaser = ig.Entity.extend({

		//Physics
		type: ig.Entity.TYPE.A,
		checkAgainst: ig.Entity.TYPE.NONE,
		collides: ig.Entity.COLLIDES.NEVER,

		isBullet: true,

		bounciness: 1,

		//Drawing
		animSheet: new ig.AnimationSheet('media/laser.png',32,16),
			size: {x: 30, y: 3},
			offset: {x: 1, y: 12},



		init: function( x, y, settings ) {
			this.addAnim( 'default', 1, [0] );

			this.restitution = 1;

			this.parent( x, y, settings );

	        this.vel.x -= 100;


		},

		update: function() {

			this.parent();

		},

		

	});
});

And this is my mirror/paddle code:

ig.module(
	'game.entities.paddle'
)
.requires(
	'impact.entity',
	'plugins.box2d.entity'
	//'plugins.box2d.collision'
)
.defines(function() {
	EntityPaddle = ig.Entity.extend({

		//Physics
		type: ig.Entity.TYPE.B,
		checkAgainst: ig.Entity.TYPE.NONE,
		collides: ig.Entity.COLLIDES.NEVER,


		//Drawing
		animSheet: new ig.AnimationSheet('media/paddle.png',32,16),
			size: {x: 30, y: 3},
			offset: {x: 1, y: 12},



		init: function( x, y, settings ) {

			ig.input.bind(ig.KEY.LEFT_ARROW, 'rotateCC');
			ig.input.bind(ig.KEY.RIGHT_ARROW, 'rotateC');

			this.addAnim( 'default', 1, [0] );

			this.parent( x, y, settings );

		},

		update: function() {

			this.parent();

			if (ig.input.state('rotateCC')) {
				this.body.ApplyTorque (-20);
			};

			if (ig.input.state('rotateC')) {
				this.body.ApplyTorque (20);
			}
		},

	});
});

1 decade ago by Joncom

how to rotate the mirror without ApplyTorque
You can rotate the entity directly. Example: entity.angle += 10;

how to make the mirror stay in place when it is hit
Making it a static body may work. From the official Box2D manual:
A static body has does not move under simulation and behaves as if it has infinite mass... Static bodies can be moved manually by the user. A static body has zero velocity.

EntityPlayer = ig.Entity.extend({
    bodyType: Box2D.Dynamics.b2Body.b2_staticBody,
    ...
});

how to make the laser bounce off the mirror with no loss in velocity
Have you tried .bounciness?
EntityPlayer = ig.Entity.extend({
    bounciness: 1, // 100% velocity reflection
    ...
});

1 decade ago by JamieRushworth

That's brilliant thanks, that solves those issues. Now the laser bounces off spinning out of control though, so I guess I need to lock its rotation and set the angle manually. Thanks for your help and for the very useful plugin.

1 decade ago by Joncom

No problem. Glad that helped!
Page 1 of 1
« first « previous next › last »