Author Topic: About collide cause the framerate drop  (Read 2779 times)

Offline gamenewer

  • long
  • ***
  • Posts: 171
    • View Profile
About collide cause the framerate drop
« on: October 15, 2014, 11:54:15 am »
Helloe Egon, I add 10 cars in my game , each car set  setCollisionMode(Object3D.COLLISION_CHECK_SELF|Object3D.COLLISION_CHECK_OTHERS);
and add  addCollisionListener(carCollision);    and called checkForCollisionSpherical  every frame,  I found the framerate droped huge , about 23 fps, normal  is 30fps , Could you help me to  improve the collision perfomace ?   very thanks  :)

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: About collide cause the framerate drop
« Reply #1 on: October 15, 2014, 10:02:25 pm »
Collision detection is always costly. What collides with what in your case? Only cars with each other or all cars with the race track or...?

Offline gamenewer

  • long
  • ***
  • Posts: 171
    • View Profile
Re: About collide cause the framerate drop
« Reply #2 on: October 16, 2014, 03:29:13 am »
Only cars with each other,  If there any idea to  improve collider ?  Such as simple box collider...

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: About collide cause the framerate drop
« Reply #3 on: October 16, 2014, 08:31:53 pm »
That won't help. The collision detection first checks if a collision is actually possible based on simple distance checks, then is checks against the bounding box and only then it checks against actual polygons. So this can only happen, if the cars are pretty close to each other. Every other case will be sorted out very early in the pipeline, so it should be as fast as possible. You might want to give ellipsoid collision detection a try. It more complex than spherical, but it' s also more optimized. It might still be slower though, but it's worth a try.
Keep in mind that 10 cars all checked for collisions will all other cars means 90 collision checks per frame/game tick. That's quite a lot. Maybe you can limit that number somehow? If nothing else helps, maybe you can send me a test case of this app? I would like to profile it and see if things can be improved slightly.

Offline gamenewer

  • long
  • ***
  • Posts: 171
    • View Profile
Re: About collide cause the framerate drop
« Reply #4 on: October 17, 2014, 11:22:17 am »
I'm so sorry ,  the reason is I use the particle by a wrong way,  about 200 particles are visible in the world.  I change they are invisible , the framerate is Ok now (30fps) .   :)

Another question:  about the camera  follow the  player (car) , I use the code take from the car example,  when the car translate fast , the distance between camera and player  is becomes far ,  I want  the camera near the player , change the offset , but no effect , I don't know why?  How to ajust it by car speed ?  Thanks a lot !

final  float  Voffset = 8.6f;
final  float  Hoffset = 50f;

               Camera camera = world.getCamera();

          // Making this interpolation based on ticks fixes the jitter on low fps
           SimpleVector center=player.getTransformedCenter();
          
           SimpleVector oldCamPos=camera.getPosition();
           SimpleVector oldestCamPos=new SimpleVector(oldCamPos);
           oldCamPos.scalarMul(4f);

           SimpleVector camPos=new SimpleVector(center);
           SimpleVector zOffset=player.getZAxis();
           //SimpleVector yOffset=new SimpleVector(0, -18*zoomFactor*zoomFactor, 0);//50 (10)
          
           SimpleVector yOffset=new SimpleVector(0, -Voffset*zoomFactor, 0);
           //zOffset.scalarMul(-80f*zoomFactor*zoomFactor); //70
           /*if(player.speed != 0)
               zOffset.scalarMul(-30f*zoomFactor); //70
           else
              zOffset.scalarMul(-30f*zoomFactor);
           */
           //if(player.speed > 0)
           //   zOffset.scalarMul(-50f*zoomFactor /player.speed);
           //else
           /*
           if(player.speed == 0)
               zOffset.scalarMul(-Hoffset*zoomFactor);
           else
              zOffset.scalarMul(-Hoffset*player.maxSpeed/(player.speed) );
           */
           zOffset.scalarMul(-Hoffset*zoomFactor);
          
           camPos.add(zOffset);
           camPos.add(yOffset);

           camPos.add(oldCamPos);
           camPos.scalarMul(0.2f); //0.2f  1/(4+1)

           SimpleVector delta=camPos.calcSub(oldestCamPos);
           float len=delta.length();

           if (len!=0) {
              camera.moveCamera(delta.normalize(), len);
           }

           camera.lookAt(center);
 

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: About collide cause the framerate drop
« Reply #5 on: October 18, 2014, 09:41:18 pm »
That's because of the interpolation that the car example does for smooth camera movement. We had discussions about this particular code a few times in the forum. It seems to be quirte popular... ;) Maybe this helps: http://www.jpct.net/forum2/index.php/topic,3994.msg28165.html#msg28165

Offline gamenewer

  • long
  • ***
  • Posts: 171
    • View Profile
Re: About collide cause the framerate drop
« Reply #6 on: October 20, 2014, 03:21:18 am »
I think maybe something wrong in my game,   if the car's speed  is 200km/h ,   framerate is 30 /s , in every  frame  , the car should move

200*1000/(60*60*30)   m  ,   is this right?   

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: About collide cause the framerate drop
« Reply #7 on: October 20, 2014, 07:42:36 am »
Depends on the scale of your scene. The engine makes no assumptions about how many meters a 3d unit is.