Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Ball doesnt change his start position  (Read 1454 times)

0 Members and 1 Guest are viewing this topic.

moonyTown

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Ball doesnt change his start position
« on: March 31, 2020, 06:09:23 pm »
Hi There,

i have a problem with the rotation. My goal was to rotate a Rect and make it shoot by pressing left mouse button or space.
The rotation works but not the shoot. The ball always fly away from the same position no matter if the Rect rotate or not.

Here an example picture

https://imgur.com/CDmGOdI
https://imgur.com/60wauzO

using SFML.Graphics;
using SFML.System;
using SFML.Audio;
using SFML.Window;
using System;
using System.Collections.Generic;

namespace Pong3final
{
    class Player : RectangleShape
    {
        public float Speed;
        public float RotationMovement;
        public List<CircleShape> ShotList = new List<CircleShape>();
        float TimeSinceLastShot;

        public bool PlayerSettingMouse = false;
        public bool PlayerSettingKeyboard = true;

        public Player(Vector2f size) : base(size)
        {
            Origin = new Vector2f(Position.X + Size.X / 2, Position.Y + Size.Y * 2 / 3);

            if (PlayerSettingMouse)
                PlayerSettingKeyboard = false;

            if (PlayerSettingKeyboard)
                PlayerSettingMouse = false;
        }
        public void Rotate(float deltaTime, Window window)
        {
            if (PlayerSettingKeyboard)
            {
                if (Keyboard.IsKeyPressed(Keyboard.Key.Right))
                {
                    RotationMovement += 150 * deltaTime;
                }
                if (Keyboard.IsKeyPressed(Keyboard.Key.Left))
                {
                    RotationMovement -= 150 * deltaTime;
                }

                Rotation = -RotationMovement;
            }

            if (PlayerSettingMouse)
            {
                Vector2i MousePosition = Mouse.GetPosition(window);

                float dx = MousePosition.X - Position.X;
                float dy = MousePosition.Y - Position.Y;

                RotationMovement = (float)(Math.Atan2(dx, dy) * (180 / Math.PI));

                Rotation = -RotationMovement;
            }
        }

        public void Shot(float deltaTime)
        {

            if (PlayerSettingKeyboard)
            {
                TimeSinceLastShot += deltaTime;

                if (Keyboard.IsKeyPressed(Keyboard.Key.Space) && TimeSinceLastShot > 0.5f)
                {
                    TimeSinceLastShot = 0;
                    CircleShape Shot = new CircleShape(5);
                    Shot.Position = new Vector2f(Position.X, Position.Y + Size.Y * 2 / 3);
                    ShotList.Add(Shot);
                }

                foreach (CircleShape shots in ShotList)
                {
                    shots.Position += new Vector2f(Position.X - Position.X + Size.X, Position.Y - Position.Y + Size.Y) * deltaTime;
                }
            }

            if (PlayerSettingMouse)
            {
                TimeSinceLastShot += deltaTime;

                if (Mouse.IsButtonPressed(Mouse.Button.Left) && TimeSinceLastShot > 0.5f)
                {
                    TimeSinceLastShot = 0;
                    CircleShape Shot = new CircleShape(5);
                    Shot.Position = new Vector2f(Position.X, Position.Y + Size.Y * 2 / 3);
                    ShotList.Add(Shot);
                }

                foreach (CircleShape shots in ShotList)
                {                  
                    shots.Position += new Vector2f(Position.X - Position.X + Size.X, Position.Y - Position.Y + Size.Y) * deltaTime;
                }
            }

        }
    }
}
[code=csharp]
« Last Edit: March 31, 2020, 06:26:19 pm by moonyTown »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Rotation Position
« Reply #1 on: March 31, 2020, 06:22:13 pm »
This:
(Position.X - Position.X + Size.X, Position.Y - Position.Y + Size.Y)
is just
(Size.X, Size.Y)

Didn't look at all of your code but it doesn't seem that you are adding any sort of rotation vector to the position of the circle to tell it which direction to go.
You're going to need sines and cosines for that vector. You need to calculate the cartesian co-ordinates for a given rotation direction, in case you need something specific to look up. ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

moonyTown

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Ball doesnt change his start position
« Reply #2 on: March 31, 2020, 08:37:47 pm »
OK thanks, now I see the mistake by the movement
 Position.X - Position.X + Size.X, Position.Y - Position.Y + Size.Y)
 

But, why need the circle a rotation? I just want that the start position of the circle is always by the top of the rect.
This was my attempt but it failed.
CircleShape Shot = new CircleShape(5);
                    Shot.Position = new Vector2f(Position.X, Position.Y + Size.Y * 2 / 3);
                    ShotList.Add(Shot);
                }
           

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Ball doesnt change his start position
« Reply #3 on: March 31, 2020, 11:49:42 pm »
One thing you need to remember is that when the rectangle is rotated, the circle isn't linked to it in any way so if you want the circle's movement to mimic the rotation of the rectangle, the direction needs to be taken into account.

That said, if the position is incorrect, something is else is wrong.

Since the rectangle is rotating around its origin and you want the circle to be positioned at that origin, simply set the circle's position to the exact same position of the rectangle ("position" is technically the position of the origin).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

moonyTown

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Ball doesnt change his start position
« Reply #4 on: April 01, 2020, 12:20:37 am »
What do you mean by isn't linked,  isn't it enough to write something like Shot. Position = Rect. position like I did for the start position before the movement begin.

But okee I will try to implement what you wrote and post it later?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Ball doesnt change his start position
« Reply #5 on: April 01, 2020, 01:57:07 am »
Yes, setting the circle's position to the rectangle's position will make the circle be at the same position as the rectangle (unless you move the rectangle; the circle will not automatically follow).

However, the direction of movement of the circle is no way linked to the graphical rotation of the rectangle. You need to calculate the movement direction manually.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*