Skip to content

Commit

Permalink
Add view to capture and log taps as json coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
jdetloff committed May 5, 2013
1 parent 87704ef commit 2ef2291
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 28 deletions.
6 changes: 6 additions & 0 deletions GraceWorld.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@
0F7C4B6B172C475E00037EAC /* OrbitalSurface.m in Sources */ = {isa = PBXBuildFile; fileRef = 0F7C4B69172C475E00037EAC /* OrbitalSurface.m */; };
0F7C4B6F172C62B000037EAC /* OneWayEdgeCollisionDetector.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0F7C4B6D172C62B000037EAC /* OneWayEdgeCollisionDetector.cpp */; };
0F7C4B70172C62B000037EAC /* OneWayEdgeCollisionDetector.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0F7C4B6D172C62B000037EAC /* OneWayEdgeCollisionDetector.cpp */; };
0FBAB2171735C548007785BF /* CoordinateReporter.m in Sources */ = {isa = PBXBuildFile; fileRef = 0FBAB2161735C548007785BF /* CoordinateReporter.m */; };
0FBAB21A1735D404007785BF /* levelData.json in Resources */ = {isa = PBXBuildFile; fileRef = 0FBAB2191735D404007785BF /* levelData.json */; };
0FBAB21B1735D404007785BF /* levelData.json in Resources */ = {isa = PBXBuildFile; fileRef = 0FBAB2191735D404007785BF /* levelData.json */; };
0FBAB2411735D485007785BF /* NSObject+SBJSON.m in Sources */ = {isa = PBXBuildFile; fileRef = 0FBAB2361735D485007785BF /* NSObject+SBJSON.m */; };
Expand Down Expand Up @@ -647,6 +648,8 @@
0F7C4B69172C475E00037EAC /* OrbitalSurface.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OrbitalSurface.m; sourceTree = "<group>"; };
0F7C4B6D172C62B000037EAC /* OneWayEdgeCollisionDetector.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = OneWayEdgeCollisionDetector.cpp; sourceTree = "<group>"; };
0F7C4B6E172C62B000037EAC /* OneWayEdgeCollisionDetector.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OneWayEdgeCollisionDetector.h; sourceTree = "<group>"; };
0FBAB2151735C548007785BF /* CoordinateReporter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CoordinateReporter.h; sourceTree = "<group>"; };
0FBAB2161735C548007785BF /* CoordinateReporter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CoordinateReporter.m; sourceTree = "<group>"; };
0FBAB2191735D404007785BF /* levelData.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = levelData.json; sourceTree = "<group>"; };
0FBAB2341735D485007785BF /* JSON.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSON.h; sourceTree = "<group>"; };
0FBAB2351735D485007785BF /* NSObject+SBJSON.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+SBJSON.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -974,6 +977,8 @@
0F37B53516D625F8000653B1 /* NPCCreator.h */,
0F37B53616D625FD000653B1 /* NPCCreator.m */,
0F770E1E16BBC12900D64191 /* Supporting Files */,
0FBAB2151735C548007785BF /* CoordinateReporter.h */,
0FBAB2161735C548007785BF /* CoordinateReporter.m */,
0FBAB2331735D485007785BF /* JSON */,
);
path = GraceWorld;
Expand Down Expand Up @@ -1864,6 +1869,7 @@
0F4CE35E1730D1BA00637555 /* OrbitController.m in Sources */,
0F4CE3611730FF1D00637555 /* PolygonView.m in Sources */,
0FCD44421733815800F7BED4 /* PlayerPhysicsWrapper.m in Sources */,
0FBAB2171735C548007785BF /* CoordinateReporter.m in Sources */,
0FBAB2411735D485007785BF /* NSObject+SBJSON.m in Sources */,
0FBAB2431735D485007785BF /* NSString+SBJSON.m in Sources */,
0FBAB2451735D485007785BF /* SBJSON.m in Sources */,
Expand Down
21 changes: 21 additions & 0 deletions GraceWorld/CoordinateReporter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// CoordinateReporter.h
// GraceWorld
//
// Created by John Detloff on 5/4/13.
// Copyright (c) 2013 Uebie. All rights reserved.
//

#import <UIKit/UIKit.h>

@protocol CoordinateReporterDelegate <NSObject>
- (NSDictionary *)attributesToReportForCoordinate:(CGPoint)coordinate;
@end

@interface CoordinateReporter : UIView
@property (nonatomic, weak) id<CoordinateReporterDelegate>delegate;

- (void)clear;
- (void)print;

@end
49 changes: 49 additions & 0 deletions GraceWorld/CoordinateReporter.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// CoordinateReporter.m
// GraceWorld
//
// Created by John Detloff on 5/4/13.
// Copyright (c) 2013 Uebie. All rights reserved.
//

#import "CoordinateReporter.h"

@implementation CoordinateReporter {
NSMutableString *_coordinatesString;
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (!_coordinatesString) {
_coordinatesString = [@"" mutableCopy];
}

UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
NSDictionary *propertyDict = [self.delegate attributesToReportForCoordinate:point];

NSMutableString *reportString = [@"{" mutableCopy];

[propertyDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSString *keyString = (NSString *)key;
NSNumber *val = (NSNumber *)obj;
[reportString appendFormat:@"\"%@\":%.2f,",keyString,[val floatValue]];
}];
[reportString replaceCharactersInRange:NSMakeRange([reportString length]-1, 1) withString:@""];
[reportString appendString:@"}"];

