Nez.Collider.onAddedToEntity C# (CSharp) Method

onAddedToEntity() public method

public onAddedToEntity ( ) : void
return void
		public override void onAddedToEntity()
		{
			if( _colliderRequiresAutoSizing )
			{
				// we only deal with boxes and circles here
				Assert.isTrue( this is BoxCollider || this is CircleCollider, "Only box and circle colliders can be created automatically" );

				var renderable = entity.getComponent<RenderableComponent>();
				Debug.warnIf( renderable == null, "Collider has no shape and no RenderableComponent. Can't figure out how to size it." );
				if( renderable != null )
				{
					var renderableBounds = renderable.bounds;

					// we need the size * inverse scale here because when we autosize the Collider it needs to be without a scaled Renderable
					var width = renderableBounds.width / entity.transform.scale.X;
					var height = renderableBounds.height / entity.transform.scale.Y;

					// circle colliders need special care with the origin
					if( this is CircleCollider )
					{
						var circleCollider = this as CircleCollider;
						circleCollider.radius = Math.Max( width, height ) * 0.5f;

						// fetch the Renderable's center, transfer it to local coordinates and use that as the localOffset of our collider
						localOffset = renderableBounds.center - entity.transform.position;
					}
					else
					{
						var boxCollider = this as BoxCollider;
						boxCollider.width = width;
						boxCollider.height = height;

						// fetch the Renderable's center, transfer it to local coordinates and use that as the localOffset of our collider
						localOffset = renderableBounds.center - entity.transform.position;
					}
				}
			}
			_isParentEntityAddedToScene = true;
			registerColliderWithPhysicsSystem();
		}