Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (601)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (649)
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 3247 times)
0 Members and 1 Guest are viewing this topic.
Offline Orangy Tang

JGO Kernel

Medals: 57
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?

[ - 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: 57
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:
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.

[ - 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
Online princec

« JGO Spiffy Duke »

Medals: 555
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:

public void reactToCollision(Collisionable c) {

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

   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.

Jesse (14 views)
2015-07-29 04:35:27

Riven (36 views)
2015-07-27 16:38:00

Riven (18 views)
2015-07-27 15:35:20

Riven (21 views)
2015-07-27 12:26:13

Riven (12 views)
2015-07-27 12:23:39

BurntPizza (30 views)
2015-07-25 00:14:37

BurntPizza (41 views)
2015-07-24 22:06:39

BurntPizza (24 views)
2015-07-24 06:06:53

NoxInc (27 views)
2015-07-22 22:16:53

NoxInc (18 views)
2015-07-22 22:13:39
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 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‑
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!