473,785 Members | 3,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[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 1689
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
3752
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? Here is an example of just such a usage: <dl class="faq"> <di>
5
1259
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. (I'll use lazy init to fill the collections when needed.) More specialized employee types would inherit from the base class. Sometimes, though, all I need is the employee name and ID number - for example, when filling a DDL - and I hate to...
19
1710
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. Besides that one can pass reference to allocatior into every STL container. This raises the following questions: 1. Containers that were templatized with different allocators are considered different types and thus cannot be assigned to each other....
9
2761
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
1325
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 have the test page open in multiple browsers, they are all getting the same data. I don't want this to happen. I have tried different uses of protected, internal, static and such with no luck. Can anyone help me here?
29
3577
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 one data field - but i'm not sure) :-) Background info:
65
4239
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 difference is between these and: (3) mylist =
4
1795
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 it with several smaller containers ? Any ideas and links to the proper documentation is greatly appreciated.
21
3676
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 std::map<string,doubleMap; but instead of double, I have integers, even booleans so I guess that the use of double waste a lot of storage. Is there any way to do this?
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10148
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.