Report a Bug

SIGN IN!

  • Impact ForumsLatest from the Impact Forums
  • Impact IRC ChatGet help from others on IRC
Visit Forums

Impact Forums

Entities inside area without Box2D
Posted August 30, 2014, 12:09 am — by Firegael
So, after solving the problem by writing some functions with the help of stack overflow, I also found a function which impact++ already has in it's intersection helper file; pointInPolygon(x, y, vertices).

I'll solve whether or not the following point is inside the following triangle using both methods anyhow, for thoroughness.

I should also note that Collin Hover's Impact++ method works for other polygons, not just triangles, where as mine is specific to triangles.

Problem:

point = 6,-1
point2 = 1,1
[/code]
Given triangle with the following vertices, is p inside?

a = 0,0
b = 8, -4
c = 12, 0
[/code]


My solution:

 
/**
 * I am unsure as to why this is called sign in every example I found. If someone could enlighten me that'd be great.
 * @param {Vector2} a Point which we are checking.
 * @param {Vector2} b One of three points of a triangle.
 * @param {Vector2} c One of three points of a triangle.
 * @return {Number}  Returns >= 0 if outside.
 */
sign = function(a, b, c) {
 
  return (a.x - c.x) * (b.y - c.y) - (b.x - c.x) * (a.y - c.y);
 
}
 
/**
 * Finds whether or not a point lies inside a triangle.
 * @param {Vector2} point Point which we are checking.
 * @param {Vector2} a   Point A.
 * @param {Vector2} b   Point B.
 * @param {Vector2} c   Point C.
 * @return {Boolean}    Returns true if inside triangle, false if outside.
 */
pointInTriangle = function(point, a, b, c) {
 
  var b1 = sign(point, a, b) < 0;
  var b2 = sign(point, b, c) < 0;
  var b3 = sign(point, c, a) < 0;
 
  return ((b1 == b2) && (b2 == b3));
 
}
 
pointInTriangle(point, a, b, c); // => true
pointInTriangle(point2, a, b, c); // => true
 

Using Impact++ pointInPolygon:

 
pointInPolygon(point.x, point.y, [a, b, c]); // => true
pointInPolygon(point2.x, point2.y, [a, b, c]); // => false
 
Phantom Settings in Entity. Help!
Posted August 28, 2014, 11:33 am — by henonchesser
http://chub.henonchesser.com/

Ok so here's the early stages of my game engine. My Character knows if he's holding something and what it is, and the objects know if they're being held and by whom. My problem is that on the death of either entity they both seem to remember this things. I've even overridden the Kill function to forget, but it doesn't. It comes out as some glitchy mess. Death sometimes seems to toggle being able to pick things up... I'm lost.

The BOX
 
ig.module (
	'game.entities.crate'
	)
.requires (
	'impact.entity'
	)
