# Little Math question

**URL:** <https://forum.castle-engine.io/t/little-math-question/853>\
**Category:** Uncategorized\
**Created:** [July 6, 2023, 7:52pm UTC](https://forum.castle-engine.io/t/little-math-question/853 "2023-07-06T19:52:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carring](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/carring/32/1950_2.png) [@Carring](https://forum.castle-engine.io/u/Carring)\
**Post date:** [July 6, 2023, 7:52pm UTC](https://forum.castle-engine.io/t/little-math-question/853/1 "2023-07-06T19:52:15Z")

</div>

Hi,

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?

---

<div class="post-metadata">

**Author:** ![eugeneloza](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/eugeneloza/32/12_2.png) [@eugeneloza](https://forum.castle-engine.io/u/eugeneloza)\
**Post date:** [July 6, 2023, 8:12pm UTC](https://forum.castle-engine.io/t/little-math-question/853/2 "2023-07-06T20:12:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Carring](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/carring/32/1950_2.png) [@Carring](https://forum.castle-engine.io/u/Carring)\
**Post date:** [July 7, 2023, 5:01am UTC](https://forum.castle-engine.io/t/little-math-question/853/3 "2023-07-07T05:01:03Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![michalis](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/michalis/32/3_2.png) [@michalis](https://forum.castle-engine.io/u/michalis)\
**Post date:** [July 8, 2023, 12:47am UTC](https://forum.castle-engine.io/t/little-math-question/853/4 "2023-07-08T00:47:51Z")

</div>

> [@Carring](#):
>
> 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.

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

```

See TVector3 docs, they lead to [Castle Game Engine: CastleVectorsInternalSingle: Packed Record TGenericVector3](https://castle-engine.io/apidoc/html/CastleVectorsInternalSingle.TGenericVector3.html) .

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 🙂

---

<div class="post-metadata">

**Author:** ![Carring](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/carring/32/1950_2.png) [@Carring](https://forum.castle-engine.io/u/Carring)\
**Post date:** [July 8, 2023, 9:24am UTC](https://forum.castle-engine.io/t/little-math-question/853/5 "2023-07-08T09:24:28Z")

</div>

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

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).

```auto
  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.

```

> [@michalis](#):
>
> 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.

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”).  
🙂

---

<div class="post-metadata">

**Author:** ![michalis](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/michalis/32/3_2.png) [@michalis](https://forum.castle-engine.io/u/michalis)\
**Post date:** [August 24, 2023, 2:12pm UTC](https://forum.castle-engine.io/t/little-math-question/853/6 "2023-08-24T14:12:22Z")

</div>

> [@Carring](#):
>
> 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).
> 
> ```auto
> 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.
> 
> ```

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.

> [@](#):
>
> 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”).

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

---

<div class="post-metadata">

**Author:** ![Carring](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/carring/32/1950_2.png) [@Carring](https://forum.castle-engine.io/u/Carring)\
**Post date:** [August 25, 2023, 6:51am UTC](https://forum.castle-engine.io/t/little-math-question/853/7 "2023-08-25T06:51:17Z")

</div>

> [@michalis](#):
>
> 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.

Ah, ok. I thought that my second line would overrule the Max line. 🙂  
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.  
Thanks!
