Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Collision reaction  (Read 3038 times)
0 Members and 1 Guest are viewing this topic.
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Posted 2003-02-26 16:36:57 »

I'm stuck trying to create a framework for my game objects' collision reaction in a flexible way..

Assuming collision detection is already found, I'm now left with two objects which need to affect each other. Currently i've got 4 main catergories of object: Player, AI, Projectile & Powerup. All have a common base class for shared functionality.

Now what? Ideally, i'd like to be able to write a new class, such as a Grenade, which would be of type Projectile, and not have to alter any other classes. Yet all existing classes should interact with the new class properly. In short, adding a new weapon/enemy/powerup should be a single drop-in class.

So far I've realised I probably need some sort of priority system for determining which of the two objects takes responsibility of the collision, but beyond that i'm out of ideas. Anyone got any good advice on the subject?

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Herkules

Senior Devvie




Friendly fire isn't friendly!


« Reply #1 - Posted 2003-02-26 16:49:25 »

Have a matrix where rows and columns are concrete classes. New write handler objects for each possible collision pair (some might be generic) and register them at the appropriate cell in the matrix.

On a collision event, look up the collision handler from the classes in play and execute it.

If you populate the matrix symmetrically (handler_ij == handler_ji), order might not be important.

If you have a new class, extend the matrix by one row and one column and register handlers describing how the other behave on collision.

class GrenadeAnyCollisionHandler .....


HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #2 - Posted 2003-02-26 17:24:22 »

While a nice idea, that unfortunatly leads to exactly the situation i'm trying to avoid: specific handler code for each and every combination. Ideally two people could write additional objects and not have to worry about the interaction between the two other than in general terms.

I'm currently leaning towards soemthing like:
1  
2  
3  
4  
5  
6  
if (dynamicObject.reactsTo(hitObject) && hitObject.reactsTo(dynamicObject)
{
    // Find relative priorities

    // Forward to object with highest priority for handling
}


And providing a default behaviour of reactsTo() for each catergory of object (AI reacts to player, projectiles, but not powerups, etc.) but with them being overridable to allow specific behaviour to be added.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline princec

« JGO Spiffy Duke »


Medals: 439
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #3 - Posted 2003-02-26 18:02:37 »

Have a quick look through the XAP dev diary for a concise and complete explanation about collision detection. I'm doing exactly what you want to do.

Cas Smiley

Offline mill

Junior Devvie




popcorn freak


« Reply #4 - Posted 2003-03-23 08:27:06 »

i'd use either an interface Collisionable or an abstact class describing all the things they have in common.

then would have a method called reactToCollision (either in the interface or the abstract class. if it's in the class, the method should be declared abstract).

using this approach you would then have something like this:

1  
2  
3  
public void reactToCollision(Collisionable c) {
    c.doIt(this);
}


perhaps it's not the most clear description you have read, but it's pretty basic. i'm also pretty sure this is a well known design pattern, but i can't remember the name and i don't have the GOF-book here either (visiting my parents over the weekend. no the book is not visiting my parents, I am)

Offline Herkules

Senior Devvie




Friendly fire isn't friendly!


« Reply #5 - Posted 2003-03-23 08:58:41 »

This is one of the double-dispatch schemes. Could be implemented by the 'visitor' pattern.

But doesn't take you very far. First, it is not symmetric and second you end up very soon with

1  
2  
3  
4  
5  
   if ( c instanceof Bullet )
   ....
   else if ( c instanceof SpaceStation )
   .....
   


This suffers from the option to implement new Collisionables without touching the existant.

Also, kind of collision, location, time, rel. speeds,... are missing for response evaulation. This would lead to an VERY bloated Collisionable interface that anticipates everything you could invent in the future.


HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline cfmdobbie

Senior Devvie


Medals: 1


Who, me?


« Reply #6 - Posted 2003-03-23 09:46:07 »

As Cas said, check out the XAP diary.  He's using a double-handshake collision processing system that doesn't require any instanceofs.

Adding a new collidable-with item to his system requires adding another default do-nothing method in the superclass, and then implementing that method on objects that want to do something other than the default.

Hellomynameis Charlie Dobbie.
Offline javatypo

Junior Devvie





« Reply #7 - Posted 2003-04-13 21:03:17 »

make different methods that move the object in different ways, and then call them as necesary
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #8 - Posted 2003-04-13 21:34:22 »

Try checking out the collision detection routines in my GAGE library. A lot of people have commented that it's a very good solution to the traditionally complex issue of collision detection.

Java Game Console Project
Last Journal Entry: 12/17/04
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Elsealabs (15 views)
2014-12-28 10:39:27

CopyableCougar4 (19 views)
2014-12-28 02:10:29

BurntPizza (24 views)
2014-12-27 22:38:51

Mr.CodeIt (14 views)
2014-12-27 04:03:04

TheDudeFromCI (19 views)
2014-12-27 02:14:49

Mr.CodeIt (26 views)
2014-12-23 03:34:11

rwatson462 (58 views)
2014-12-15 09:26:44

Mr.CodeIt (47 views)
2014-12-14 19:50:38

BurntPizza (96 views)
2014-12-09 22:41:13

BurntPizza (115 views)
2014-12-08 04:46:31
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!