For a start it doesn't really look very nice. It also is not reusable, and you have to make it even more convoluted to do some of the more advanced math needed in games. The better alternative is to use vectors. Using vectors we can do movement something like this:

2DVector speed;
2DVector position;
player.position += player.speed;

You will probably have realised that this is going to require knowledge of overloaded operators among other things. This should be a great challenge and I look forward to seeing what people can come up with. Other functions that could be included are dot product, normalise etc.

Extra points for creating a template class so you can easily switch between ints, floats and doubles depending on how much precision you want. Use any language you want but C++ would be great Feel free to ask questions within this thread so we can all collaborate.

Re: Game Programming Challenge: Write a reusable 2D vector class

Posted 22 September 2011 - 04:08 AM

I'll enter this Java one I made for a game physics engine. I've done a few strange things. First, it is immutable which I guess sends alarm bells ringing for most game developers. I did it to see of Java can cope. Turns out it copes nicely. Second, I save the minimum state I have to. Every time you want an angle, it calculates it again. I did this to avoid the possibility of inconsistant states, and I'll only optimise if it ever becomes a bottleneck. Finally, there are no overloaded operators in Java but you will see things like add, subtract, multiply. Oh, and angles are in degrees. I like it that way.

Re: Game Programming Challenge: Write a reusable 2D vector class

Posted 23 September 2011 - 05:17 AM

Look out for speed-critical sections as well in your code. If using C++ a very common method for finding the appropriate function calls for applying your implementations to various types and other situations where you can have naming conflicts would be to make use of namespaces:

namespace Vector
{
class Vector2D
{
// ...
}
}

This could also be manipulated in such a way to get around using templates which are generally a big no-no in any speed-critical section of code.

I'm also seeing a fear of callback functionality that will drastically improve the flexibility of your implementations. And last but not least, let's not forget the use of design patterns like singleton (as always suggested) to keep track of your various instances.

Re: Game Programming Challenge: Write a reusable 2D vector class

Posted 23 September 2011 - 07:37 AM

So vectors have an origin, a magnitude and a direction... which is what I think you were getting at before. But if you work with zero-based vectors (I forget their proper name) you can do away with the origin. and just store (x,y) or (magnitude,angle) as you choose.

You can then compose an object that uses a vector as its position and a vector as its delta. In my physics engine, moving objects have a position vector, a speed vector and an acceleration vector. I think the speed vector is similar to your delta (acceleration being a second order delta). This would make my update() methods similar to your ++ operator, albeit more verbose. The moving object class has an update method that takes no parameters (since it already has the acceleration and speed), so I guess that would be the direct equivalent.

Re: Game Programming Challenge: Write a reusable 2D vector class

Posted 23 September 2011 - 08:26 AM

I believe an orgin (0,0) vector is called a "free vector." Or perhaps bound at zero. I'm not real sure how useful this is to the game mechanics of position. I want to know where I am as well as where I'm going, so the orgin does matter.

You're right that only ++ would makes sense in context, as a vector only travels one direction. Other vector properties are obviously valuable, like changing magnatude directly, rather than mucking about with a second point.

I'm not sure why you need both a position and speed, as that kind of sounds like one vector. Acceleration makes sense, though for a physics engine I guess that could be a few vectors. Gravity and the object's momentum.

I've honestly just looked at your code. I was writing in a happy C++ vacuum, having fun with frameworks. I'd thought to stick to basics. I guess the real test of a given framework would be how easy it is to do a given task. Ball bouncing is good. Hmm...

Re: Game Programming Challenge: Write a reusable 2D vector class

Posted 23 September 2011 - 01:14 PM

I supposed you're right. I just think of the arrows with the little dot at the beginning...

So, technically, I guess want my vector class to just be an extension of a point class, with direction and mag worried out? But for a travelling point, I'd want to pair them. I like the ++ or cfoley's update. Need a name for that.

I agree with the matrix class. There are a number of ways to skin that cat. I believe that was my first graphic project, in Pascal. Pity I lost those floppies. Damn, I feel old sometimes.