Wednesday, March 4, 2009

Sphere Pong

This week's mechanic I was working on didn't really work, but it was a fun idea. I created a 3d pong game where you move the puck around a sphere and try and keep the ball in the middle. The problem is there is no reference point. I need some sort of skysphere or spherical markings or maybe also a tail on the ball so you can more easily tell direction. Ran out of time, but may come back and revisit this one someday.

I'm still learning XNA and the graphics look crummy, but for me right now this is more about learning the api than working on making a good looking game. Forward progress at any rate.
For a code sample a lot of the time was spent in positioning the puck around the sphere. I only finished the x,z plane as it didn't work for that, so no reason to add the third dimension. Here is the code to position the puck around the sphere.
// move the paddle using the upVector (true) or the cross product of the origin and up (false)
// direction is a + or - 1 for up, down, left or right
private void movePaddle(bool usingUpVector, float dir)
{

if (!usingUpVector)
{
// need to take the cross between the up and the origin
Vector3 vp = paddlePos;
vp.Normalize();
// vector change
Vector3 vc = Vector3.Cross(vp, paddleUp) * dir;
vc.Normalize();
vc = vc * paddleSpeed;

// find the new location, will be outside the sphere
vp = paddlePos + vc;

// fix up the distance ot the sphere
vp.Normalize();
vp = vp * radius;

paddlePos = vp;

// the up vector should not have changed, right?

// move the camera behind the puck
fixupCamera();
}
else
{
// for this case, take the cross product
// move in the direction of the up vector (+ or -)
// position against the spehere
// use the cross product to calculate the new up vector
}
}

No comments: