Grounded boolean question.

private var grounded:boolean = false; 
  
function Update () {
var controller:CharacterController = GetComponent(CharacterController); 
var flags = controller.Move(moveDirection * Time.deltaTime); 
grounded = (flags  CollisionFlags.Below) != 0;    
......
.....
}

I saw this code is for judge if character is grounded or jumping now.

Q1.
What is that ? Is this same with in javascript?

Q2.
grounded is boolean, then if (flags CollisionFlags.Below) is 0, grounded become true?
Is that possible use and define boolean var like that way?

is bitwise-AND
is the boolean AND operator

  • returns a bit pattern with ones only wherever a 1 occurs in both inputs
    1010 0100 = 0000
    1010 1000 = 1000
    1010 1011 = 1010

It’s not being defined there; it’s defined at the top of the script ( var grounded : boolean). I’m not quite sure what you mean.
Booleans can be true or false; != returns either true or false, so it’s perfectly acceptable to assign it to a boolean.

Thanks for reply.

I mean normally, boolean be defined by logical process, and just wrote like grounded = true; or grounded = false;

But that code define boolean as a some strange way.

grounded stores a simple true/false value as usual.

private var grounded:boolean = false;

Okay, so grounded = false.

grounded = (flags  CollisionFlags.Below) != 0;

Suppose (flags CollisionFlags.Below) evaluates to 1. Then that statement simplifies to

grounded = (1 != 0);

a.k.a.

grounded = (true);

Again, grounded stores a simple true/false value. :slight_smile:

Sorry but I can’t understand this. What this means?

Hi. Thx for reply.

Then,

grounded = (flags CollisionFlags.Below) != 0;

What that just [flasgs] means? It represents 0 or 1 ? or true or false? or what? Above, flags is controller.Move(moveDirection * Time.deltaTime);
So what happen to flags?

And CollisionFlags.Below means character controller is collide with terrain or other collider of ground, then become 1? or true?

So grounded can be changed to false automatically?

The approach that is being used here with the variable “flags” dates back to an earlier era of computing where programmers routinely thought of numbers as strings of bits. But really a string of bits is just a binary (base 2) representation of a number. So, we can write numbers in binary like this:

5 = 101
16 = 10000
19 = 10011
255 = 11111111

…and so on.

Now the bitwise AND operator (written as ‘’ and not be confused with ‘’) is a bit of an oddity in that it works directly with these binary digits (0s and 1s) as though they were boolean values. An example may make this clearer…

  • We know that (false true) = false, which is normal boolean logic.
  • Suppose that we represent true as the binary digit ‘1’ and false as the binary digit ‘0’.
  • Now we can use bitwise logic to write the same expression as: (0 1) = 0.

Bitwise logic is useful because we can use it on all the bit positions of a number at once. Here’s a step by step example.

  • Begin with the expression (254 17). This may look weird if you’re not used to bitwise logic. What does it even mean?
  • Well… expand the numbers into binary and you get (11111110 10001). Remember that ‘1’ is true and ‘0’ is false.
  • So, look at the rightmost bit (digit) of each number. In that position we have (0 1). We know what that is, it’s 0.
  • Proceed right to left, one bit at a time. We put each answer bit in the same position it came from, so we get 10000.
  • Bit positions with no digit in them are zeroes, just like regular arithmetic.
  • OK, so our answer is 10000, which we can convert back to decimal. It’s 16.
  • So (254 17) = 16.

Now we’re ready to look at this “flags” idea.

The variable “flags” is just a sneaky way to pack lots of boolean variables into a single integer variable. By doing a bitwise AND operation with the CollisionFlags.Below constant, the programmer here is saying “I want to see what this one boolean value from my set of boolean values is”.

The catch is, the expression (flags CollisionFlags.Below) is not itself a boolean! Nor is it simply 0 or 1. The two values it can take are 0 or CollisionFlags.Below (which will be an integer number with exactly one ‘1’ digit in when written in binary). But we can still think of this as a boolean value. 0 is ‘false’ as before and this time CollisionFlags.Below means ‘true’. The simplest way to test which of these two cases we have is just to test for zero, like this:

(flags CollisionFlags.Below) != 0

Now this is (finally) a boolean value, because the != operator is an infix operator that returns a boolean. We have what we want!

So this line:

grounded = (flags CollisionFlags.Below) != 0

Means “check the single binary digit of the integer ‘flags’ that corresponds to the constant CollisionFlags.Below and if that digit is set then ‘grounded’ is true, otherwise it is false”.

Did that make sense?

Wow. Thank you very much.

I think I don’t understand this 100%. It seems there are more deep something in this. But I understand about 60%.