[_coordinatesString appendFormat:@"%@,\n",reportString];
}


- (void)clear {
_coordinatesString = nil;
}


- (void)print {
NSLog(@"%@",_coordinatesString);
}


@end
70 changes: 42 additions & 28 deletions GraceWorld/GraceGameViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@
#import "UIView+b2BodyBackedView.h"
#import "OrbitalCoordinate.h"
#import "OrbitalSurface.h"
#import "CoordinateReporter.h"
#import <QuartzCore/QuartzCore.h>

#define kReportingCoordinates NO

#define kWorldSize CGSizeMake(3879*1.2, 2732*1.2)
#define kLandscapeSize CGSizeMake(1024, 768)
#define kWorldOffsetFromCenter UIOffsetMake(0,50)

#define kOrbitDistanceInPixels 1161.f
#define kOrbitToPixelsRatio (100/kOrbitDistanceInPixels)

@interface GraceGameViewController () <RunJumpCrawlDelegate, WorldPhysicsDelegate>
@interface GraceGameViewController () <RunJumpCrawlDelegate, WorldPhysicsDelegate, CoordinateReporterDelegate>
@end


Expand Down Expand Up @@ -96,18 +99,31 @@ - (void)viewDidLoad {
[_physicsBackedViews addObject:_boyContainer];

_worldDataStore = [[WorldDataStore alloc] init];
[_worldDataStore loadLevelData];
[self addTestObjects];

_gameLoop = [NSTimer scheduledTimerWithTimeInterval:1/60.0 target:self selector:@selector(gameTick) userInfo:nil repeats:YES];

if (kReportingCoordinates) {
CoordinateReporter *reporter = [[CoordinateReporter alloc] initWithFrame:self.view.bounds];
reporter.delegate = self;
[_runJumpCrawlEventView addSubview:reporter];

UIButton *clearButton = [[UIButton alloc] initWithFrame:CGRectMake(950, 40, 50, 50)];
clearButton.backgroundColor = [UIColor blueColor];
[clearButton addTarget:reporter action:@selector(clear) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:clearButton];

UIButton *printButton = [[UIButton alloc] initWithFrame:CGRectMake(850, 40, 50, 50)];
printButton.backgroundColor = [UIColor redColor];
[printButton addTarget:reporter action:@selector(print) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:printButton];
}

}


- (void)addTestObjects {
// OrbitalCoordinate *coordA = [[OrbitalCoordinate alloc] initWithHeight:105 angle:1.46];
// OrbitalCoordinate *coordB = [[OrbitalCoordinate alloc] initWithHeight:93 angle:1.46];
// OrbitalSurface *surface = [[OrbitalSurface alloc] initWithCoordA:coordA coordB:coordB];
// [_physicsController placeOrbitalSurfaces:@[surface]];

NSArray *bridgeSurfaces = [_worldDataStore bridgeOrbitalCoordinates];
[_physicsController placeOrbitalSurfaces:bridgeSurfaces];

Expand Down Expand Up @@ -232,27 +248,6 @@ - (void)gameTick {
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
GraceWorldView *layer = [_worldLayers objectAtIndex:0];

UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];

CGPoint center = [layer worldCenter];
center = [self.view convertPoint:center fromView:layer];

CGFloat convertedX = center.x - (1024/2 - touchPoint.x);
CGFloat angle = [layer angle];
CGFloat touchAngle = - atan2(touchPoint.y - center.y, center.x - convertedX) - M_PI/2;

CGFloat dx = (touchPoint.x - center.x) * kOrbitToPixelsRatio;
CGFloat dy = -(touchPoint.y - center.y) * kOrbitToPixelsRatio;
CGFloat height = sqrtf(dx*dx + dy*dy);

NSLog(@"*** touch at angle %f height %f ***", angle - touchAngle, height);
}


#pragma mark - Physics Delegate


Expand Down Expand Up @@ -310,6 +305,25 @@ - (void)jump {
b2Vec2 velocity(boyPhysics->GetLinearVelocity().x, 12);
boyPhysics->SetLinearVelocity(velocity);
}
}
}


- (NSDictionary *)attributesToReportForCoordinate:(CGPoint)coordinate {
GraceWorldView *layer = [_worldLayers objectAtIndex:0];
CGPoint center = [layer worldCenter];
center = [self.view convertPoint:center fromView:layer];

CGFloat convertedX = center.x - (1024/2 - coordinate.x);
CGFloat angle = [layer angle];
CGFloat touchAngle = - atan2(coordinate.y - center.y, center.x - convertedX) - M_PI/2;

CGFloat dx = (coordinate.x - center.x) * kOrbitToPixelsRatio;
CGFloat dy = -(coordinate.y - center.y) * kOrbitToPixelsRatio;
CGFloat height = sqrtf(dx*dx + dy*dy);

NSNumber *angleNum = [NSNumber numberWithFloat:angle-touchAngle];
NSNumber *heightNum = [NSNumber numberWithFloat:height];
return @{@"height":heightNum,@"angle":angleNum};
}

@end

0 comments on commit 2ef2291

Please sign in to comment.