raycast and mask in Unity

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

raycast and mask in Unity

Postby blackbird_dream » Thu Aug 09, 2018 9:43 am

Maybe this question for Julio and Sweenie :
how to handle mask in Raycast.
I understand the mask value defines the layers to exclude.
Waht is the meaning of the instruction
Code: Select all
var excludemask = (1 << 9);
in c#
I don't understand.
User avatar
blackbird_dream
 
Posts: 279
Joined: Wed Jun 07, 2006 3:08 pm
Location: Italy

Re: raycast and mask in Unity

Postby JernejL » Thu Aug 09, 2018 10:24 am

it's bit arithmetic, it takes bits from number 1 (which conviniently is just 1 bit) and pushes it left 9 bits, making it a number which only has bit 9 set, for some bit boolean masks, i guess it would work better if the mask layers would be defined as constants.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1495
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: raycast and mask in Unity

Postby blackbird_dream » Thu Aug 09, 2018 10:33 am

thks for your explanation
It seems the mask doesn't work or I'm misusing it.
I wrote :
Code: Select all
const int mask=8;

it still casts the mesh whenever I change the layer in the mesh editor
If I write
Code: Select all
var mask=(1<<8);
the cast is no more effective for any value of the layer assigned in the mesh editor
User avatar
blackbird_dream
 
Posts: 279
Joined: Wed Jun 07, 2006 3:08 pm
Location: Italy

Re: raycast and mask in Unity

Postby blackbird_dream » Thu Aug 09, 2018 10:57 am

ok
Its' the field
layer
of the treecollider editor which has to be set to the desired value and which is used
User avatar
blackbird_dream
 
Posts: 279
Joined: Wed Jun 07, 2006 3:08 pm
Location: Italy

Re: raycast and mask in Unity

Postby Sweenie » Fri Aug 10, 2018 2:09 am

Check Demo 23 for an example.

In that demo I have defined a custom layer.
Check top right corner of the Inspector, there you can customize the layers.
Basically a layer is defined as a bit in a 32-bit number so there can be 32 different layers defined.(I think)
In demo 23 I defined bit 9 to be "No Newton Raycast"
Then for the capsule I changed the field "Layer" on the capsule collider to be assigned to layer 9(bit 9).

In the raycast script I set the exclusionmask to be 1 << 9 which makes the raycast detect any collider except the collider assigned to layer 9.
Sweenie
 
Posts: 490
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: raycast and mask in Unity

Postby blackbird_dream » Fri Aug 10, 2018 2:42 am

Thks Sweenie
Why the instruction
Code: Select all
var mask=9;
doesn't give the same result ?
or the equivalent int with the 9th bit set ?
User avatar
blackbird_dream
 
Posts: 279
Joined: Wed Jun 07, 2006 3:08 pm
Location: Italy

Re: raycast and mask in Unity

Postby blackbird_dream » Fri Aug 10, 2018 2:50 am

Ok I understand
the correct equivalent instruction is :
Code: Select all
var mask=512;

512 DEC = 1000000000 BIN (9th bit set on)
User avatar
blackbird_dream
 
Posts: 279
Joined: Wed Jun 07, 2006 3:08 pm
Location: Italy


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 6 guests