.defines (function() {
 
	EntityCrate = ig.Entity.extend ( {
 
	//LOCAL VARIABLES/////////////////////////////////////////////////////////
 
		//Size, Bounds, Orientation
		size: { x:16, y:16 },
 
 
		//Movement, Gravity, Speed
		maxVel : { x: 100, y: 150}, //max speed the enitity can move on this axis
		friction: { x: 600, y: 0 }, //friction agianst collision layer on an axis
 
		//Collision, C Type, Checks
		type: ig.Entity.TYPE.A, //Type A Collider
		checkAgainst: ig.Entity.TYPE.NONE, //Don't check for collisions
		collides: ig.Entity.COLLIDES.ACTIVE, //Get's Pushed
 
		//custom game variables
		pickedUp: false,
		holder: null,
 
		/////////////////////////////////////////////////////////////////////////
 
		//Initial Tiles
		animSheet: new ig.AnimationSheet( 'media/basegreen.png', 16, 16 ),
 
		init: function( x, y, settings) {
			this.addAnim('idle',1, [15]);
			//defualt settings, overides anything before it
			this.parent( x, y, settings);
 
		}, //INITIALIZE 
 
		kill: function(){
 
			this.pickedUp = false;
			this.holder = null;
			this.parent();
 
		},
 
		update: function() {
			this.parent();
 
 
 
			this.currentAnim = this.anims.idle;
 
			if (this.standing) {
				this.friction.y = 1000;
				this.maxVel.y = 0;
			}
			else {
				this.friction.y = 200;
				this.maxVel.y = 150;
			}
 
			if (this.holder != null && this.pickedUp == true) {
 
				this.pos.y = this.holder.pos.y;
 
				//and
 
				if (this.holder.flip == true){
					this.pos.x = this.holder.pos.x - 14;
					this.collides = ig.Entity.COLLIDES.NEVER;
				}
				else {
					this.pos.x = this.holder.pos.x + 10;
					this.collides = ig.Entity.COLLIDES.NEVER;
				}
 
			}
			else {
					this.collides = ig.Entity.COLLIDES.ACTIVE
			}
		},
 
		beACrate: function() {
 
		},
 
		draw: function() {
			this.parent();
		}
 
	});
});
 

--------------------------------------------------------------------------------------------------------------
THE PLAYER
 
ig.module (
	'game.entities.player'
	)
.requires (
	'impact.entity'
	)
