Need to collide two md2 (animated character)

Started by gkapoor, October 28, 2010, 05:43:44 PM

Previous topic - Next topic

gkapoor

Hi All,

I am loading two md2 (in serialize form) file using Loader.loadSerializedObject(A) and Loader.loadSerializedObject(B) in side onSurfaceCreated. A and B both are animated (like Ninja they Jump, Kick) and using animate to animate these two charcter. I want to collide both there Animated Character and want to show some change in color kind of blood when they both collide. Please let me know if anyone know the solution and please tell me the steps how can I achieve this collision between these two animated characters. I will give him some reward if he/she can help me to get out of this problem or find some solution.
Inside public void onSurfaceCreated(GL10 gl, EGLConfig config) I am trying with following method but it's  not working . I am not able to find any Collision and CollisionListener is not working.

A.animate (counter, animationSequenceCounter)
B.animate (counter, animationSequenceCounter)

A.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
A.setCollisionOptimization(Object3D.COLLISION_DETECTION_OPTIMIZED);
B.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
B.setCollisionOptimization(Object3D.COLLISION_DETECTION_OPTIMIZED);
A.addCollisionListener(new CollisionListener()
{
@Override
public boolean requiresPolygonIDs()
{
                                 return true;
}
@Override
public void collision(CollisionEvent e)
{
  if (e.getType()==CollisionEvent.TYPE_TARGET && e.getSource()!=null)
    {
                                    destroy();
                     
   
}
}
});
B.addCollisionListener(new CollisionListener()
{
@Override
public boolean requiresPolygonIDs()
{
return true;
}
               
@Override
public void collision(CollisionEvent e)
{
                  
if (e.getType()==CollisionEvent.TYPE_TARGET && e.getSource()!=null)
 {
destroy();
   }
                  
}
});

Thanks & Regards,
GKapoor

EgonOlsen

That code doesn't show how you are doing the actual collision detection!? If you have something like detecting the collision of a kicking foot of one character with another character's head in mind, then the default collision detection methods alone won't cut it....

gkapoor

Ok then what is solution of this? Please let me know the steps to solve this problem...Im just having 1. A and B two animated files only 2. Loading these two by loadSerializedObject...now what next steps i need to follow to collide these two file with eachother? what method, listeners and under which class or functions in need to delacre all those....Please Help me !!!!! just make a simple example of this in android or just guide me in right direction....Please please please!!!!!!

EgonOlsen

A simple example of collision detection can be found here: http://www.jpct.net/wiki/index.php/Collision_detection
It's for jPCT, not jPCT-AE, but the basic logic stays the same. You are a little vague about what you want to achieve...should that be a kind of a boxing game or is the only goal to let two animated chars bump into each other and register that?

gkapoor

No..it will be like two ninja character but in md2 form not like bones files...and wants to fight them with each other and show some change in color or kind of blood on hit.

EgonOlsen

So what you actually want is punches and kicks being recognized, not simple object collisions...right?

gkapoor

yes...My Friend...I want to recognize punches and kicks, not the simple object collision..

EgonOlsen

I see...well, i think there have to be best practices to do this. I just don't know them. Fact is, that the default collision detection methods of jPCT-AE alone won't be sufficient. I have two ideas how to deal with this and i'm not sure, if they are applicable...however, my ideas usually aren't thaaaat bad, so maybe they'll work... ;)

Idea one: Don't use real collision detection at all, but a kind of heuristic based on player's positions and actions. Like: If player 1 faces towards player 2 and player 2 doesn't block and player 1 does a kick and the distance of both is < x, then the kick of player 2 will hit player 1. This can be checked regardless of any animation playing and it's very fast to do. It can be tricky to tweak it to look reasonable though.

Idea two: Don't do collision detection on the models but on some abstract and simplified model. Like an ellipsoid for the players and some spheres that marks the end of each arm and leg. If a player kicks or punches, move the spheres so that their positions reflect the animation playing (more or less) and check collisions between the spheres and the player's ellipsoid...something like that. That's harder to do than the first approach IMHO. Especially you have acutally two "animations" to play with one being the animation of the actual view and the other one the "animation" inside the collision model. But it *might* produce better results.

In both cases, the md2 models and animations only represent the view, not the data on which the actual collision detection is based on. You won't find a magic method inside jPCT-AE that handles this. It's a special case related to fighting games and you have to solve this on your own. However, maybe one of these ideas is helpful.

gkapoor

Thanks My friend....I think Idea two is perfect...can you please tell me some coding syntex for the Idea two, means how can i do this as i am new in this field and dnt know much more about this...it will be highly appreciated if you can do this..or just give some sample code of this....please don't reffer me to some other link... ;)..thanks!!!!!

EgonOlsen

Well...no, i can't do this. That would mean to write a large part of your game. I gave you some ideas (that mights or might not work well) as well as a hint where to find a starting point about collision detection in jPCT. I'm not here to solve your coding problem especially not if they require a rather large effort that's totally unrelated to jPCT itself. I don't have the time to do this nor do i think that this is the kind of support that can be expected from me.


Mohit

#11
Hi

I was doing the same thing.
I liked the IDEA ONE but i have a few doubts
The code I use to calculate distance is

           if(pcBot.calcMinDistance(pcBot.getTransformedCenter(), myBot.getTransformedCenter(), 8)!=Object3D.COLLISION_NONE){
           Log.e("Collision detected","i have hit the pc bot");
        soundPool.play(punchId, 1, 1, 0, 0, 1);
        soundPool.play(moanId, 1, 1, 0, 0, 1);
           }

(Not correct  ;) as the distance between head and punch is to be calculated not between their centers. And the punch is moving).

I would like to check if there is any object within the 5*5*5 ellipsoid/sphere etc.
Is there any way to do that.????

Also if I continue use
public float calcMinDistance(SimpleVector org,
                             SimpleVector dr,
                             float ignoreIfLarger)


What parameters should be used for "org" and "dr" ? org should be pcBot's head and dr should be myBot's fist(How to find them??) pcBot's head can be approximated but how the fist? As its moving .

Thanks in advance.


EgonOlsen

Idea 1 was actually to be something much simpler than what you describe with the taking the distance between punch and head. The idea was to say that if the distance  of the players (based on their centers for example) is below x (and maybe larger y) and player one is in state 1,2 or 4 (and not 3, which might be ducked or something) and player 2 punches at him, then he will be hit....something like that. I didn't had in mind to base this calculation on different body parts.

Checking if a point is within a given sphere is checking if the distance between the sphere's center and that point is <= the radius of the sphere...really simply.

Regarding org and dr, org is the origin vector. In your case, it would be the center of the fist or similar. dr is the direction in which the movement happens. In your case, the direction of the blow.

Mohit

Thanks Egon. You gave the simplest solution.
Different body parts aren't needed.
Different states for punching, ducking etc. will do.