Archive for Programming Tidbits

Programming Tidbit #3 – Constraining Rotation to a Max Turn Rate Per Second

d_turret

This is the third post in the series “Programming Tidbits.” These articles consist of useful bits of information or code that I would like to share, but are not necesarily full length articles.

Sometimes you may wish to constrain the amount of rotation an entity can rotate per second or tick or whatever, such as the turret on a tank or the turning of the tank itself. This can be done with a bit of math. It could seem a bit complicated without using the math libraries I have already created.

Here it is using my code:

We are going to assume the maxTurnRate will be in degrees per second.

if( maxTurnRate > 0 && currentVelocity.angleTo( newVelocity ) > ( maxTurnRate * MathUtils.DEG_TO_RAD ) * stepSize )
{
 	var mat:Matrix2D = new Matrix2D();
	mat.rotate( ( ( maxTurnRate * MathUtils.DEG_TO_RAD ) * stepSize ) * heading.sign( newVelocity ) );
 	mat.transformVector( currentVelocity);
 	mat.transformVector( heading );
 }
 else
 {
 	velocity.x = _newVel.x;
 	velocity.y = _newVel.y;
 }

The Code Explained

So let’s take a look at what is happening there. First, we are checking to see if maxTurnRate is > 0…because we will assume if it is set as 0 then the entity should be able to turn freely. If it is greater, then we will get the angle from our current velocity and check to see if it is greater than the max turn rate multiplied by the step size. The step size is generally just: tickSize / 1000.

The math to get the angle from one vector to another is:

public function angleTo( vector1:Vector2D, vector2:Vector2D ):Number
{
	return Math.acos( dotProduct( vector1, vector2 ) / ( vector1.length * vector2.length ) );
}
 
public function dotProduct( vector1:Vector2D, vector2:Vector2D ):Number
{
	return ( vector1.x * vector2.x ) + ( vector1.y * vector2.y );
}

Next, if the rotation is greater than the max amount aloud…we will use a matrix to rotate the velocity vector to it’s correct position and transform it and the heading ( the direction the entity is going ). Note: The “sign” function just returns -1 if vector is counterclockwise, else 1 if clockwise. We need this to know which direction to rotate obviously.

The Matrix Code

Here is some of the matrix code, though I definitely don’t plan on explaining matrices in this post. Which is why it’s best to just use the library if you don’t actually understand the math part itself. :D

/**
 * Use to rotate.
 * ( Considers that the Vector object represents a point, not an actual vector )
 * @param rot
 *
 */
public function rotate(rot:Number):void
{
	var sin:Number = Math.sin(rot);
	var cos:Number = Math.cos(rot);
	multiply( new Matrix2D( cos, sin, 0, -sin, cos, 0, 0, 0, 1 ) );
}
 
/**
 * Use to multiply two Matrixes...mainly used internally.
 * @param matrix The Matrix to multiply by.
 *
 */
public function multiply(matrix:Matrix2D):void
{
	var m:Matrix2D = new Matrix2D();
 
	//first row
	m.a1 = (a1 * matrix.a1) + (a2 * matrix.b1) + (a3 * matrix.c1);
	m.a2 = (a1 * matrix.a2) + (a2 * matrix.b2) + (a3 * matrix.c2);
	m.a3 = (a1 * matrix.a3) + (a2 * matrix.b3) + (a3 * matrix.c3);
	//second row
	m.b1 = (b1 * matrix.a1) + (b2 * matrix.b1) + (b3 * matrix.c1);
	m.b2 = (b1 * matrix.a2) + (b2 * matrix.b2) + (b3 * matrix.c2);
	m.b3 = (b1 * matrix.a3) + (b2 * matrix.b3) + (b3 * matrix.c3);
	//third row
	m.c1 = (c1 * matrix.a1) + (c2 * matrix.b1) + (c3 * matrix.c1);
	m.c2 = (c1 * matrix.a2) + (c2 * matrix.b2) + (c3 * matrix.c2);
	m.c3 = (c1 * matrix.a3) + (c2 * matrix.b3) + (c3 * matrix.c3);
 
	Set(m.a1,m.a2,m.a3,m.b1,m.b2,m.b3,m.c1,m.c2,m.c3);
}
 
/**
 * This is used to do the final transformation upon the Vector
 * @param point The Vector to apply this Matrix transformation to.
 *
 */
public function transformVector(point:Vector2D):void
{
	var tempX:Number = (a1*point.x) + (b1*point.y) + (c1);
	var tempY:Number = (a2*point.x) + (b2*point.y) + (c2);
	point.x = tempX;
	point.y = tempY;
}

And that’s all!

So anyways…I’ve shown the internal workings of the math code there for you, though the first code block is all that should really matter to you in the end…since it shows how to actually use the math.

