Click here to Skip to main content
15,847,077 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
Share this answer
maxc900 7-Jul-13 4:25am    
Ok you're right. But if we have multiple regions in table? We have all eight balls in the table but some of them are in right half and the others in the left half. Hence a half part should know all the balls and the others too (because some of them would pass there).
OriginalGriff 7-Jul-13 4:38am    
Why divide it? Think about it from a real-world snooker table: there is no difference between the left and right halves (although there are special areas such as the "D" and the ball locations) so why create an artificial distinction between them? If you do, what happens when a ball passes from the left to the right half (as it will often, in the real game)? What if there is a ball which has it's centre in the left half, but just enough ball in the right half to cause a "kiss"?
maxc900 7-Jul-13 4:59am    
Let me explain why. It easy to check 8 balls. But if you have a million balls? It's unlikely to collide two balls which are distant from each other. So I divide the table into regions and i check only the balls in neighbor regions.
OriginalGriff 7-Jul-13 5:06am    
Only if you check the velocity at which they are moving.
All it takes is one very very fast ball in the wrong side...

I get the feeling you aren't interested in snooker here - what are you actually trying to do?
maxc900 7-Jul-13 5:14am    
this is a molecular dynamics technique. you assume that the molecules behave like balls.

Anyway :) What do you suggest for global molecules and cell lists?
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.
Share this answer
maxc900 7-Jul-13 7:00am    
i'll look for that state machine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900