从类的实例访问另一个实例 [英] Access another instance from an instance of a class

查看:76
本文介绍了从类的实例访问另一个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想想台球。我需要检查它们之间的交互(点击可能)。所以我需要从任何球类实例中获取其他球的位置和速度。



我怎样才能找到一种方法,而不是将所有其他球信息传递给实例。



我认为我可以使用全局分子载体(通过索引访问)载体< moleculeclass> Molecules。为此目的使用单例类是可以的吗?你有什么建议?

Think about billiard balls. I need to check interactions (hits maybe) between them. So I need to access other balls' position and velocities from any instance of ball class.

How can I figure out a way other than passing all other ball information to that instance.

I think I can use a global molecule vector (to access through index) vector<moleculeclass>Molecules. Using a singleton class for such purpose is okay?. What can you suggest?

推荐答案

不,你没有。球不知道任何其他球。它只知道球本身 - 它的矢量信息,它的平滑度,它的质量。



表格知道所有的球 - 它需要计算影响,因为它知道球是打算击中另一个球,还是一个垫子,或者进入一个洞。该表知道球在那一刻移动的场的摩擦力 - 因此它使用球信息来计算它的新位置。球不知道它的位置是什么 - 因为它甚至不知道它是什么桌子!



想一想:如果你做球/球碰撞在球中,你也必须进行球/垫碰撞。你从哪个球做碰撞?其中一个是主球,是所有计算的基础吗? :笑:
No, you don't. The ball does not know about any other ball. It only knows about the ball itself - it's vector information, it's smoothness, it's mass.

The table knows about all the balls - and it needs to calculate the impacts, because it knows if the ball is going to hit another ball, or a cushion, or go into a hole. The table knows the friction of the field the ball is moving over at that moment - so it uses the ball information to calculate it's new location. The ball has no idea what it's location is - because it doesn't even know what table it is on!

Think about it: if you do ball/ball collisions in the ball, you are also going to have to do ball/cushion collisions as well. And which ball do you do the collisions from? Is one of them a "master ball" that is the basis of all calculations? :laugh:


我需要检查他们之间的互动(点击率)



使用你的台球类比我假设如果所有球都躺在桌子上,那么它们之间就没有相互作用。我正在动,正如你所说的那样,检查附近的那些似乎是一个很好的策略,如果有可能是一个很大的数字。

所以也许使用状态机(设计在白板上)来确保你已经识别出这种行为。例如状态:静态,移动,交互和识别以及导致从状态到状态的转换的原因。您似乎已经确定了交互状态可以从移动状态发生,正如您已经确定的那样。虽然对于静止球,但在不同条件下发生明显的相互作用也许使用一个状态机来代表一个代表静态球的状态机旁边的移动球,也许还有一个代表缓冲区的状态机......以确保你能够确定你想要编码的内容。



...除了将所有其他球信息传递给该实例之外的其他方式...



通过识别在开始交互的状态之间转换,以及结束交互的转换,您可以编写一个处理预交互运动的过程。这是日常生活中的事情。一旦它识别出交互标记,它就会将其传递给处理交互的第二个流程。这导致可以在该级别创建Ball-To-Ball-Interaction对象以处理所识别的两个球之间的相互作用。该交互对象只需要处理两个球。也许你需要其他类型的交互对象,例如Ball-To-Cush-Interaction等



所以我说每个球都不需要彼此了解,而且他们不需要通过彼此之间的信息,使用Interaction类来管理它。
I need to check interactions (hits maybe) between them

Using your billiards analogy I assume that if all balls are lying on the table there's no interaction between them. I one's moving, as you say checking the ones in the vicinity seems a good strategy if there's potentially a large number.
So perhaps use a state machine (designed on a white-board) to ensure you've identified the behaviour. e.g. states: Static, Moving, Interacting and identified and what causes the transitions from state to state. You seem to have identified that the interaction state can occur from a moving state, as you've identified. Though for a static ball obviously interaction occurs under different conditions. Perhaps use a state machine representing a moving ball next to a state machine representing a static ball, and perhaps a state machine representing the cush... to ensure you identify what you want to code.

... a way other than passing all other ball information to that instance...

By identifying the transitions between states that start Interaction, and also transitions that end Interaction you could write one process that's taking care of pre-Interaction movement. That is the everyday stuff. Once it identifies an interaction flag it up an pass it to a second process that deals with Interactions. Which leads to the possibility of creating a Ball-To-Ball-Interaction object at that level to deal with the interaction between the two balls identified. That interaction object only needs to deal with two balls. Perhaps you need other types if Interaction objects e.g. Ball-To-Cush-Interaction etc.

So I'd say each ball doesn't have to know about each other, and they don't need to pass information to each other, use an Interaction class to manage it.


这篇关于从类的实例访问另一个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