2

I am writing a pong clone with a component based entity system. But I am having trouble with the different kinds of movement in the game.

The problem is the following: My paddles will just move up and down, so I only need a Y-velocity for these. But the ball can move in each direction. So the ball will need an angle and a velocity. Now my question is: Is it usual to put the data for moving a ball in another component than the data for moving a paddle? Because if I would do so, I would need two systems just for movement. Is that a good approach?

A little bit information about my system: My entites are only IDs. I have an entity manager which holds all the entities and the linked components. Components only hold certain data on which systems operate. Basically I am using this approach: Tutorial

M0rgenstern
  • 433
  • 6
  • 16

1 Answers1

4

They should use the same movement component for both, if the movement component is velocity. However, the systems that modify the velocity will be different. The ball will use the physics component to bounce off the paddles and walls, where the paddles will use the mouse/keyboard input component to move. So the physics system will be modifying the movement component of the ball, and the input system will be modifying the movement component of the paddles. The paddles won't use the x-axis for movement, but that's OK.

The will both use the movement system. The movement system will take their movement components and update their position components.

House
  • 73,224
  • 17
  • 184
  • 273
  • The physics, so the collisions is not the problem. And the problem is as well not, that the paddles would not use the x component. The problem is, that if I give them both the same component for movement, than the movement system will handle them both equal. But: The ball needs movement by an angle, the paddles not. And as my components are just data container and my systems are operating independently from WHAT the entity is, but only on the components, that would not be possible. Or am I thinking too complicated? – M0rgenstern May 10 '13 at 15:15
  • You're thinking too complicated. I'll edit my answer to be a little more clear. – House May 10 '13 at 15:27
  • @Byte56 set you straight, the design trick will be to use systems that key on more than just one component to set up like described. – Patrick Hughes May 10 '13 at 15:33
  • Yep, similar to this answer, where the position component is used in multiple systems. – House May 10 '13 at 16:20
  • I am using the position component in multiple systems. I also use multiple components in a system. My actual problem was if it I should seperate the data or not. Edit: Sorry, but I actually still have a problem. If the ball and the paddles both have a movement component and a position component, and the movement components do not differ in the kind of data they hold. How can the movement system differ between a ball and a paddle? Because both would just be an entity with a position component and a movement component. – M0rgenstern May 10 '13 at 17:13
  • They'll both have the option to move on both axis. However, the input system is only going to apply movement on the Y-axis. So the X-axis will still be there, it'll just be unused. – House May 10 '13 at 17:23
  • 1
    The movement system is just taking the velocity and applying it to the position. How the velocity is set will depend on what the physics and input system set them to. – House May 10 '13 at 17:34
  • 1
    "system is just taking the velocity and applying it to the position. How the velocity is set will depend on what the physics and input system" Is a very important concept to learn (and very not-OOP) and will keep you from creating many super-specialized and complex systems when simple systems working in concert will both get the job done and increase flexibility instead of locking you in even deeper. – Patrick Hughes May 10 '13 at 19:11
  • I think I understand now. What I thought until now: The Ball needs to be moved by something like: xPos = xPos + cos(angle)velocity and yPos = yPos + sin(angle)velocity and the paddles have to be moved by: xPos = xPos + x_Velocity and yPos = yPos + y_Velocity But what you say is: The ball should have a physics component which holds the angle and then calculates the velocity for each axis and stores the result in the movement component. So it would be in physics system: x_Velocity = cos(angle)velocity and y_Velocity = cos(angle)velocity. Where angle and velocity belongs to the physics... – M0rgenstern May 11 '13 at 07:33
  • ... component. Do I understand that correctly now? – M0rgenstern May 11 '13 at 07:37
  • Getting closer. The velocity info will stay the same in the motion component, until the ball collides with something. Then the physics system will calculate a new velocity depending on the collision. The physics component wouldn't have a velocity or angle, it contains info about the physics shape of the entity. Kind of like the render component holds info about how to draw the entity. The velocity and position are held in the position and motion components. So the physics system will mostly not be doing anything, only when a collision happens does the physics system modify the velocity data. – House May 11 '13 at 16:14
  • Okay. But where is the angle stored? If it is stored in the movement component, then this would be confusing as the paddles would also have an angle. And: Even if I calculate the speed of the ball for each axis individual, where should I save the initial velocity, which is needed for calculating the velocity for each axis. Because for calculating like I wrote above, you would only need one velocity. I am still confused what belongs into which component... Sorry. – M0rgenstern May 11 '13 at 17:05
  • I'm not sure what angle you're talking about. If you store the velocity for each axis, you calculate the angle of collision whenever a collision happens. You give the ball an initial velocity for each axis when you create it. I think you should join the chat and ask any further questions. – House May 11 '13 at 18:08
  • Sorry, I thought it was clear which angle is meant. I mean the angle which tells the ball in which direction it moves. I am using an angle to move the ball. So the position of the ball is: x_Pos = x_Pos + cos(angle)velocity and y_Pos = y_Pos + sin(angle)velocity But you are right. If I just give the ball a x-Velocity and a y-Velocity, then I could calculate the collision angle and so also calculate the velocity for each axis after the collision. Is that correct now? – M0rgenstern May 11 '13 at 19:49
  • The angle is only used in the moment the ball collides. And it's calculated at that time. There's no need to store it anywhere, just use it to calculate the new velocities. Yes, it's correct now. – House May 11 '13 at 22:07