Little Math question


I want to move a part of a scene 15 pixels down on screen in an update procedure.
PartY := PartY - 15;

Problem is that if the initial Y value is negative (I have a part that is connected to a scene but with an initial Y value of -500 from the 0,0 Translation of the main scene the result will be that the part is going up 15 pixels (because -500 - 15 = 485).

How can I get Y to be always decreasing?

No, -15 is always “-15”. This means that the scene should go down (relative to local Y axis), always. -500 - 15 = -(500+15) = -515.

However, note that “15” in this case - is not “15 pixels”, but “15 units” (units are most often meant as meters). How exactly units (meters) convert into pixels on the screen depends on your camera paramters/orientation. I believe that with Viewport 2D and default camera settings and no other scaling applied : indeed 15 units should be 15 pixels on screen. However, again, if the camera and/or scene and/or their parent transforms have been moved, rotated or scaled then this will no longer be true.

You’re right.
I found out that I had a line that if Y value was < - 1000 it stopped going down and another procedure made it going up again.

Now I also have replaced the Y which was a single var with a Vector3.

PartXYZ := PartXYZ - 15;

Now how can I get only the Y value of the Vector3 and make it that if it is below -1000 it stops going down?

To get Y component of TVector3, just access the Y component. To stop it from going below, use e.g. Math.Max.

MyVector.Y := Max(-1000.0, MyVector.Y - 15);

See TVector3 docs, they lead to Castle Game Engine: CastleVectorsInternalSingle: Packed Record TGenericVector3 .

P.S. Sorry for silence from me on the forum. I was on vacation for 1 week and then (Pascal) conference for 1 week. Once I’ll be back and rest, I’ll be sure to catch up with all forum threads and any questions that may be pending. Though I see a lot of good activity so maybe everything was just already answered :slight_smile:

@michalis Welcome back! I hope you had a good vacation and conference time. :slight_smile:

Thanks for the “Max” tip.
How can I use it to pass it to a boolean to stop it actually (or maybe make DropPart a function).

  PartXYZ[I].Y := Max(-1000.0, PartXYZ[I].Y - 15);
   If PartXYZ[I].Y = -1000.0 then DropPart := false;  // this probably is a wrong line without use of the above Max statement.

Yes, I have 2 pending questions, about the use of TCasteBehavior to light up a range of scenes (code attachted) and for an example with usage of MouseRayHit to light up a scene only if it hits a non transparant part (so actually an expansion of the above “lighten up”).

Sorry for delay, again!

From what I see, you did answer your question above :), in the sense: the check

if PartXYZ[I].Y = -1000.0 ...

makes sense after you use

"PartXYZ[I].Y := Max(-1000.0, ...)"

earlier. Using the ... := Max(-1000.0, ...) guarantees that anything below -1000 will eventually change to something equal exactly -1000.

Ack. I’ll try to finally catch up with my forum backlog this week.

Ah, ok. I thought that my second line would overrule the Max line. :slight_smile:
So decreasing Y stops at -1000, that is what I need, because if I reverse it to place a Scene back to the original Y before it dropped it would go beyond the initial Y it if I add 5 to every loop of Y (I do not add 1 because it would go too slow). With the use of Max I can force it to the intitial Y value.

1 Like