2 Replies - 406 Views - Last Post: 03 July 2017 - 09:02 AM Rate Topic: -----

#1 eazyyzae  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 23-February 13

Floating Moving Platform strange behaviour!

Posted 03 July 2017 - 05:59 AM

Right the next thing in my game that is a bit quirky is my floating moving platforms.

After a while I thought I had them only to find out they weren't quite right.

1st problem is the up and down motion platform. My player character bounces on them when it's going down.

2nd problem is the left and right. I have 2 options here. I can have free movement on this platform where the player doesn't follow the platform or where the player does follow the platform only to be stuck to the left most edge of it. Unless I jump off it of course.

I should be able to jump through the bottom of these platforms and land on the top. Which it does.

I'm not even sure if I can do this in this class but I would like the class to handle itself if at all possible.

I think a video should explain better. I hope you can see the bounces. "8-bit Link" is just a sprite as I haven't created my own yet. :(/> artistically challenged

Youtube vid

(Sorry for the youtube link, the more I tried to make the video less than 2MB the bigger it got.)

if I change position.X to e.Position.X in the OnCollision function that gives me free movement on the left and right platforms.


Any way here's the code;

 public class FloatingPlatform : Entity
    {

        public override void LoadContent(ContentManager content, List<string> attributes, List<string> contents, InputManager input)
        {
            base.LoadContent(content, attributes, contents, input);

            moveAnimation.DrawColor = Color.White;
            moveAnimation.IsActive = true;
            isAlive = true;

            origPosition = position;

            if (direction == 0)
                destPosition.Y = origPosition.Y - range;
            else if (direction == 1)
                destPosition.Y = origPosition.Y + range;
            else if (direction == 2)
                destPosition.X = origPosition.X - range;
            else if (direction == 3)
                destPosition.X = origPosition.X + range;


            int tempRange = range;
        }

        public override void Update(GameTime gameTime, InputManager inputManager, Collision col, Layer layer)
        {
            base.Update(gameTime, inputManager, col, layer);
            moveAnimation.IsActive = true;
            ssAnimation.Update(gameTime, ref moveAnimation);

            UDLR(gameTime);

            position += velocity;
            ssAnimation.Update(gameTime, ref moveAnimation);
            moveAnimation.Position = position;
        }


        /// <summary>
        /// Deals with the UP, DOWN, LEFT, RIGHT movement of the platforms
        /// </summary>
        /// <param name="gameTime"></param>
        private void UDLR(GameTime gameTime)
        {
            if (direction == 0)
            {
                velocity.Y = -moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
            else if (direction == 1)
            {
                velocity.Y = moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

            }
            else if (direction == 2)
            {
               velocity.X = -moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
            else if (direction == 3)
            {
               velocity.X = moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }

            /// This bit helps make the platforms swap directions when at the end of range. (UP and DOWN)
            if (direction == 0)
            {
                if ((direction == 0 && position.Y <= destPosition.Y))
                {
                    direction = 1;
                }

                if (direction == 1 && position.Y >= origPosition.Y)
                {
                    direction = 0;
                }
            }
            if (direction == 1)
            {
                if ((direction == 1 && position.Y >= destPosition.Y))
                {
                    direction = 0;
                }

                if (direction == 0 && position.Y <= origPosition.Y)
                { 
                    direction = 1;
                }
            }
            /// This bit helps make the platforms swap directions when at the end of range. (LEFT and RIGHT)
            if (direction == 2)
            {
                if ((direction == 2 && position.X <= destPosition.X))
                {
                    direction = 3;
                }

                if (direction == 3 && position.X >= origPosition.X)
                {
                    direction = 2;
                }
            }
            if (direction == 3)
            {
                if ((direction == 3 && position.X >= destPosition.X))
                {
                    direction = 2;
                }

                if (direction == 2 && position.X <= origPosition.X)
                {
                    direction = 3;
                }
            }
        }

        /// <summary>
        /// Handles One-Way directional platforms.  The player can jump up through
        /// the platform and then land on it. Sets gravity to false so the player doesn't fall back through.
        /// </summary>
        /// <param name="e"></param>
        public override void OnCollision(Entity e)
        {
            FloatRect rect = new FloatRect(position.X, position.Y, Layer.TileDimensions.X, Layer.TileDimensions.Y);
            
            FloatRect preve = new FloatRect(e.PrevPosition.X, e.PrevPosition.Y, e.Animation.FrameWidth, e.Animation.FrameHeight);
            FloatRect prevTile = new FloatRect(prevPosition.X, prevPosition.Y, Layer.TileDimensions.X, Layer.TileDimensions.Y);

            if (e.Rect.Bottom >= rect.Top )
            {
                if (direction == 0) /// UP
                {
                    e.ActivateGravity = false;
                    e.Position = new Vector2(e.Position.X, rect.Top - e.Animation.FrameHeight);            
                    e.OnTile = true;
                    e.JumpKeyEnabled = true;
                    e.startPos = e.Position.Y;
                }

                if (direction == 1) /// DOWN
                {
                    e.ActivateGravity = false;
                    e.Position = new Vector2(e.Position.X, rect.Top - e.Animation.FrameHeight);                 
                    e.OnTile = true;
                    e.JumpKeyEnabled = true;
                    e.startPos = e.Position.Y;
                }                           
                                       
                if (direction == 2) /// LEFT
                {
                    e.Position = new Vector2(position.X, rect.Top - e.Animation.FrameHeight );
                    e.ActivateGravity = false;
                    e.OnTile = true;
                    e.JumpKeyEnabled = true;
                    e.startPos = e.Position.Y;

                }

                if (direction == 3) /// RIGHT
                {
                    e.Position = new Vector2(position.X, rect.Top - e.Animation.FrameHeight);
                    e.ActivateGravity = false;
                    e.OnTile = true;
                    e.JumpKeyEnabled = true;
                    e.startPos = e.Position.Y;
                }


            }

            base.OnCollision(e);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            base.Draw(spriteBatch);
            if (isAlive)
                moveAnimation.Draw(spriteBatch);
        }  
    }


Thanks for reading,

Joe

Is This A Good Question/Topic? 0
  • +

Replies To: Floating Moving Platform strange behaviour!

#2 eazyyzae  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 23-February 13

Re: Floating Moving Platform strange behaviour!

Posted 03 July 2017 - 07:10 AM

As for the moving down on the platform if I just add 2 to the

e.Position = new Vector2(e.Position.X, rect.Top - e.Animation.FrameHeight+2);

It seems to eliminate the visual effect.

But it does feel like a workaround rather than a solution.

if (direction == 1) /// DOWN
                {
                    e.ActivateGravity = false;
                    e.Position = new Vector2(e.Position.X, rect.Top - e.Animation.FrameHeight+2);                 
                    e.OnTile = true;
                    e.JumpKeyEnabled = true;
                    e.startPos = e.Position.Y;
                }

Was This Post Helpful? 0
  • +
  • -

#3 eazyyzae  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 23-February 13

Re: Floating Moving Platform strange behaviour!

Posted 03 July 2017 - 09:02 AM

Never mind about the left and right platform. I think I found a solution. I'll post it here for anyone else anyway. Although I'm still not happy with the up n down.

I just added 2 floats right and left
and added them to here;
private void UDLR(GameTime gameTime)
        {
            if (direction == 0)
            {
                velocity.Y = -moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
            else if (direction == 1)
            {
                velocity.Y = moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

            }
            else if (direction == 2)
            {
               left = velocity.X = -moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

            }
            else if (direction == 3)
            {
               right = velocity.X = moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }


Then just changed the vectors a little here;
                if (direction == 2) /// LEFT
                {
                    e.Position = new Vector2(velocity.X += (e.Position.X + left), rect.Top - e.Animation.FrameHeight );
                    e.ActivateGravity = false;
                    e.OnTile = true;
                    e.JumpKeyEnabled = true;
                    e.startPos = e.Position.Y;
                }

                if (direction == 3) /// RIGHT
                {
                    e.Position = new Vector2(velocity.X += (e.Position.X + right), rect.Top - e.Animation.FrameHeight);
                    e.ActivateGravity = false;
                    e.OnTile = true;
                    e.JumpKeyEnabled = true;
                    e.startPos = e.Position.Y;
                }

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1