Question

I already know how to check for collisions with the doesintersectNode-method in Cocos3d, but in my case, I want to avoid obstacles, before I get in touch with them. In example, I want to stop in front of a wall, before I crash against it.

For this reasons I wrote the methods getNodeAtLocation in my subclass of CC3Scene and -(BOOL)shouldMoveDirectionallywithDistance:(float)distance in the class of my person, which should move around. Unfortunately, I have some problems with the algorithm of the last method. Here the code:

-(BOOL)shouldMoveDirectionallywithDistance:(float)distance
{
BOOL shouldMove = NO;

float x = self.person.globalLocation.x;
float z = self.person.globalLocation.z;

int times = 5;
for (int i = 0; i < times; i++) {
    CC3Vector newPos = cc3v(x, 0.5, z);
    CC3PODResourceNode *obstacle = (CC3PODResourceNode *)[myScene getNodeAtLocation:newPos];
    if (obstacle) {
        return NO;
    }else{
        shouldMove = YES;
    }
    x += self.person.globalForwardDirection.x * distance / times;
    z += self.person.globalForwardDirection.z * distance / times;
}


return shouldMove;
}

In this method, I get the important parts of the coordinates (for my proposal just the x- and z-values) and increase them by a fifth of the forwardDirection. I decided, that this makes sense, when the obstacle is i.e. a thin wall. But for reasons I don't know, this method doesn't work, and the person is able to walk through this wall. So where is the problem in my code?

I strongly believe, that the getNodeAtLocation-method works correctly, as I tested it multiple times, but maybe there are my mistakes:

-(CC3Node *)getNodeAtLocation:(CC3Vector )position
{
CC3Node *node = nil;
for (CC3PODResourceNode *aNode in self.children) {
    if ([aNode isKindOfClass:[CC3PODResourceNode class]] ) {
        for (CC3PODResourceNode *child in aNode.children) {
            if (CC3BoundingBoxContainsLocation(child.globalBoundingBox, position)) {
                node = aNode;
            }
        }
    }

}

return node;
}

To conclude, in my view the mistake is in the -(BOOL)shouldMoveDirectionallywithDistance:(float)distance-method. I suppose, that something is wrong with the increase of the x- and z-values, but I couldn't figure out, what exactly is incorrect.

Was it helpful?

Solution 2

After several attempts, it appears to be easier as I thought: Just using the doesIntersectNode method has the right effect for me. But please notice, that this is not a real solution to the problem of stopping in front of obstacles.

OTHER TIPS

If you are still interested in finding an answer to this problem. I may be able to provide you with an alternative solution. I am about to release free source for a 3d collision engine I ported to cocos3d, and it will give you more flexibility than simply stoping an object in front of another.

I am currently polishing out the code a little for easy use, but if you are interested you can email me to: waywardson07@aol.com

you could also get a little preview of the engine in action here: http://www.youtube.com/watch?v=QpYZlF7EktU

Note: the video is a little dated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top