V
V
Vladimir Korshunov2021-07-16 16:06:08
C++ / C#
Vladimir Korshunov, 2021-07-16 16:06:08

Is it possible to change the logical operator depending on the variable?

There is this expression:

if (x_direction > 0)
{
    if (f_pos.x > R)
    rot_direction2 = false;
}
else
{
    if (f_pos.x < -R)
    rot_direction2 = false;
}

In this case, the value of the x_direction variable can only be 1 or -1. I really want to cut it all into one condition. You can already add R*x_direction, only the > sign should also change to <, but I don’t understand how to do this. That is, I want to write something like <*x_direction, but, of course, you can’t do that. Somehow, through algebra-logic, it also doesn’t work, are there any options at all to write all this condition into one? It just doesn't look very good here.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2021-07-16
@GashhLab

if (f_pos.x * x_direction > R) {
    rot_direction2 = false;
}

G
GavriKos, 2021-07-16
@GavriKos

Well, at least this can be combined into one if with a complex condition:

if ((x_direction > 0 && f_pos.x > R) || (x_direction < 0 && f_pos.x < -R))

You can mathematically still drag ;-)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question