No example today, since I’m pretty sure you can imagine a turret rotating at a max speed…lol. However, it will be used in the next Game Structure post for the entities.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Programming Tidbit #2 – Efficient Multiple Object Collision Testing

This is the second post in the series “Programming Tidbits.” These articles consist of useful bits of information or code that I would like to share, but may not be considered a full article.

Sometimes your game may consist of a very large amount of entities that must be checked for collision with each other. In order to increase the speed of your collision tests there are a few methods that you can employ. This is no way an all inclusive article on this subject because there are MANY different methods for improving collision performance, however hopefully this can get you started.

The easiest thing to do is to eliminate as many collision checks as possible, as well as keep the collision checks as simple as you can. I’ll try to explain a few different methods which you can combine to speed up your testing.

The Bad Way

Generally you will be storing multiple entities in some type of array or map. The most common way of checking for collision is to loop through this array and check each entity against one another.

var entity1:Entity;
var entity2:Entity;
 
for ( var i:int = 0; i < entities.length; i++ )
{
 	entity1 = entities[ i ];
 
 	for ( var j:int = 0; j < entities.length; j++ )
 	{
 		entity2 = entities[ j ];
 
 		if( entity1.isOverlapping( entity2 ) )
 		{
 			// Handle Collision
 		}
 	}
}

Reduce Redundancy

Using this method will work, however the more entities you add to the array the slower it will become. The main problem here is that many of your checks are redundant. The more efficient method would be to make sure that you only check each entity once by making a small change to the inner loop:

for ( var j:int = i + 1; j < entities.length; j++ )

Collision Groups

Another way of increasing the speed is to give your entities flags or collision groups. For example, you may flag some entities as “collidable” and you may also decide that some entities cannot collide with each other so you might give them a “group” variable. This eliminates the need for actually running checks against this entity at all.

var entity1:Entity;
var entity2:Entity;
 
for ( var i:int = 0; i < entities.length; i++ )
{
 	entity1 = entities[ i ];	
 
 	if( !entity1.isCollidable )
 	{
 		continue;
 	}
 
 	for ( var j:int = i + 1; j < entities.length; j++ )
 	{
 		entity2 = entities[ j ];
 
 		if( !entity2.isCollidable || entity1.group == entity2.group )
 		{
 			continue;
 		}
 
 		if( entity1.isOverlapping( entity2 ) )
 		{
 			// Handle Collision
 		}
 	}
}

Simplify First

The last trick would apply when you are using complex collision such as pixel perfect / polygon collision. Instead of checking each object with the complex collision algorithm, use a simple bounding circle collision test first, and if that test returns true then proceed with the more detailed collision test.

if( entity1.isOverlapping( entity2 ) )
{
	if( entity1.detailedCollisionTest( entity2 )
	{
		// Handle Collision
	}
}

Final Words

Other methods of increasing collision testing speed would be to implement some sort of “broad phase” algorithm, however that is beyond the scope of this article. The methods discussed here should be sufficient for most flash games.

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)
Loading ... Loading ...

Programming Tidbit #1 – Eliminate Magic Numbers

This is the first post in the series “Programming Tidbits.” These articles consist of useful bits of information or code that I would like to share, but may not be considered a full article.

The Wrong Thing…

When programming, a lot of times you may find yourself throwing together some code which uses “Magic Numbers.” A magic number is basically just any static number sitting in your code such as:

public function moveMyCharacter() : void
{
	myCharacter.x += 5;
	myCharacter.y += 5;
}
 
public function damageEnemy() : void
{
	someEnemy.health -= 20;
}

Why is it bad?

Now obviously that is just a simple example, but let’s say you have code ALL OVER your project using these “magic numbers.” What happens then, if later you want to change these numbers? You have a long and tedious process of finding all times that they are used. Not only that, but you are prone to missing something which will therefore lead to unpredictable code and bugs.

The right thing…

Instead of being mean to yourself, do the right thing and create constants or variables for all of your magic numbers. The usual place to put them is at the top of your class. Even better would be to externalize the data into some sort of settings file ( if the data is worthy of course ). I’ll be discussing externalizing data in a future article if you aren’t quite sure how to go about that…

So here is the code once again, done properly.

// Top of your Class somewhere...
private const MOVE_SPEED:int = 5;
private const HERO_DAMAGE:int = 20;
 
public function moveMyCharacter() : void
{
	myCharacter.x += MOVE_SPEED;
	myCharacter.y += MOVE_SPEED;
}
 
public function damageEnemy() : void
{
	someEnemy.health -= HERO_DAMAGE;
}

1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 4.80 out of 5)
Loading ... Loading ...