-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
First shot at extracting IntVector3 from Vector3 #96
Conversation
Maybe add a Vector3 class with static method like add, substract... Each method can have float|int for each position parameters and in function we choose if we take FloatVector3 or IntVector3
FloatVector3 & IntVector3 would just be a class like:
|
Yeah, I did consider that. Main reason I don't like it is because it would make chained operations a lot more verbose. $this->motion = $this->motion->addVector($vector->normalize()->multiply($d)); vs $this->motion = Vector3::addVector($this->motion, Vector3::multiply(Vector3::normalize($vector), $d)); |
Also looked into using PHPStan generics, but generics of primitive types are not well supported. |
Maybe make an in-between. Syntax will be:
(I think replace |
This system can work with Vector2 too (if we want implements operation between them later):
|
God no, that looks awful |
There's also no proper way to add a Vector2 to a Vector3, since we can't know which coordinates the caller wanted adding together. |
Ok for Vector2, but the idea of separating functions that require a vector and not is problematic ? |
Following internals discussions it became apparent that the only need for an int vector is for block positions so it makes more sense to specialize it. |
I've stalled to do this for years because I can't quite figure out how to make the API work nice.
I thought if I just ploughed ahead with it it might become clearer. I'm not happy with what I came up with, so I'm hoping to start some debate about this.
The
IntVector3
API differs fromVector3
in the following:Vector3
instead works withIntVector3
getX
,getY
,getZ
(redundant)getFloorX
,getFloorY
,getFloorZ
(useless)ceil
,floor
,round
(useless)divide
(would return float vector)normalize
(would return float vector)getIntermediateWith*Value
(would return float vector)Problems
I don't want to couple
IntVector3
andVector3
, but I don't see a choice if we want to maximize usability.The following functions don't care whether their inputs are int or float vectors, but implementing them on both sides requires duplicating code:
getIntermediateWith*Value
(also must return float vector3)normalize
(must return float vector3)divide
(must return float vector3)minComponents
,maxComponents
,sum
do not care about input types, but to get separation of types requires 2 functionsdot
,cross
don't care about left or right typesdistance
,distanceSquared
don't care about left or right typesBasically all float vector ops would accept int vectors
Big question: Where do we draw the line and tell people to just make a FloatVector3 from an IntVector3? If we don't want to force them to do that, how do we design the API?
My current approach is to delete anything from IntVector3 that would involve floats
Probably will make
FloatVector3
acceptFloatVector3|IntVector3
, but this creates symmetry issues (e.g.$v3f->add($v3i)
would be OK but$v3i->add(v3f)
would not, despite both being mathematically valid)