0

I'm developing a simple game, where one can move a character through a maze. To avoid collision detecting i thought it might be a good idea to set a path through the maze and move the character along this path.

The character shall be moved via touch. So i thought i can just test if the touchlocation.x or touchlocation.y are contained in the path and then set the characters image on the location corresponding to either the touchlocation.x or touchlocation.y value.

Unfortunately i found out i can only test if a CGPoint is contained in a path and not if the x or the y coordinate of that point would be on that path.

Does anyone of you know how to do this?

thx in advance

Mav

----EDIT----

This is the code how i create my CGPath:

mazePath = CGPathCreateMutable();
CGPathMoveToPoint(mazePath,NULL, 148, 723);
CGPathAddLineToPoint(mazePath, NULL, 148, 404);
CGPathAddLineToPoint(mazePath, NULL, 129, 353);
CGPathAddLineToPoint(mazePath, NULL, 129, 174);
CGPathAddArcToPoint(mazePath, NULL, 129, 174, 148, 404, 10);
CGPathAddLineToPoint(mazePath, NULL, 148, 404);
CGPathAddLineToPoint(mazePath, NULL, 290, 640);
CGPathAddArcToPoint(mazePath, NULL, 290, 640, 326, 196, 10);
CGPathAddArcToPoint(mazePath, NULL, 326, 296, 359, 234, 10);
CGPathAddLineToPoint(mazePath, NULL, 395, 198);
CGPathAddLineToPoint(mazePath, NULL, 396, 147);
CGPathAddLineToPoint(mazePath, NULL, 683, 147);
CGPathAddLineToPoint(mazePath, NULL, 683, 232);
CGPathAddLineToPoint(mazePath, NULL, 480, 232);
CGPathAddLineToPoint(mazePath, NULL, 461, 332);
CGPathAddLineToPoint(mazePath, NULL, 235, 345);
CGPathAddLineToPoint(mazePath, NULL, 219, 248);
CGPathAddLineToPoint(mazePath, NULL, 235, 345);
CGPathAddLineToPoint(mazePath, NULL, 233, 607);
CGPathAddLineToPoint(mazePath, NULL, 324, 605);
CGPathAddLineToPoint(mazePath, NULL, 324, 464);
CGPathAddArcToPoint(mazePath, NULL, 324, 464, 364, 425, 10);
CGPathAddLineToPoint(mazePath, NULL, 537, 426);
CGPathAddArcToPoint(mazePath, NULL, 537, 426, 559, 396, 10);
CGPathAddLineToPoint(mazePath, NULL, 559, 396);
CGPathAddLineToPoint(mazePath, NULL, 567, 330);
CGPathAddLineToPoint(mazePath, NULL, 678, 330);
CGPathAddLineToPoint(mazePath, NULL, 678, 504);
CGPathAddLineToPoint(mazePath, NULL, 412, 510);
CGPathAddLineToPoint(mazePath, NULL, 412, 598);
CGPathAddLineToPoint(mazePath, NULL, 691, 598);
CGPathAddLineToPoint(mazePath, NULL, 690, 723);   
Maverick1st
  • 3,774
  • 2
  • 34
  • 50

2 Answers2

0

i have a approach ,i think you can try this way. by using the method CGPathContainsPoint you can know whether a point is present in the path,if the condition fails it mean that the path doesn't contain the point. Now in your case you can write a condition to check whether the point is contained in the path ,and in else part develop a logic to check our point's x and y is equal to the currentPoint of the path.

CGRect box= CGPathGetBoundingBox(myPath);


if([CGPathContainsPoint:myPoint]){
// we know that the point is present in the path
}
else{

if(mypoint.x>mypoint.x && myPoint.x<=CGRectGetMaxX(box.frame))
NSLog(@"x coordinate of the point is  on the path");
if(myPoint.y>myPoint.y && myPoint.y<=CGRectGetMaxY(box.frame))
NSLog(@"y coordinate of myPoint is on the path");

}

try doing this i think it works. There might be even a better approach,try in others ways too and if u find any such plz update us.

TNQ

Dinakar
  • 1,198
  • 15
  • 37
-1

Have a look at this thread, it might help you NSLog with CGPoint data

Community
  • 1
  • 1
Armand
  • 9,847
  • 9
  • 42
  • 75
  • I think you misunderstood me. I do know how to log a CGPoint or get the x or y value out of a CGPoint. Thats not the problem. The problem is, i don't know how to check if this x or y value for itself is on the path. Presume i have a path from (0,0) to (100,10) and want to move an image along the path as soon as the user moves his finger more to the right then to the bottom. – Maverick1st Jul 13 '11 at 10:45