Iam trying to do some kind of sweep test with rays because my projectiles are flying simply too fast.
But my problem now is that the pick returns all objects/enemies in the whole scene.
The involved vectors have valid non-zero values, I've checked that.
What else could I've done wrong?
void update(float tfp)
{
PickResults pr = new TrianglePickResults();
if (projectile.getPhysicsNode().getWorldTranslation().equals(lastPos))
{
return;
}
pr.clear();
Ray mouseRay = new Ray(projectile.getPhysicsNode().getWorldTranslation(), lastPos);
InGameState.rootNode.findPick(mouseRay, pr);
for (int i = 0; i < pr.getNumber(); i++)
{
// do collision logic
}
}
Maybe test the vectors for length as well as equality, you could be dealing with floating point errors. If both your rays are so close, that when divided, there maybe sufficient round-off error that they become identical. This is just a guess though, you may also want to try updating the world data right before your pick routine.
Spatial.updateWorldData(0);
I tried projectile.getPhysicsNode().updateWorldData(0); but get an stack overflow, I suppose this method calls update(float tpf) internally where my picking routine resides in
I now also try to take the distance of both vectors into account, but the error stays the same.
void update(float tpf)
{
// projectile.getPhysicsNode().updateWorldData(0);
projectile.getPhysicsNode().updateWorldVectors();
Vector3f projectilePos = projectile.getPhysicsNode().getWorldTranslation();
if (projectilePos.equals(lastPos))
{
return;
}
if (projectilePos.distance(lastPos) < 0.1f)
{
return;
}
pr.clear();
Ray ray = new Ray(projectilePos, lastPos);
InGameState.RootNodeInstance.findPick(ray, pr);
for (int i = 0; i < pr.getNumber(); i++)
{
// do collision logic
}
lastPos.set(projectilePos);
}
you should not need to update world vectors yourself, but:
- new Ray(projectilePos, lastPos) does not make much sense as the second parameter should be a direction not a position.
- how do you initialize lastPos? (make sure it's a new vector, not an assignment from world translation)
- if that still does not help show your ray in your scene (e.g. with a Line)
Or: you are already using jME Physics for your projectile - why don't you use a physics ray? Collision detection should be much faster then. (You should update the ray in a step callback then for good accuracy)
1) new Ray(projectilePos, lastPos) does not make much sense as the second parameter should be a direction not a position.
Oh, thanks :) I now converted it to a direction: Ray ray = new Ray(lastPos, projectilePos.subtract(lastPos).normalize());
2) how do you initialize lastPos? (make sure it's a new vector, not an assignment from world translation)
I do lastPos.set(projectilePos); which should only overwrite its values.
3) if that still does not help show your ray in your scene (e.g. with a Line)
I just tried it and it looks ok.
But then! I noticed something very strange which I suspect to be the cause of all my troubles: The bullets, although I seem to be very near to the enemy and the bullets are flying very fast, seem never to reach its destination. They fly fast away and are getting smaller and smaller but they are always stay in front of the enemyship, never fly through or impact it.
It looks as if the enemy is miles away, although Iam thinking that Iam right in front of him.
What strange thing is that??? :?
It could be a visual thing, are the missiles using the depth buffer?
(HINT: )
Vector3f tempDirection = new Vector3f();
Ray tempRay = new Ray();
private void method() {
tempDirection.set( projectilePos );
tempDirection.subtractLocal( lastPos );
tempDirection.normalizeLocal();
ray.setOrgin( lastPos );
ray.setDirection( tempDirection );
}
(GC can be a killer in jME so best to avoid it)
I also wonder about your physics, maybe you should try irrisor's advice about the physics ray. He is the (physics) man afterall....
irrisor:
I suggested this because someone suggested it to me with my camera.lookAt() problems and it solved the issue. If I don't call that whenever I use the lookAt method on a moving physics node, the camera looks at (0, 0, 0) until the node stops moving; then camera looks right at the correct node. I think this may have something to do with the advancedVehicleTest problem I mentioned also, but that may just be coincidence.
Post I'm talking about --> http://www.jmonkeyengine.com/jmeforum/index.php?topic=7486.msg58843#msg58843
Thank you very much for the reply!
Yes, the strange visual effect was really a missing call to updateRenderState(), so my node had no zbuffer
Good tip with the GC, I sometimes saw my game behaving as it would run at 5fps although the counter displayed 500fps…I suppose now the GC kicked in every few millis so it appeared really slow although overall performance was good.
No I do not use physics for my bullets anymore (but my identifiers still suggests this, no time to fix yet) since ODE keeped crashing (maybe over 1000 projectiles flying around it a bit too hard for every physics engine…)
But my original picking problem still is there, only that my new code is picking nothing at all :?
void processCollisions()
{
projectile.getPhysicsNode().updateWorldVectors();
Vector3f projectilePos = projectile.getPhysicsNode().getWorldTranslation();
pr.clear();
Ray ray = new Ray(lastPos, projectilePos.subtract(lastPos).normalize());
InGameState.RootNodeInstance.findPick(ray, pr);
for (int i = 0; i < pr.getNumber(); i++)
{
// do collision logic
}
lastPos.set(projectilePos);
}
:? Well, suddenly it works, not sure what I've done different now. I changed the code creating the bounding volumes and updating stuff…maybe this made my picking routine work now
Anyway, thank you all for your help and suggestions!
Actually, I think picking needs bounding boxes; but I could be wrong. Yeah the GC thing has been drilled into me (with good reason) by all the fine peeps here.