1

I am making a driving game for the iOS, where a car (UIView) is put on top of the google maps ios api. I am having trouble detecting collisions with the car and the grey buildings that appear on the map.

I have been trying to do it by pixel color i.e. checking if the pixel color, just ahead of the car, is the color of a buildings roof (grey). There is no direct access to an iPhones pixel color, so I am using the google maps screenshot method to get an image and get the pixel color from that. The problem is, is that I can only take a screen shot of the screen I just left. I am using mapImage = [GMSScreenshot screenshotOfMainScreen];. I have also tried getting the window and screen and calling mapImage = [GMSScreenshot screenshotOfScreen:topWindow.screen]; Both have the same effect.

What is a solution to this problem? Can you think of a better way of handling collisions for this situation? Does reverse geocoding have the ability to detect the tops of buildings vs streets? Thanks for any and all help!

EDIT: Images: Interface Builder: Left is the main menu, right is the game. The Ui image view in the upper left corner is set as the current screen shot image for referencing purposes. This how I knew It was off

Game Play, As you can see, it is only presenting the previous menu.

My viewdidLoad function: Not much other than this thats related to getting the image.

- (void)viewDidLoad
{
    [super viewDidLoad];

    cameraPosition = CLLocationCoordinate2DMake(42.271291, -83.729918);
    moveDistance = .00055;

    cA = [UIColor colorWithRed:.937255 green:.921569 blue:.886275 alpha:1];

    camera = [GMSCameraPosition cameraWithLatitude:cameraPosition.latitude
                                         longitude:cameraPosition.longitude
                                              zoom:19
                                           bearing:0
                                      viewingAngle:0];


    mapView_ = [GMSMapView mapWithFrame:CGRectMake(0, 0, 480, 300) camera:camera];
    mapView_.myLocationEnabled = NO;

    [self.view addSubview:mapView_];

    car_ = [[Car alloc] initWithFrame:CGRectMake(240, 150, 13, 29) withImage:[UIImage imageNamed:@"carA-01.png"]];
    [self.view addSubview:car_];

    [self.view addSubview:controllerView];

    updateClock = [NSTimer scheduledTimerWithTimeInterval:(1/20)
                                                   target:self
                                                 selector:@selector(update)
                                                 userInfo:nil
                                                  repeats:YES];

    crashClock = [NSTimer scheduledTimerWithTimeInterval:1
                                                  target:self
                                                selector:@selector(checkCrash)
                                                userInfo:nil
                                                 repeats:YES];

    UIWindow *topWindow = [[UIApplication sharedApplication].windows objectAtIndex:0];

    mapImage = [GMSScreenshot screenshotOfScreen:topWindow.screen];


    //  mapImage = [GMSScreenshot screenshotOfMainScreen];
}
Dobroćudni Tapir
  • 3,082
  • 20
  • 37

1 Answers1

0

Are you getting a new screenshot every frame (or on some interval), eg from your update callback?

The GMSMapView doesn't render immediately when you create or make changes to it - it renders later on its next update. Therefore when you take the screenshot in viewDidLoad you're probably getting what was on the screen at the time that the map view was added, ie the initial menu.

Saxon Druce
  • 17,406
  • 5
  • 50
  • 71
  • I was only calling it in viewdidload, then when I drive off the screen and change cameras. However, I found calling it in my update function was extremely slow and caused the car movement to be choppy. To be more specific the car movement is dictated by the updateClock timer, and the window was updated in the crashTimer. Is there a way to avoid this choppiness every second? I also tried calling the screen shot method twice, but this didn't work either. – Mark Mevorah Apr 24 '13 at 04:12
  • When you update the camera on the map, it will render 'some time later' on its next update. It seems to target about 30fps, so I guess it could be around 33ms or so later that the map gets rendered. When you change the camera, maybe you could set a timer to take the new screenshot after a small delay. – Saxon Druce Apr 25 '13 at 02:15
  • That worked, thanks! But now is appears that the image the screen shot is taking is rotated 90 degrees clockwise, any ideas how to fix this? – Mark Mevorah Apr 25 '13 at 05:20
  • The `GMSScreenshot` methods might assume that the device is in portrait mode, and so always return a portrait screenshot, instead of checking. You could try rotating it yourself afterwards, eg see http://stackoverflow.com/questions/1315251/how-to-rotate-a-uiimage-90-degrees. However if you don't need to see the screenshot, just look it up for collision detection, you could just adjust the x/y that you use to lookup to account for the rotation. – Saxon Druce Apr 26 '13 at 00:54
  • Also by the way, the latest version 1.2.x of the Google maps SDK has removed `GMSScreenshot`, and you now use `renderInContext`. Maybe this will use the device rotation correctly. There's some sample code here: http://stackoverflow.com/questions/16090637/google-maps-ios-sdk-1-2-need-snapshot-of-map-view – Saxon Druce Apr 26 '13 at 00:56