Game Development Community

My Matrix related consolemethod addons.

by Benj · in Torque Game Engine · 02/07/2007 (10:02 am) · 0 replies

I didnt know if i should do this as a resource or a post, so ill start as a post, if people like it ill do a resource(never done one before)

these go in sceneobject.cc

this one points a objects Z axis at a normal, and Y axis at another.
ConsoleMethod( SceneObject, PointAt, void, 3, 3, "(2 Normals as 1 String)")
{
	MatrixF mat = object->getTransform();
	Point3F znormal;
	Point3F ynormal;
	Point3F tmpx;

	dSscanf(argv[2],"%f %f %f %f %f %f",&znormal.x,&znormal.y,&znormal.z,&ynormal.x,&ynormal.y,&ynormal.z);
	
	mCross(znormal,ynormal,tmpx);

	tmpx.neg();

	mCross(znormal,tmpx,ynormal);

	tmpx.normalize();
	ynormal.normalize();
	znormal.normalize();

	mat.setColumn(0,tmpx);
	mat.setColumn(1,ynormal);
	mat.setColumn(2,znormal);

	object->setTransform(mat);
}


this one dumps the transform matrix of a object(for debugging).
ConsoleMethod( SceneObject, dumpMatrix, void, 2, 2, "Dumps The Transform Matrix to the console.")
{
	const MatrixF& mat = object->getTransform();
	mat.dumpMatrix("Transform Matrix");
}

these 2 both get and set the rotation parts of the transform matrix.
ConsoleMethod( SceneObject, getMatrix, const char*, 2, 2, "Gets The Rotation Matrix")
{
	char *returnBuffer = Con::getReturnBuffer(256);
	Point3F px;
	Point3F py;
	Point3F pz;
	const MatrixF& mat = object->getTransform();
	mat.getColumn(0,&px);
	mat.getColumn(1,&py);
	mat.getColumn(2,&pz);
    dSprintf(returnBuffer,256,"%f %f %f %f %f %f %f %f %f",px.x,px.y,px.z,py.x,py.y,py.z,pz.x,pz.y,pz.z);	
	return(returnBuffer);
}

ConsoleMethod( SceneObject, setMatrix, void, 3, 3, "Sets The Rotation Matrix")
{
	MatrixF mat = object->getTransform();
	Point3F px;
	Point3F py;
	Point3F pz;
	
	dSscanf(argv[2],"%f %f %f %f %f %f %f %f %f",&px.x,&px.y,&px.z,&py.x,&py.y,&py.z,&pz.x,&pz.y,&pz.z);

	mat.setColumn(0,px);
	mat.setColumn(1,py);
	mat.setColumn(2,pz);

	object->setTransform(mat);
}

i just did the rotation parts of the matrix with get/setmatrix because you can easily move the object with settransform.