.defines (function() {
 
	EntityPlayer = ig.Entity.extend ( {
 
		//LOCAL VARIABLES/////////////////////////////////////////////////////////
 
		//Size, Bounds, Orientation
		size: { x:12, y:16 },
		offset: { x:2, y:0 },
		flip: false,
		zIndex:50,
 
		//Movement, Gravity, Speed
		maxVel : { x: 100, y: 150}, //max speed the enitity can move on this axis
		friction: { x: 600, y: 0 }, //friction agianst collision layer on an axis
		accelGround: 400, //movement acceleration on ground
		accelAir: 200,    // ditto only in air
		jump: 120,      // jump speed, only it's used as a negative later on
 
		//Collision, C Type, Checks
		type: ig.Entity.TYPE.A, //Type A Collider
		checkAgainst: ig.Entity.TYPE.NONE, //Don't check for collisions
		collides: ig.Entity.COLLIDES.PASSIVE, //Get's Pushed
 
		//Custom Game Variables
		flapPower: 5,
		attacking:false,
		blocking:false,
		skin: 0,
 
		//picking up stuff
		holding: false,
		holdingWhat:null,
 
		/////////////////////////////////////////////////////////////////////////
 
		//Initial Tiles
		animSheet: new ig.AnimationSheet( 'media/player.png', 16, 16 ),
 
 
		init: function( x, y, settings) {
			//Start the animation
			this.setupAnimation(0);
			//defualt settings, overides anything before it
			this.parent( x, y, settings);
 
			if(!ig.global.wm) {
				ig.game.spawnEntity(EntityShield,this.pos.x, this.pos.y, {flip: this.flip} );
				ig.game.spawnEntity(EntitySword,this.pos.x, this.pos.y, {flip: this.flip} );
			}
		}, //INITIALIZE 
 
		setupAnimation: function(skin) { 
 
				this.addAnim( 'idle', 1, [0+skin]);
				this.addAnim('run', .07, [ 0+skin, 1+skin]);
				this.addAnim('jump', 0.1, [2+skin, 3+skin]);
				this.addAnim('fall', 0.1, [2+skin, 3+skin]);
		}, //SETUP ANIMATION 
 
		kill: function(){
			this.holding = false;
			this.holdingWhat = null;
			this.parent();
		},
 
		update: function () {
 
 
			//MOVEMENT//////////////////////////////////////////////////////////
 
 
			if (this.blocking == true) {
				this.maxVel.x = 50;
				this.jump = 60;
			}
			else {
				this.maxVel.x = 100;
				this.jump = 120;
			}
 
			//movement - left + right
			var accel = this.standing ? this.accelGround : this.accelAir;
			if ( ig.input.state ('p1left') ) {
					this.accel.x = -accel; //moving left is negative
					this.flip = true;
					}
			else if ( ig.input.state ('p1right') ) {
					this.accel.x = accel;
					this.flip = false;
					}
			else {
					 this.accel.x = 0;
					}
			// jump
			if ( this.standing && ig.input.pressed ('p1jump') ) {
 
					this.vel.y = -this.jump;
 
					}
			else if ( ig.input.pressed('p1jump') && this.flapPower != 0 ) {
 
						this.vel.y = -this.jump / 2 ;
						this.flapPower--;
					}
 
 
			//reset flying
			if ( this.standing) {
 
				this.flapPower = 5;
			}
 
				//ANIMATION INIT//
					//sets animation based on velocity both X and Y
					if ( this.vel.y < 0 ) {
						this.currentAnim = this.anims.jump;
						}
					else if ( this.vel.y > 0 ) {
						this.currentAnim = this.anims.fall;
						}
					else if ( this.vel.x != 0 ) {
						this.currentAnim = this.anims.run;
						}
					else{
						this.currentAnim = this.anims.idle;
						}
				//END ANIM INIT// 
 
 
			//END MOVEMENT///////////////////////////////////////////////////////////
			this.parent();
 
			//flips animation if entity is flipped
			this.currentAnim.flip.x = this.flip;
 
 
		}, //UPDATE METHOD
 
		draw: function() {
 
			this.parent();
 
		}
 
	} );
 
	EntityShield = ig.Entity.extend ({
 
		//variables
		owner: null,
 
		//Size, Bounds, Orientation
		size: { x:12, y:16 },
		offset: { x:2, y:0 },
		zIndex:100,
 
		//Initial Tiles
		animSheet: new ig.AnimationSheet( 'media/base.png', 16, 16 ),
 
		init: function( x, y, settings) {
			this.addAnim('idle',1, [170]);
			//defualt settings, overides anything before it
			this.parent ();
 
 
		}, //INITIALIZE 
 
		update: function (x, y, settings) {
 
			this.parent(x,y,settings);
 
			//this.pos.x = this.parent.x;
			//this.pos.y = this.parent.y;
			var master = ig.game.getEntitiesByType( EntityPlayer )[0];
			if( !master){
				this.kill();
			}
			else {
 
				//FLIP THE SHIELD IF THE PLAYER IS FLIPPED////////////////
				this.pos.y = master.pos.y +2; //POS X
 
				if (master.flip == true) {
					this.pos.x = master.pos.x +4; //POS Y FLIPPED
				}
				else {
					this.pos.x = master.pos.x -4; //POS Y NONFLIPPED
				}
 
 
				//USE SHILED/////////////////////////////////////////////
 
				if ( ig.input.state ('p1shield') && master.attacking == false) {
					if (master.flip == true) {
						this.pos.x -= 8; //POS Y FLIPPED
						master.blocking = true;
					}
					else {
						this.pos.x += 8; //POS Y NONFLIPPED
						master.blocking = true;
					}
				}
				else {
					master.blocking = false;
				}
			}
		},
 
		draw: function() {
 
			this.parent();
 
		}
 
	});
 
	EntitySword = ig.Entity.extend ({
 
		//variables
		owner: null,
 
		//Size, Bounds, Orientation
		size: { x:12, y:16 },
		offset: { x:2, y:0 },
		zIndex:1,
		flip: null,
 
		//Attack Vars
		attackSpeed: 2,
		attackRange: 8,
		attackTimer: null,
		attacking: false,
 
		//Collision, C Type, Checks
		type: ig.Entity.TYPE.A, //Type A Collider
		checkAgainst: ig.Entity.TYPE.A, //Don't check for collisions
		collides: ig.Entity.COLLIDES.PASSIVE, //Get's Pushed
 
		//Initial Tiles
		animSheet: new ig.AnimationSheet( 'media/base.png', 16, 16 ),
 
		init: function( x, y, settings) {
			this.addAnim('idle',1, [256]);
			//defualt settings, overides anything before it
			this.parent ();
 
			this.attackTimer = new ig.Timer();
		}, //INITIALIZE 
 
		check: function( other ) {
			var master = ig.game.getEntitiesByType( EntityPlayer )[0];
 
			if (typeof(other.beACrate) == 'function' && master.holding == false && master.attacking == true){
				master.holding = true;
				master.holdingWhat = other;
				other.pickedUp = true;
				other.holder = this;
				//other.flip = this.flip;
			}
		}, //INITIALIZE 
 
 
		update: function (x, y, settings) {
 
			this.parent(x,y,settings);
 
			//this.pos.x = this.parent.x;
			//this.pos.y = this.parent.y;
			var master = ig.game.getEntitiesByType( EntityPlayer )[0];
			if( !master){
				this.kill();
			}
			else {
				//FLIP THE SWORD IF THE PLAYER IS FLIPPED////////////////
				this.pos.y = master.pos.y; //POS X
 
				if (master.flip == true) {
					this.flip = true;
					this.pos.x = master.pos.x -6; //POS Y FLIPPED
				}
				else {
					this.flip = false;
					this.pos.x = master.pos.x + 6; //POS Y NONFLIPPED
				}
 
				//USE SHILED/////////////////////////////////////////////
 
				if ( ig.input.pressed ('p1sword') && master.attacking == false && master.blocking == false && master.holding == false) {
 
					//now we're attacking
					master.attacking = true;
 
					this.attackTimer.set(.2);
 
				}
				else if (ig.input.pressed ('p1sword') && master.attacking == false && master.blocking == false && master.holding == true){
 
					//now we're throwing
					if (master.holdingWhat !=null){
							//drop anything
							master.holding = false;
 
							//shoot the held item
 
							if (ig.input.state ('p1down')) {
 
								//lay it down gently
							}
							else {
								if (master.flip == true){
									master.holdingWhat.vel.x = -500;
								}
								else {
									master.holdingWhat.vel.x = 500;	
								}
							}
 
							//tell the held item what to think
							master.holdingWhat.pickedUp = false;
							master.holdingWhat.holder = null;
 
							//remove our reference to it
							master.holdingWhat = null;
						}
 
				}
 
				if( master.blocking == true) {
					if (master.flip == true) {
					this.pos.x = master.pos.x +2; //POS Y FLIPPED
				}
				else {
					this.pos.x = master.pos.x -2; //POS Y NONFLIPPED
				}
				}
 
				if( master.attacking == true) {
 
					this.collides = ig.Entity.COLLIDES.ACTIVE;
 
 
					if (master.flip == true) {
						this.pos.x -= this.attackRange - this.attackTimer.delta()/this.attackRange; //POS Y FLIPPED
					}
					else {
						this.pos.x += this.attackRange - this.attackTimer.delta()/this.attackRange; //POS Y NONFLIPPED
					}
 
					if (this.attackTimer.delta() >= 0){
 
 
						master.attacking = false;
						//alert("Attack");
					}
				}
 
				else {
					this.collides = ig.Entity.COLLIDES.NEVER;
				}
				this.currentAnim.flip.x = master.flip;
			}
		},
 
		draw: function() {
 
			this.parent();
 
		}
 
	});
 
} );
 

And my re spawning the player in main
 
	if(this.player1._killed || !this.player1 && this.spawnTimer.delta() >= 1 ){
						 this.player1 = ig.game.spawnEntity(EntityPlayer,this.p1StartPosX,this.p1StartPosY);
 
						 this.spawnTimer.reset();
 
				}
 

Older Posts

Ready to get to the point?

Your Email will remain private and is only used for good. We promise!


Please use only letters, numbers or underscores.

SIGN UP!