|
|
|
#1 |
|
crossconscious
Join Date: Sep 2005
Location: Belgium
Posts: 1,186
|
[F9] Vector class help
Hi,
Never been any good with vectors, but I decided to give it a shot and write a vector class, as part of a simple physics engine. But since I'm no good at it, I'm not sure if it's, well, any good. Could someone please have a look and see if the calculations look right, and if there's anything I could/should have done differently? Thanks. PHP Code:
__________________
www.crossconscious.com Last edited by Fall_X; 08-28-2007 at 07:04 PM. |
|
|
|
|
|
#2 |
|
Senior Member
Join Date: Jul 2007
Location: Columbus, OH.
Posts: 461
|
Not one to critique another persons code, but I'll offer up my own opinions since you asked.
I use vector types in my own codes quite a bit.I would pitch length and angle - they are derived quantities and instead of recalculating them every time a x/y component changes, compute them "on demand" through member functions. Also, I think that this class muddies the difference between displacement vectors and position vectors. The way this class defines them, all vectors are displacement vectors with strict start and end points (through the ..1 and ..2 notation). To me, it makes more sense to model any vector with just 2 components (or 3 .. when thinking in 3D spaces). A vector just has one .x and one .y member. If you need to model a displacement with explicit start and end points, use 2 position vectors - but I find I rarely need to model something like this. (In my codes, position is always a vector with 2 components, velocity is a vector with 2 components. The displacement / change in position that occurs through an update event is not modeled as a "point1", "point2" structure - its just "position += velocity*timestep".) For what it's worth, here's the vector class that I am using in my current project (doom clone). Take whatever you like. PHP Code:
I don't mean to say what you have is wrong. Really, a vector class is so simple that I would not fuss much about it's implementation or put a lot of focus on making a flexible / reusable component. Learning the math behind using vector arithmetic effectively is much more important. I would not be ashamed of reimplementing vector arithmetic from time to time, because different applications have different functional requirements. (Maybe you DO need to compute angle all the time, and it should be a stored member... but that's a choice that IMO should be made on an application-by-application basis) In reality, I find vector arithmetic is often happening inside tight inner loops and I end up just writing a lot of these functions (dot, cross, add, sub) in-place to avoid the overhead of a function call. (If AS3 had an inline function declaration that would do this for you, I would agree that making a full-fledged vector is a lot more sensible proposition). On the upshot, the implementation you have looks correct. But you forgot the cross product! That's the most important op of all (IMO).
Last edited by rachil0; 08-28-2007 at 09:53 PM. |
|
|
|
|
|
#3 |
|
Member
Join Date: Aug 2007
Posts: 61
|
APE (Actionscript Physics Engine) has a fairly complete Vector class.
--------------------------------------------------- Glaze :2D Tile, Physics, Particle, AI Game Engine http://yaa-blog.blogspot.com/search/label/Glaze --------------------------------------------------- |
|
|
|
|
|
#4 |
|
crossconscious
Join Date: Sep 2005
Location: Belgium
Posts: 1,186
|
Thanks for the advice. Exactly what I was hoping for.
I havenow changed my implementation so that I have a NullVector base class (without first coordinates, they are assumed 0 in the calculations), which should be a bit faster when you don't need the first coordinates. Then my Vector class extends this. I plan to use vectors to describe levels (for collision), so it makes sense to have a class with two sets of coordinates. Not too sure about the name NullVector though, but I couldn't think of anything appropriate. (edit: think I'm going to go for DisplacementVector which extends PositionVector, but I'm not sure yet). As for calculating the angle each time something changes, well, that was a choice I had to make. Faster getting or faster setting of variables. It should be easy to change anyway.
__________________
www.crossconscious.com Last edited by Fall_X; 08-29-2007 at 09:00 AM. |
|
|
|
|
|
#5 |
|
Member
Join Date: Aug 2007
Posts: 61
|
I saw you site & followed the discussion on tile engines. I think we have a common interest http://board.flashkit.com/board/showthread.php?t=742953
|
|
|
|
|
|
#6 |
|
crossconscious
Join Date: Sep 2005
Location: Belgium
Posts: 1,186
|
Yeah, I've seen your engine. Nice work!
I think I know quite a bit about scrolling and tile stuff, but my knowledge about physics is way more limited than yours, so I had already planned on looking at your source code asap
__________________
www.crossconscious.com |
|
|
|
![]() |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | |
|
|