www.jpct.net

jPCT-AE - a 3d engine for Android => Support => Topic started by: kiffa on April 18, 2013, 10:55:25 am

Title: How to improve the performance of collision check in a racing game?
Post by: kiffa on April 18, 2013, 10:55:25 am
In my racing game, i used Object.checkForCollisionEllipsoid() to check collision.

The collision will happen between these objs: player's car(1 object3d)、npc-cars(1 - 6, 6 object3ds)、road-bar(one large object3d):
(http://p13.freep.cn/p.aspx?u=v20_p13_photo_1304181652565102_0.jpg)


My codes:

Code: [Select]
bar.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);

playerCar.setCollisionMode(Object3D.COLLISION_CHECK_SELF);
npcCar1.setCollisionMode(Object3D.COLLISION_CHECK_SELF);
npcCar2.setCollisionMode(Object3D.COLLISION_CHECK_SELF);
...
npcCar6.setCollisionMode(Object3D.COLLISION_CHECK_SELF);


OcTree oc = new OcTree(bar, 10, 3, OcTree.MODE_OPTIMIZED);
oc.setRenderingUse(false);
oc.setCollisionUse(true);
bar.setOcTree(oc);

//onDrawFrame
playerCar.checkForCollisionEllipsoid(step, mEllipsoid, 3);
npcCar1.checkForCollisionEllipsoid(step, mEllipsoid, 3);
...
npcCar6.checkForCollisionEllipsoid(step, mEllipsoid, 3);

My question:
1, The bar has 8800 triangles, how to create the octree will give the best performance?

2, Any other suggestions to improve the collision performance? The bar is just plane.

Title: Re: How to improve the performance of collision check in a racing game?
Post by: dutch_delight on April 18, 2013, 08:13:32 pm
perhaps you can only check for car to car collision when they are near you so something like this:

Code: [Select]
if (playerCar.getTransformedCenter().distance(npcCar1.getTransformedCenter()) < 100) {
npcCar1.checkForCollisionEllipsoid(step, mEllipsoid, 3);
}

dont know if that will be quicker but that's what i do. i only do important calculations on the ai cars when the player can actually see them close enough.

as for the bar, cant you optimize that somehow? 8800 triangles seems a bit high for a collision mesh
Title: Re: How to improve the performance of collision check in a racing game?
Post by: EgonOlsen on April 18, 2013, 08:33:54 pm
~8000 polygons for a collision mesh is fine if you are using an octree. Something around 500 polygons per leaf should do it. For reference: The landscape in my RPG game uses around 8000 polygons and an octree with 500 polygons per leaf and it works fine even on older devices. Just make sure that you are using it for collision detection only, because using it for rendering might be slower than simply rendering the mesh at once.
Title: Re: How to improve the performance of collision check in a racing game?
Post by: kiffa on April 19, 2013, 05:03:03 am
To dutch_delight:
  Thanks for your advice.
  And in your game, how do you implement the AI? I was bothered by AI these days, i didn't use the waypoints, i just let them run, then check collision, then move by the result of  checkForCollisionEllipsoid(). Codes:
 
Code: [Select]
SimpleVector step = calcStepBySpeed(deltaTime);
  step = npcCar.checkForCollisionEllipsoid(step, mEllipsoid, 3);
  step.y = 0; // flat road, no up an downs.
  npcCar.translate(step);
  I think this is a rather bad AI :-\ , and i need  check collision every frame. If you would share your design, there will be many thanks.

To EgonOlsen:
  What dose the return value of checkForCollisionEllipsoid() accurately mean? Does the return vector has the same length as the origin vector? Could you explain the algorithm? Could i use obb instead of ellipsoid in jPCT-AE? Which is more suitable for my game?


And there are 7 cars in my game(1player + 6 npc), so there are 7 checkForCollisionEllipsoid() calls per frame, i think this is expensive, any suggestions?
Title: Re: How to improve the performance of collision check in a racing game?
Post by: EgonOlsen on April 19, 2013, 07:27:08 am
What dose the return value of checkForCollisionEllipsoid() accurately mean?

It's the corrected translation vector. It can have any lenght but it won't be larger than the initial translation. It's the translation to apply to the object to avoid a collision while sliding along the obstacle.

And there are 7 cars in my game(1player + 6 npc), so there are 7 checkForCollisionEllipsoid() calls per frame, i think this is expensive, any suggestions?

If they interact with each other only, then this shouldn't be a problem. If they all are supposed to collide with the track's borders, then it might become one. All i can say is: Be creative. The build-in collision detection method smight not be the best ones to choose for a car racing game. You could, for example, simplify your car checks to simple 2d box checks, which might be better to handle car physics anyway.
Title: Re: How to improve the performance of collision check in a racing game?
Post by: dutch_delight on April 19, 2013, 08:04:40 pm
What I do for my AI is a very simple path finding, I've got a few paths stored in an array.
When an AI car is close enough to the current point that it is moving towards, it steers to the next point.

Getting some pretty nice results.