c# – The best way to add gameTime to motion with acceleration and diagonal normalization?

c# – The best way to add gameTime to motion with acceleration and diagonal normalization?

[ad_1]

I lately began rewriting my engine from scratch, as a result of the outdated one was my first try and the construction was very messy. Every little thing has been going nice, however now I am being held again by some stuff associated to character motion and collision detection.

So, I wished to remake the character motion with acceleration and deceleration physics. I received it working tremendous, however I did not use gameTime.ElapsedGameTime.TotalSeconds. Now it will appear to be sophisticated and messy so as to add it to all of the locations the place I take advantage of velocity, Acceleration, and Deceleration

What I am questioning is that, is it value it to even use gameTime in any respect? I believe I’ve learn in lots of posts and tutorials that utilizing gameTime is greatest follow as a result of then motion is not coupled to border charge. And whether it is value utilizing it, how would you implement it in such a system together with acceleration and normalizing for diagonal motion?

I would not thoughts restructuring the code in any respect, however I simply do not know the place to begin from. I have been fascinated with and attempting to unravel this difficulty for thus lengthy and I do not know what to do. It is draining all my motivation 🙁 For those who discover another enhancements right here, be happy to say. Thanks.

Right here is the motion code:

    personal void UpdateMovement(Listing<GameObject> objects) {

        if (velocity != Vector2.Zero){

            if (SimpleCollisionCheck(objects, out GameObject obj) == true) {
                    DirectionalCollisionCheck(obj);
            }

        }

        place += velocity;

        if (velocity.X > 0) { // Lower X velocity over time
            if (velocity.X - Deceleration > 0) { velocity.X -= Deceleration; } else { velocity.X = 0; }
        } else if (velocity.X < 0) {
            if (velocity.X + Deceleration < 0) { velocity.X += Deceleration; } else { velocity.X = 0; }
        }

        if (velocity.Y > 0) { // Lower Y velocity over time
            if (velocity.Y - Deceleration > 0) { velocity.Y -= Deceleration; } else { velocity.Y = 0; }
        } else if (velocity.Y < 0) {
            if (velocity.Y + Deceleration < 0) { velocity.Y += Deceleration; } else { velocity.Y = 0; }
        }

    }

    protected void MoveLeft() {
        if (velocity.X - Acceleration > -VelocityMaximum) { velocity.X -= Acceleration + Deceleration; }
        path.X = -1;
    }

    protected void MoveRight() {
        if (velocity.X + Acceleration < VelocityMaximum) { velocity.X += Acceleration + Deceleration; }
        path.X = 1;
    }

    protected void MoveUp() {
        if (velocity.Y - Acceleration > -VelocityMaximum) { velocity.Y -= Acceleration + Deceleration; }
        path.Y = -1;
    }

    protected void MoveDown() {
        if (velocity.Y + Acceleration < VelocityMaximum) { velocity.Y += Acceleration + Deceleration; }
        path.Y = 1;
    }

Right here is the collision code:

    protected bool SimpleCollisionCheck(Listing<GameObject> objects, out GameObject obj) {

        Rectangle futureCollision = Collision;

        if (Velocity.X != 0) {
            if (Velocity.X > 0) { futureCollision.X += (int)VelocityMaximum; } else { futureCollision.X -= (int)VelocityMaximum; }
        }

        if (Velocity.Y != 0) {
            if (Velocity.Y > 0) { futureCollision.Y += (int)VelocityMaximum; } else { futureCollision.Y -= (int)VelocityMaximum; }
        }

        foreach (GameObject o in objects) {
            if (o != this && o.Energetic == true && o.CollisionEnabled == true && o.CollisionCheck(futureCollision) == true) {
                obj = o;
                return true;
            }
        }

        obj = null;
        return false;

    }

    personal bool CollisionLeft(GameObject o) {
        return ((Collision.Left - VelocityMaximum) < o.Collision.Proper) &&
            (Collision.Proper >= o.Collision.Proper) &&
            (Collision.Prime < o.Collision.Backside) &&
            (Collision.Backside > o.Collision.Prime);
    }

    personal bool CollisionRight(GameObject o) {
        return ((Collision.Proper + VelocityMaximum) > o.Collision.Left) &&
            (Collision.Left <= o.Collision.Left) &&
            (Collision.Prime < o.Collision.Backside) &&
            (Collision.Backside > o.Collision.Prime);
    }

    personal bool CollisionUp(GameObject o) {
        return ((Collision.Prime - VelocityMaximum) < o.Collision.Backside) &&
            (Collision.Backside >= o.Collision.Backside) &&
            (Collision.Left < o.Collision.Proper) &&
            (Collision.Proper > o.Collision.Left);
    }

    personal bool CollisionDown(GameObject o) {
        return ((Collision.Backside + VelocityMaximum) > o.Collision.Prime) &&
            (Collision.Prime <= o.Collision.Prime) &&
            (Collision.Left < o.Collision.Proper) &&
            (Collision.Proper > o.Collision.Left);
    }

    personal void DirectionalCollisionCheck(GameObject o) {

        if (velocity.X < 0.0f && CollisionLeft(o)) {

            if (CollisionNudgeEnabled && o.CollisionNudgeEnabled) {

                if ((PositionCentered.Y /*- CollisionThickness*/) < (o.Collision.Prime - collisionNudgeOffset)) {
                    velocity.Y = velocity.X;
                } else if ((PositionCentered.Y /*+ CollisionThickness*/) > (o.Collision.Backside + collisionNudgeOffset)) {
                    velocity.Y = -velocity.X;
                }
            }

            velocity.X = 0.0f;

        } else if (velocity.X > 0.0f && CollisionRight(o)) {

            if (CollisionNudgeEnabled && o.CollisionNudgeEnabled) {

                if ((PositionCentered.Y /*- CollisionThickness*/) < (o.Collision.Prime - collisionNudgeOffset)) {
                    velocity.Y = -velocity.X;
                } else if ((PositionCentered.Y /*+ CollisionThickness*/) > (o.Collision.Backside + collisionNudgeOffset)) {
                        velocity.Y = velocity.X;
                }
            }

            velocity.X = 0.0f;
        }

        if (velocity.Y < 0.0f && CollisionUp(o)) {

            if (CollisionNudgeEnabled && o.CollisionNudgeEnabled) {

                if (PositionCentered.X < (o.Collision.Left - collisionNudgeOffset)) {
                    velocity.X = velocity.Y;
                } else if (PositionCentered.X > (o.Collision.Proper + collisionNudgeOffset)) {
                    velocity.X = -velocity.Y;
                }
            }

            velocity.Y = 0.0f;

        } else if (velocity.Y > 0.0f && CollisionDown(o)) {

            if (CollisionNudgeEnabled && o.CollisionNudgeEnabled) {

                if (PositionCentered.X < (o.Collision.Left - collisionNudgeOffset)) {
                    velocity.X = -velocity.Y;
                } else if (PositionCentered.X > (o.Collision.Proper + collisionNudgeOffset)) {
                    velocity.X = velocity.Y;
                }
            }

            velocity.Y = 0.0f;
            }
        }
```

[ad_2]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply