473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

[OO question] Single container, multiple types

I've run into a problem with a game I'm designing -- I (think) I need
to do something ugly, for speed reasons, and I'm wondering if people
here might have any ideas about how solve my problem more elegantly:

I'm using a templated container that I created for this project that
sorts objects based on a numerical value. That is, each object in the
container has an integer value associated with it, and objects with
lower integer values come before those with higher ones. If an object's
int value changes, it is moved in the container. This allows me to do
quick collision detection in the game's 2D world -- objects that need
to generate collisions insert themselves into the container twice, once
for their leftmost coordinate and once for their rightmost coordinate
(actually, there are two containers, one for each plane, and objects
insert themselves twice into both containers). When it comes time to
check collisions, only those objects that are between an object's left
and right positions in the container can have collided with it.

Originally, there was just one type of object that would generate
collisions. But now there are two types of objects that collide with
each other -- sprites (characters that are visible onscreen), and
sectors (regions that are not visible but which cause events to occur
when a sprite moves into them). It would be great if I could stick
these objects into the same containers, because they both need to do
collision detection. The problem is, there is no common interface
between them -- no mixin class that they can both inherit from or
anything like that. Their collision detection needs to occur at two
discrete times -- first all sprites check for collisions with all other
sprites, and then all sectors check for collisions with sprites.
Sprites never check for collisions with sectors and sectors never check
for collisions with other sectors. I need to know, when handling
collisions, whether an object in the container is a sprite or a sector.

I can't think of any way to do this except to create a stupid
SpriteSector interface that declares two methods, isSprite() and
isSector(), have sprites and sectors inherit from this class, and have
the container store SpriteSector pointers (or I could just C++'s RTTI
functionality but that would essentially be the same thing). This just
seems so ugly and poor and I like elegance and was wondering if anybody
could point me to a nice design pattern or something.

Thanks,
Tim

Jul 22 '05 #1
1 1670
Tim Conkling wrote:
I've run into a problem [...]

I can't think of any way to do this except to create a stupid
SpriteSector interface that declares two methods, isSprite() and
isSector(), have sprites and sectors inherit from this class, and have
the container store SpriteSector pointers (or I could just C++'s RTTI
functionality but that would essentially be the same thing). This just
seems so ugly and poor and I like elegance and was wondering if anybody
could point me to a nice design pattern or something.


Have you ever visited comp.object newsgroup? Seems right up your alley.
My point here is that you might have a design problem on your hands, not
just a language problem.

What I'd do is create some kind of "Collidable" interface and make all
my classes that represent objects with boundaries to inherit from that.
As to 'isSprite()' and 'isSector()', it doesn't seem necessary. You just
put pointers to those in separate containers for the purpose of doing
other, class-specific, operations.

Victor
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Dave H | last post by:
Hello, I have a query regarding definition lists. Is it good practice semantically to use the dt and dd elements to mark up questions and answers in a frequently asked questions list, or FAQ? ...
5
by: Roger Bonine | last post by:
I'm working on a rewrite of our employee database. I plan to implement a fairly heavyweight base class, which includes 20 or 30 fields, including address and phone number collections and the like....
19
by: regisser | last post by:
I have a quastion for the C++ professionals and members of the C++ standartization commetee. As i know C++ standart requires an allocator to be a templated parameter in every STL container....
9
by: Tim Rentsch | last post by:
I have a question about what ANSI C allows/requires in a particular context related to 'volatile'. Consider the following: volatile int x; int x_remainder_arg( int y ){ return x % y; }
4
by: Grigs | last post by:
Hello, I have a fairly simple class that I created and it works great. Basically it remembers a certain amount of strings. When a new one comes in the oldest drops off. The problem is, when I...
29
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this...
65
by: Steven Watanabe | last post by:
I know that the standard idioms for clearing a list are: (1) mylist = (2) del mylist I guess I'm not in the "slicing frame of mind", as someone put it, but can someone explain what the...
4
by: Michel Esber | last post by:
Hello, LUW V8 FP 11 running Linux RH AS4 Update 3. In regards to performance and IO parallelism, does it matter if I create a tablespace with a single big container, or is it better to create...
21
by: aaragon | last post by:
Hello everyone, I would like to know if there is a way to use the std::map to store different types for one of its two types. That is, I'm trying to use it as: typedef...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.