iOS Vector and Matrix Math: Part 2

Continuing on from my previous post iOS Vector and Matrix Math. I have managed to use matrix multiplication to transform a vector.

Vector Transform

Transforms a vector by a matrix

float matrix[16] =
{
	1.0f, 2.0f, 3.0f, 4.0f,
	7.0f, 6.0f, 9.0f, 10.0f,
	11.0f, 12.0f, 13.0f, 14.0f,
	15.0f, 16.0f, 17.0f, 18.0f
};
float vectorIn[4] = { 1.0f, 1.0f, 0.0f, 1.0f };
float vectorOut[4];
 
vDSP_mmul( &vectorIn[0], 1, &matrix[0], 1, &vectorOut[0], 1, 1, 4, 4 );

iOS Vector and Matrix Math

LEGO HazmatContinued on post iOS Vector and Matrix Math: Part 2.

For the past week or two I’ve been trying to reproduce the water effect of PixelJunk Shooter on the iPhone, however I seem to have reached a bottleneck with the collision detection used for repulsion when compressing.

I tried changing from Length to Length Squared, to Manhattan Distance, but it still grinded my framerate to 1FPS.

I decided to see if the Accelerate Framework in the iOS SDK had the vector math added to it yet, and to my surprise it had. It may have been added a while ago and I missed it. So I began to replace my math functions with the vector ones.

The strides are for the single precision functions are the number of floats. So for a stride of 8 bytes, you need to use 2.

float vec[2];
 
vDSP_vclr( &vec[0], 1, 2 );

This function sets the contents of a vector to zero. The equivalent function would be this

void vDSP_vclr( float * inVec, const int inStride, const unsigned int inCount )
{
	for( unsigned int idx = 0; idx < inCount; ++idx )
	{
		*inVec = 0.0f;
		inVec += inStride;
	}
}

I haven’t used the double precision version of the vector library yet so I don’t know if the stride is the count of doubles, or of floats.

I have managed to go from 17 FPS for 650 particles to 30 FPS (I originally had it at 300 particles at 30FPS but it turned out I was profiling in debug). A definite improvement from using the Accelerate library functions for 2d vector math. One of my main issues for getting 1FPS originally was I was island producer was hit testing each particle with itself so it always had the maximum number of particles on each island.

The next couple of pages on this post cover some of the functions (vector and matrix) and their equivalent C++ function.