E
E
EVGENY T.2019-01-17 10:34:33
OOP
EVGENY T., 2019-01-17 10:34:33

In which class to write the logic of collisions of two objects?

I'm designing a simple arcade game. Snowflakes fly, the player catches them with a mitten. Accordingly, there are three classes:
1. Snowflake
2. Mitten
3. Scene
We need to catch the moment when the player catches a snowflake with a mitten - a simple collision detection. In what class would it be more correct to do this from the point of view of OOP?
Options:
1. In the scene. The scene knows what kind of snowflakes it has, where the mitten is.
2. In a mitten. The mitten is the main character here.
3. In a snowflake. The snowflake should disappear.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
H
hack504, 2019-01-17
@Beshere

Nowhere. In the OOP paradigm, both the snowflake and the mitten and the scene describe only their behavior by methods and properties. Enter another abstraction - the world (or physics), which contains all these objects and describes the behavior of their interaction.
The scene detects a collision between a snowflake and a mitten - it passes it on to the world, and the world, in turn, removes the snowflake, starts the animation of the mitten, and triggers a joyful sound click in the scene.
Thus, if in the future to implement the add-on "Rooks have arrived", then the logic of the collision of the mittens and the droppings is easily implemented => the litter remains, the mittens freezes, in the scene there is a sad sound "ooh"

L
Lander, 2019-01-17
@usdglander

GreatRash , Vladimir Proskurin , Ryabos ,
Three answers and all different! :)))

G
GreatRash, 2019-01-17
@GreatRash

I would write in a snowflake.
UPD: but definitely not in the scene.

R
Ryabos, 2019-01-17
@Ryabos

The snowflake, as I understand it, just falls. If you place the logic here, there will be an extra dependency between entities.
A mitten is needed in order to catch snowflakes. Suitable candidate.
The scene is responsible for placing the mittens and snowflakes. If it also handles events, it may turn out to be a GOD object.

V
Vladimir Dementiev, 2019-01-17
@SayMAN83

As already mentioned above, there should be a class for processing animation. Snowflakes do not need to be removed, they can simply be made invisible and then used again. In the scene, you only need to draw all the objects on the screen and pass the animation results to the objects. To be more precise, Pass the view matrix and draw each object in this way. On the stage.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question