Nescio Posted February 4, 2021 Report Share Posted February 4, 2021 Unfortunately I can't really help you here, I'm not a programmer and I don't understand C++ source code, nor do I get the problem (isn't a position a vector, mathematically?). Anyway, the lines you're asking about were introduced by commit 21144, the associated patch (with discussion) is https://code.wildfiregames.com/D1041 Quote Link to comment Share on other sites More sharing options...
DanW58 Posted February 5, 2021 Author Report Share Posted February 5, 2021 (edited) A position is NOT a vector, mathematically. A position is a position whether there is a frame of reference or not, whereas a vector is a position relative to another position, possibly an "origin", in a specific frame of reference. The confusion is more than just semantics. A vector can be divided in half. A position is a point, which has no size, and therefore cannot be divided. A vector can be rotated. A position cannot. A vector can be added to a position to obtain another position; and a vector can be subtracted from a position; but a position cannot be subtracted from a vector; it makes no sense. And you might ask, "well, isn't it easier to just mix it all together?". Yes, it is; as it is also easier to make programming mistakes. By the same argument, a temperature and a distance are both quantities. Does that mean that we should be able to add them in a program? C++ is a huge evolution over C precisely because it made it possible for us to create our own types, and make it more difficult for conceptual mistakes to compile. That's why people should be trying to NOT mix concepts together when possible. To confuse positions and vectors is not too different from making every parameter a typedef of float. We've got a type system; let's use it. So let me repeat the question again, as now it is lost in the previous page: Could someone please help me understand this piece of code? I've been stuck for two days here. It's in the file source/simulation2/components/CCmpFootprint.cpp, the function PickSpawnPoint(), half way down, where it reads, if (m_Shape == CIRCLE) // Multiply the point by 2pi / 6*(distX+r) to get the angle. pos += u.Rotate(fixed::Pi() * (offsetPoints[r] + k) / (3 * (distX + r))).Multiply(halfSize.X + gap * r ); else { The problem is that I'm precisely immersed in making clear separations between what is a point and what is a vector and what is a unit-vector, etceteras. In my system, a point can be added to a vector, to get another point, for example; but a point cannot be added to another point. Much less can a point be multiplied, unless there is a way to end up with a "point cluster". But here in this code pos is referred to as a point, in comments, but implemented as a vector; and this comment says that we are going to "Multiply the point..." (literally! How does one "multiply a point"?) by what looks to me like an angle, sort of like dividing a cat's tail by a magic flavor, allegedly to obtain yet another angle.... So this "point" is some kind of number that multiplied by an angle yields yet another angle?!?! 2pi/6 (or pi/3) is an angle, alright, (60 degrees in radians); that much I know. The (distX+r) is as clear as mud even though I know r is some kind of "row number". And then this act of magical multiplication is implemented by operator+=(); yep... But the worst part is I can't figure out what pos is, or what it is supposed to be. A point or a vector? If I make it a CFixedVector3D<WORLD>, a dozen things break; and if I make it a CFixedPoint3D<WORLD> about two and a half dozen things break... Thanks in advance. Edited February 5, 2021 by DanW58 Quote Link to comment Share on other sites More sharing options...
Nescio Posted February 5, 2021 Report Share Posted February 5, 2021 4 hours ago, DanW58 said: A position is NOT a vector, mathematically. A position is a position whether there is a frame of reference or not, whereas a vector is a position relative to another position, possibly an "origin", in a specific frame of reference. The confusion is more than just semantics. A vector can be divided in half. A position is a point, which has no size, and therefore cannot be divided. A vector can be rotated. A position cannot. A vector can be added to a position to obtain another position; and a vector can be subtracted from a position; but a position cannot be subtracted from a vector; it makes no sense. To me it seems you're confusing a position with a point, which are two different things. Please correct me if I'm mistaken, it's been a while since I last dabbled in mathematics. 4 hours ago, DanW58 said: So let me repeat the question again, as now it is lost in the previous page: Repeating the question won't give you the answer. 0 A.D. has a history of nearly two decades, people who worked on it in the past are no longer active now. If you don't understand a piece of code and the rest of the file doesn't make it any clearer, then you should use svn blame or go through the svn revision history to figure out when it was introduced, and read the discussions at the relevant differentials or trac tickets, if any. It might give you information you need. Quote Link to comment Share on other sites More sharing options...
hyperion Posted February 5, 2021 Report Share Posted February 5, 2021 3 hours ago, DanW58 said: A position is NOT a vector, mathematically. A position is a position whether there is a frame of reference or not, whereas a vector is a position relative to another position, possibly an "origin", in a specific frame of reference. The confusion is more than just semantics. A vector is a tool which can be used to describe a position, speed, impulse, force, spin, translation and whatever else I haven't mentioned. There is no position without a frame of reference. You use the term point when in fact you mean position, location (less formal) and to a lesser extent cartesian coordinate. 4 hours ago, DanW58 said: A vector can be added to a position to obtain another position Two vectors can be added to describe the translation of a position. 4 hours ago, DanW58 said: A vector can be divided in half Multiplying a vector with a scalar is still a vector. As for your question, without checking the code: pos is a vector as is u, Rotate is a matrix multiplication denoted by a scalar for the the normal vector, Multiply is a scalar multiplication denoting the scaling of the vector. pos += u is the translation of pos (denoting a position, likely the center of the structure spawning the unit) by u'. Yes, the comment is sub par. Quote Link to comment Share on other sites More sharing options...
DanW58 Posted February 5, 2021 Author Report Share Posted February 5, 2021 (edited) Okay, it seems to me the distinction is semantic; to me a position (in a map) IS a point. In any case, my point classes are called 'point' as in CFixedPoint3D<WORLD>. 6 hours ago, hyperion said: 11 hours ago, DanW58 said: A vector can be added to a position to obtain another position Two vectors can be added to describe the translation of a position. I didn't deny that vectors can be added; I was talking about a position (in my lexicon, meaning a point) having a vector added to it, to specify a new ... point. 6 hours ago, hyperion said: 11 hours ago, DanW58 said: A vector can be divided in half Multiplying a vector with a scalar is still a vector. EXACTLY. I was contrasting that to the idea of multiplying or dividing ... a point. Anyways... 6 hours ago, hyperion said: As for your question, without checking the code: pos is a vector as is u, Rotate is a matrix multiplication denoted by a scalar for the the normal vector, Multiply is a scalar multiplication denoting the scaling of the vector. pos += u is the translation of pos (denoting a position, likely the center of the structure spawning the unit) by u'. "pos is a vector", I think you are right. My current theory is that all of this function is happening in spawning building space, such that spawning positions around the building are points relative to the center of the building, and would therefore be (what I call) vectors, in the real world (the real virtual world). "as is u", I think u and v are in fact unit-vectors, and are so in my revised code, namely CFixedUnitVector2D<WORLD>. "Rotate is a matrix multiplication...", well, there is no matrix multiplication going on in that line of code I showed, though I agree 100% that rotation of a vector can be achieved by matrix multiplication. It can also be achieved by quaternion multiplication, but that takes more code. "Multiply is a scalar multiplication denoting...", 100% agreed, but it's not what I see in the code. "pos += u is the translation of pos..", EXACTLY. I think that pos is initially initialized to initialPos which my current theory says is the center of the spawning building, and that this line of code takes us from there to the exact place in the periphery where the next spawn will happen. If all of this is correct, my confusion could be due to the comment on the line of code being about a rotation of a vector about to be added TO the center of the building, not a rotation of the center of the building itself... Okay, I think I'm now on the way to figuring this out. Thanks! Nescio, I almost missed your post. Yes, I guess I do confuse a position with a point. Point taken ;-) Sad to hear the inital coders have left. Edited February 5, 2021 by DanW58 Quote Link to comment Share on other sites More sharing options...
hyperion Posted February 5, 2021 Report Share Posted February 5, 2021 2 hours ago, DanW58 said: "as is u", I think u and v are in fact unit-vectors, and are so in my revised code, namely CFixedUnitVector2D<WORLD>. going by the calculation of the scalar of Multiply() it's safe to assume length of u being 1. So u being a unit vector is in all likelihood true. Thou there is no v in your excerpt. 2 hours ago, DanW58 said: "Multiply is a scalar multiplication denoting...", 100% agreed, but it's not what I see in the code. radius of structure + gap + radius of unit, how is this not adjusting length of u (assumed to be 1) to the distance between centre of building and spawn point? 3 hours ago, DanW58 said: "Rotate is a matrix multiplication...", well, there is no matrix multiplication going on in that line of code I showed, though I agree 100% that rotation of a vector can be achieved by matrix multiplication. It can also be achieved by quaternion multiplication, but that takes more code. In vector math a rotation is a matrix multiplication. How it's implemented is not that important. In the special case of 3 dimensions quaternions can be used for simpler representation, different performance characteristics and being less susceptible to rounding issues. More or less code is really not relevant. In case of 2 dimensions even cleaner than quaternions is using complex in polar form, ie. multiply with e^(i*scalar_in_radians). The expression in Rotate() having PI there suggest it evaluates to scalar_in_radians which is sufficient to describe a rotation. 4 hours ago, DanW58 said: "pos += u is the translation of pos..", EXACTLY. I think that pos is initially initialized to initialPos which my current theory says is the center of the spawning building, and that this line of code takes us from there to the exact place in the periphery where the next spawn will happen. If all of this is correct, my confusion could be due to the comment on the line of code being about a rotation of a vector about to be added TO the center of the building, not a rotation of the center of the building itself... yes, u is being rotated, u' being scaled and pos translated by u''. pos should be the centre of the building and pos' the spawn point of the unit. The comment suggest that there was some partitioning of the full circle happening before and how unwinding is done here, with some gibberish around it. At least that's how I read it. Quote Link to comment Share on other sites More sharing options...
DanW58 Posted February 5, 2021 Author Report Share Posted February 5, 2021 You're right, v is not being used here. 3 minutes ago, hyperion said: radius of structure + gap + radius of unit, how is this not adjusting length of u (assumed to be 1) to the distance between centre of building and spawn point? Now I'm starting to see... I thought all this stuff was in the denominator. I'm terrible at counting parenthesis... I'd be the worst LISP programmer. Now I understand. The last problem I was having was with my UnitVector class not having Multiply(), and was going "where does it need to multiply?" That last closing parenthesis before Multiply is giving me a rotated unit vector... BINGO! Thanks, gentlemen; this file compiles now. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.