Example #1
0
void Rigidbody::Update(float dt) {
	Transform *transform = _entity->GetTransform();
	Vec2 v = vel * dt;

	Vector<RayPointData> rays;
	float eps = 0.01f;
	for (int i = 0; i < _xPoints + 2; i++) {
		float x = transform->pos.x + eps + (transform->size.x - 2 * eps) * i / (_xPoints + 1);
		if (v.y <= 0.0f) {
			rays.push_back({ { x, transform->pos.y }, { 0.0f, v.y }, CD_Up });
		}
		if (v.y >= 0.0f) {
			CollisionDirection dir = CD_Down;
			if (i == 0) {
				dir = (CollisionDirection)(dir | CD_DownLeftSide);
			}
			else if (i == _xPoints + 1) {
				dir = (CollisionDirection)(dir | CD_DownRightSide);
			}
			rays.push_back({ { x, transform->pos.y + transform->size.y }, { 0.0f, v.y }, dir });
		}
	}
	for (int i = 0; i < _yPoints + 2; i++) {
		float y = transform->pos.y + eps + (transform->size.y - 2 * eps) * i / (_yPoints + 1);
		if (v.x <= 0.0f) {
			rays.push_back({ { transform->pos.x, y }, { v.x, 0.0f }, CD_Left });
		}
		if (v.x >= 0.0f) {
			rays.push_back({ { transform->pos.x + transform->size.x, y }, { v.x, 0.0f }, CD_Right });
		}
	}

	Collider *collider = GetComponent<Collider>();
	_collidedDirs = CD_None;
	static int frame = 0;
	frame++;
	for (auto r : rays) {
		float dist = FLT_MAX;
		if (collider->Raycast(r.pos, r.pos + r.dir, &dist, _layerMask)) {
			_collidedDirs = (CollisionDirection)(_collidedDirs | r.colDir);
			if (r.colDir & CD_Horizontal) {
				v.x = absMin(v.x, dist * sign1(v.x));
			}
			else {
				v.y = absMin(v.y, dist * sign1(v.y));
			}
		}
	}

	if (_collidedDirs & CD_Vertical) {
		vel.y = 0.0f;
	}
	if (!(_collidedDirs & CD_Down)) {
		vel.y += kGravity * dt;
	}

	transform->pos += v;
}
Example #2
0
//----------------------------------------------------------------------------//
Sizef Element::calculatePixelSize(bool skipAllPixelAlignment) const
{
    // calculate pixel sizes for everything, so we have a common format for
    // comparisons.
    Sizef absMin(CoordConverter::asAbsolute(d_minSize,
        getRootContainerSize(), false));
    Sizef absMax(CoordConverter::asAbsolute(d_maxSize,
        getRootContainerSize(), false));

    Sizef base_size;
    if (skipAllPixelAlignment)
    {
        base_size = Sizef((d_parent && !d_nonClient) ?
                           d_parent->getUnclippedInnerRect().getFresh(true).getSize() :
                           getParentPixelSize(true));
    }
    else
    {
        base_size = Sizef((d_parent && !d_nonClient) ?
                           d_parent->getUnclippedInnerRect().get().getSize() :
                           getParentPixelSize());
    }

    Sizef ret = CoordConverter::asAbsolute(d_area.getSize(), base_size, false);

    // in case absMin components are larger than absMax ones,
    // max size takes precedence
    if (absMax.d_width != 0.0f && absMin.d_width > absMax.d_width)
    {
        absMin.d_width = absMax.d_width;
        CEGUI_LOGINSANE("MinSize resulted in an absolute pixel size with "
                        "width larger than what MaxSize resulted in");
    }

    if (absMax.d_height != 0.0f && absMin.d_height > absMax.d_height)
    {
        absMin.d_height = absMax.d_height;
        CEGUI_LOGINSANE("MinSize resulted in an absolute pixel size with "
                        "height larger than what MaxSize resulted in");
    }

    // limit new pixel size to: minSize <= newSize <= maxSize
    if (ret.d_width < absMin.d_width)
        ret.d_width = absMin.d_width;
    else if (absMax.d_width != 0.0f && ret.d_width > absMax.d_width)
        ret.d_width = absMax.d_width;

    if (ret.d_height < absMin.d_height)
        ret.d_height = absMin.d_height;
    else if (absMax.d_height != 0.0f && ret.d_height > absMax.d_height)
        ret.d_height = absMax.d_height;

    if (d_aspectMode != AM_IGNORE)
    {
        // make sure we respect current aspect mode and ratio
        ret.scaleToAspect(d_aspectMode, d_aspectRatio);

        // make sure we haven't blown any of the hard limits
        // still maintain the aspect when we do this
        if (d_aspectMode == AM_SHRINK)
        {
            float ratio = 1.0f;
            // check that we haven't blown the min size
            if (ret.d_width < absMin.d_width)
            {
                ratio = absMin.d_width / ret.d_width;
            }
            if (ret.d_height < absMin.d_height)
            {
                const float newRatio = absMin.d_height / ret.d_height;
                if (newRatio > ratio)
                    ratio = newRatio;
            }

            ret.d_width *= ratio;
            ret.d_height *= ratio;
        }
        else if (d_aspectMode == AM_EXPAND)
        {
            float ratio = 1.0f;
            // check that we haven't blown the min size
            if (absMax.d_width != 0.0f && ret.d_width > absMax.d_width)
            {
                ratio = absMax.d_width / ret.d_width;
            }
            if (absMax.d_height != 0.0f && ret.d_height > absMax.d_height)
            {
                const float newRatio = absMax.d_height / ret.d_height;
                if (newRatio > ratio)
                    ratio = newRatio;
            }

            ret.d_width *= ratio;
            ret.d_height *= ratio;
        }
        // NOTE: When the hard min max limits are unsatisfiable with the aspect lock mode,
        //       the result won't be limited by both limits!
    }

    if (d_pixelAligned)
    {
        ret.d_width = CoordConverter::alignToPixels(ret.d_width);
        ret.d_height = CoordConverter::alignToPixels(ret.d_height);
    }

    return ret;
}