Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (604)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (654)
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 related questions ( not how to do it )  (Read 2836 times)
0 Members and 1 Guest are viewing this topic.
Offline Sanzeinga

Senior Newbie

« Posted 2009-03-22 08:25:01 »

So the player has various objects colliding on him, and can collide on various things. 

Question 1: whats the best way to deal with this so that velocity change effects and knockback occurs correctly.  For example, the asteroid is pushing you against the boundary of the wall.  While checking the collision of the asteroid, i realize that if the player is trying to move away from the asteroid ( faster than the asteroid moves towards it ) then the asteroid will clip onto the player because the player ACTUALLY can't move away because a wall is blocking.  So i figure i'll check the collisions with things like walls first.  Your velocity drops to 0 because youre colliding a wall, but then an asteroid hits you and pushes you away, into that wall clipping you again.  ( this occurs since i'm checking collisions based on where you will be rather than where you are, since if youre already colliding, then its too late ( the asteroid that should be pushing you is already on top of you )

dx=0 dx=-10 dx=-5, the asteroid won't hit you since you'll be 10 pixels left, except you won't be 10 pixels left thanks to the wall.
Offline Gudradain
« Reply #1 - Posted 2009-03-22 15:10:30 »

Hmm seems to be a complicated problem.

I have done a (very) small physic engine and I found out that in most of the case the easiest way is enough for your game. Anyway, let's see if I can help you.

What I did is basically for each moving object, move the objects one by one and after you move an object check to see if this object is in collision with another object. If yes, move the moving object outside of the object it hit.

This method work just fine as long as object have similar weight.

What you seems to be trying to do is (and let forget about the part that you check futur collision before moving). For each moving object, move this object than check if it is in collision with another object. If yes, move the object that was hit outside of the moving object. Than check if the hit object is in collision with another object. If yes, move the second hit object outside of the first hit object and so on. Now, if a hit object is a wall, than the wall won't move and the hitting object will be push back outside of the wall. If the object that hit the wall is in collision with another object, move this other object outside of the object that hit the wall and so on until there is no collision at all.

While this method might be a lot better if you care about how real your physics is it is a lot harder to implements and take a lot more time to execute because you can have a chain of 100 collision with only 1 moving object. And maybe another chain of 100 collision with the second moving object and so on. All that in 1 turn.

I found out, that if I have more than 100 moving objects the physics is what take the more time in my game and my physic is really simple. So I wouldn't make the physics more realistic if I don't really need it.

Anyway, good luck.
Offline Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

« Reply #2 - Posted 2009-03-22 18:57:35 »

Typically what I do is every timestep I update a collisions list to reflect all collisions that happened since last timestep. The list contains objects that are pairs of other objects (i.e. the two objects that we found collided), and the list is ordered by object, so you can get the index of a certain object's collisions very easily. As such, you can quickly get a sublist of all collisions concerning a certain object.

Using that list, you could solve problem #1. You'll know that one object is in between two others, and therefore should be reflected in a certain way.

See my work:
OTC Software
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Gudradain
« Reply #3 - Posted 2009-03-22 21:17:50 »

I also have a Collision list but I don't understand how you use it to detect if a Object that is hit is right next to a wall and then won't move.

As long, as you don't move the object in the wall it won't detect that it is in collision with the wall. (Except if you check if next position before moving but it's like moving a dummy object).

What do you do to build your collision list?
Offline Sanzeinga

Senior Newbie

« Reply #4 - Posted 2009-03-24 19:18:07 »

I don't really understand what you mean for my 1st question.. but here's my 2nd question:

This is an example situation, for the general problem im having:
i have a missile that needs to fly to the enemy player and hit them, on the way it collides with various things that it should ignore ( walls, rocks, etc assume the missile is flying above these and only comes down to hit if the collision is the enemy player ).  The collision detection currently calls a general projectile.actionOnCollision() type method that this missile inherited.  My problem is how i should distinguish between collision with other random objects and the collision with the player ( how can i tell what kind of object the missile is currently colliding with, since as far as the missile can tell its just colliding with an Entity ).  Is there a solution besides running a separate series of collision tests just for objects that can only collide with players, since that seems like a workaround solution.  And actually, if for example there were missile detection objects, which upon collision change colors ( to signify the missile flew right over it ) the workaround solution doesn't seem like it would work anyway.
Offline Gudradain
« Reply #5 - Posted 2009-03-24 21:24:29 »

Do you actually have a game in which you encounter this type of problem or you are just asking for personal culture?

Anyway, the solution to the question 2 is to have multiple level for your object. Let's say all object on the ground are on level 1 and your missille is on level 2. When you check for collision you check if the 2 objects are on the same level. So like that, your missile will pas through all object on level 1.

Now, let's say you want your missile to hit a specific target that is on the ground. You just have to change the level of this object to level 2. Multiple level is better, example : making your ground object level 1 and level 2 will make it hittable by the missile and still behave correctly against other ground object.

You can just play a bit with this idea to make it work.
Offline Sanzeinga

Senior Newbie

« Reply #6 - Posted 2009-03-24 23:08:58 »

My game so far :  ( cant really see whats going on without turning on full screen )
machine gun fire blocked by the rocks, the vertical missile ignores the other collisions in the way and then activates (fallingState) when it collides with the other player.  There might be other ways to handle unnecessary collisions in this specific case, but i'm just using this as an example.  Right now im using a quick fix of adding "collidesRocks" boolean and making a seperate set of collision checks of collision on projectile vs players and projectile vs non player entities.  I dont actually want to "ignore" the collisions so much as i want to have some way to identify the type of collision  ("this entity is a rock, i can ignore it, this entity is a tower, activate it, this entitiy is a player, detonate on it")  so that i can check my list of projectiles(all objects extending a superclass called projectile) against a list of entities ( including other projectiles since projectile extends entity ).
Am i wrong to think that using a system of tagging multiple levels ( or basically multiple category groups ) won't really let me do this?  Or is there a way to do this like that
Offline Gudradain
« Reply #7 - Posted 2009-03-24 23:31:25 »

No I think it should work.
Offline Gudradain
« Reply #8 - Posted 2009-03-26 16:48:00 »

Since my last post I work on the collision level in my game engine.

You can check the result here : shootingDemo1
Control with the key wasd and shoot with mouse

As you can see, a sphere doesn't enter in collision with the blue sphere and a green sphere doesn't either enter in collision with another green sphere. But if a green sphere enter in collision with something else it disappear.

What I did is : For each object I have a position level and a collision level. The position level describe all the level that my object is in. The collision level describe all the level that the object can enter in collision with it.

Example for the demo above

--------------- Position Level ------------ Collision Level
Blue                      1                                   0
Shoot                    1                                   0
Red                       0                                   0
Black                     0                                   0
Wall                      all                                 all

Blue check collision with a green sphere (shoot). Blue is level 1 and green can collide with level 0 AND shot is level 1 and green can collide with level 0. So no collision

Red check collision with a green sphere (shoot). Red is level 0 and green can collide with level 0 so we have a collision.
Offline Sanzeinga

Senior Newbie

« Reply #9 - Posted 2009-03-26 19:09:09 »

Nothing appears ( im on mac ) after it starts.  ( it doesn't claim its failing to start though )

3/26/09 3:07:54 PM [0x0-0xcf0cf] Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: javax/swing/GroupLayout$Group
3/26/09 3:07:54 PM [0x0-0xcf0cf]  at game5.GameFrame.<init>(
3/26/09 3:07:54 PM [0x0-0xcf0cf]  at game5.GameCreator.<init>(
3/26/09 3:07:54 PM [0x0-0xcf0cf]  at game5.GameCreator$
3/26/09 3:07:54 PM [0x0-0xcf0cf]  at java.awt.event.InvocationEvent.dispatch(
3/26/09 3:07:54 PM [0x0-0xcf0cf]  at java.awt.EventQueue.dispatchEvent(
3/26/09 3:07:54 PM [0x0-0xcf0cf]  at java.awt.EventDispatchThread.pumpOneEventForHierarchy(
3/26/09 3:07:54 PM [0x0-0xcf0cf]  at java.awt.EventDispatchThread.pumpEventsForHierarchy(
3/26/09 3:07:54 PM [0x0-0xcf0cf]  at java.awt.EventDispatchThread.pumpEvents(
3/26/09 3:07:54 PM [0x0-0xcf0cf]  at java.awt.EventDispatchThread.pumpEvents(
3/26/09 3:07:54 PM [0x0-0xcf0cf]  at
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Gudradain
« Reply #10 - Posted 2009-03-26 20:03:51 »

I'm never try my demos on a Mac machine before  Sad. Don't even have one to test it on...

Maybe you can try this demo to see if it's just the shooting demo that doesn't work

I use java 1.6 update 7.
Offline SimonH
« Reply #11 - Posted 2009-03-26 20:12:52 »

I use java 1.6 update 7.
No java 1.6 on a Mac Sad You'll have to recompile for java 1.5.

People make games and games make people
Offline Gudradain
« Reply #12 - Posted 2009-03-26 20:30:24 »

Seriously???!! Shocked

So, do I need to install java 1.5 and make specific version for Mac?
Offline h3ckboy

JGO Coder

Medals: 5

« Reply #13 - Posted 2009-03-26 20:49:13 »

the shoot demo si very slow.
Offline Gudradain
« Reply #14 - Posted 2009-03-27 02:09:03 »

the shoot demo si very slow.

H3ckboy, I just test it on 4 differents computer (all with Windows XP...) and it doesn't lag. What are you using XP, vista, mac, linux?
Offline Sanzeinga

Senior Newbie

« Reply #15 - Posted 2009-03-27 02:33:12 »

pong demo doesnt work either
Offline h3ckboy

JGO Coder

Medals: 5

« Reply #16 - Posted 2009-03-27 16:13:28 »


It is the mouse. It kinda slows down. It may be my computer. my laptop kinda sux.
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

SHC (28 views)
2015-08-01 03:58:20

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

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

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

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

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

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

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

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

NoxInc (37 views)
2015-07-22 22:16:53
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!