Unexpected behavior when working with PhysicsBodies in SpriteKit - xcode

I´ve got some issues with the physics bodies in this exercise.
when the dot(snake) is moving very fast, it "squeezes" itself through the physics bodies of the bricks. Can someone help, please?
snake:
self.physicsBody.categoryBitMask = snakeCategory;
self.physicsBody.collisionBitMask = lineCategory | brickCollissionCategory;
self.physicsBody.contactTestBitMask = brickContactCategory;
self.physicsBody.usesPreciseCollisionDetection = YES;
self.physicsBody.allowsRotation = NO;
self.physicsBody.dynamic = YES;
self.physicsBody.restitution = 0;
self.physicsBody.affectedByGravity = NO;
bricks:
self.physicsBody.categoryBitMask = brickCollissionCategory;
self.physicsBody.collisionBitMask = snakeCategory;
self.physicsBody.dynamic = NO;
self.physicsBody.affectedByGravity = NO;
self.physicsBody.usesPreciseCollisionDetection = YES;
self.physicsBody.restitution = 0;

Related

CIFilter and RAW image support on Yosemite

I'm playing around a bit with Core Image, CIFilter and RAW images. If I understand Apple's documentation correctly, to load a RAW image and display it in an image view, some code like this should do the trick:
NSURL *url = [NSURL URLWithString:#"file:///Users/emiel/Desktop/_MG_1087.CR2"];
// NSURL *url = [NSURL URLWithString:#"file:///Users/emiel/Desktop/_DSC0044.NEF"];
CIFilter *filter = [CIFilter filterWithImageURL:url options:nil];
NSLog(#"CIFilter: %#", filter);
CIImage *image = filter.outputImage;
NSCIImageRep *rep = [NSCIImageRep imageRepWithCIImage:image];
NSLog(#"%f - %f", rep.size.width, rep.size.height);
NSImage *nsImage = [[NSImage alloc] initWithSize:CGSizeMake(rep.size.width, rep.size.height)];
[nsImage addRepresentation:rep];
self.imageView.image = nsImage;
This code works fine for the .NEF (Nikon RAW) file, but the CR2 (Canon RAW) file gives me a totally black image. The image dimensions of the black image are correct though. What is going wrong here? Do I need to set some default to a non-default value? Preview/Aperture/iPhoto/etc all work fine with my .CR2 image, so it is possible to read them.
The only difference I can find is that apparently, for the CR2 file a version 6 of the decoder is used, and the NEF file is decoded by version 5. If I set the CR2 to be decoded by version 5 all works, but this seems to be a weird solution since I don't know if there is a version 5 or 6 or whatever for other RAW image formats.
NEF File:
{CIRAWFilterImpl {
inputBias = 3;
inputBoost = 1;
inputBoostShadowAmount = "0.9";
inputColorNoiseReductionAmount = "0.5";
inputDecoderVersion = 5;
inputDraftMode = 0;
inputEV = 0;
inputEnableNoiseTracking = 1;
inputEnableSharpening = 1;
inputEnableVendorLensCorrection = 0;
inputIgnoreOrientation = "<null>";
inputImageOrientation = 1;
inputLinearSpaceFilter = "<null>";
inputLuminanceNoiseReductionAmount = "0.1";
inputNeutralChromaticityX = "0.4212473792917087";
inputNeutralChromaticityY = "0.4060714400950335";
inputNeutralLocation = "[]";
inputNeutralTemperature = "3296.579476861167";
inputNeutralTint = "3.848452212530264";
inputNoiseReductionAmount = "<null>";
inputNoiseReductionContrastAmount = 0;
inputNoiseReductionDetailAmount = 0;
inputNoiseReductionSharpnessAmount = "0.5";
inputScaleFactor = 1;
}}
CR2 File:
{CIRAWFilterImpl {
inputBias = 3;
inputBoost = 1;
inputBoostShadowAmount = "0.9";
inputColorNoiseReductionAmount = "0.5";
inputDecoderVersion = 6;
inputDraftMode = 0;
inputEV = 0;
inputEnableNoiseTracking = 1;
inputEnableSharpening = 1;
inputEnableVendorLensCorrection = 0;
inputIgnoreOrientation = "<null>";
inputImageOrientation = 1;
inputLinearSpaceFilter = "<null>";
inputLuminanceNoiseReductionAmount = 0;
inputNeutralChromaticityX = "0.3575430840790828";
inputNeutralChromaticityY = "0.3599707869545071";
inputNeutralLocation = "[]";
inputNeutralTemperature = "4585.861752390336";
inputNeutralTint = "-11.18923977879745";
inputNoiseReductionAmount = "<null>";
inputNoiseReductionContrastAmount = 0;
inputNoiseReductionDetailAmount = 0;
inputNoiseReductionSharpnessAmount = "0.5";
inputScaleFactor = 1;
}}
Help?
This appears to be a regression in 10.10.2 and/or the latest RAW compatibility update, according to posts in Apple's Devforums. The only known workaround is to manually override inputDecoderVersion to 5.
Please also file a Radar, so that Apple takes notice.

XCODE reading text from file erases variables

This my be the strangest error I've ever seen.
I'm creating a maze game that loads levels from text files, but if the file has more than three rows, it seems to randomly change most of my variables. I load in a text file then run through the lines and create an object at the proper location based on its letter.
Any advice?
Text File:
TTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTT
TTTTTRPPGPPDTTTTT
TTTTTPTPTPTPTTTTT
TTTTTPPPWPPPTTTTT
TTTTTPTPPPTPTTTTT
TTTTTPPPTPPPTTTTT
TTTTTPTPPPTPTTTTT
TTTTTUPPPPPLTTTTT
TTTTTTTTPTTTTTTTT
TTTTTTTTBTTTTTTTT
TTTTTTTTPTTTTTTTT
NSString *path = [[NSBundle mainBundle] pathForResource:#"map2c" ofType:#"txt"];
NSString *contents = [NSString stringWithContentsOfFile:path encoding:NSASCIIStringEncoding error:nil];
NSArray *lines = [contents componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"\r\n"]];
int y = 0;
for (NSString* line in lines) {
NSString* ty = #"TBGWUDLRP";
char *str = (char *)[line UTF8String];
char *str2 = (char*)[ty UTF8String];
grannyMoving = 2;
int x = str2[0];
char x2 = str2[1];
char GrannyStr = str2[2];
char wolfStr = str2[3];
char UpStr = str2[4];
char DownStr = str2[5];
char leftStr = str2[6];
char rightStr = str2[7];
char PathStr = str2[8];
y--;
maxY = y;
if (line.length) {
maxI = line.length;
for (int i = 0; i< line.length; i++) {
SKSpriteNode* tree = [SKSpriteNode spriteNodeWithImageNamed:#"tree3"];
tree.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(31, 31)];
tree.physicsBody.affectedByGravity = false;
tree.physicsBody.dynamic = NO;
tree.position = CGPointMake(64*i+20, 64*y+800);
tree.zPosition = -1* tree.position.y;
From the documentation:
This C string is a pointer to a structure inside the string object,
which may have a lifetime shorter than the string object and will
certainly not have a longer lifetime. Therefore, you should copy the C
string if it needs to be stored outside of the memory context in which
you use this property.
To fix this you have to make a copy of the characters returned by UTF8String. This answer provides two solutions.

SpriteKit Animation Physics

is there a way to apply skphysics to a animated character? Okay I have my player play an animation when I move it around the screen and below is it's animation code but for some reason when I want to apply physics to my animation, I get an error saying wrong data type.
Sending 'SKSpriteNode *_strong' to parameter of incompatible type 'CGRect' (aka 'struct CGRect')
NO Reputation and I can't answer my own question so I posted here.
actually never mind I had to add this _Player.frame and I am hoping this is right and is putting physics on my character. However, please correct me if I am wrong. thanks.
- (void) Player {
_Player = [SKSpriteNode spriteNodeWithImageNamed:#"bird"];
_Player.xScale = 0.88;
_Player.yScale = 0.88;
_Player.position = CGPointMake(100, 100);
[self addChild:_Player];
// 1
NSMutableArray *PlayerCh = [NSMutableArray arrayWithCapacity:2];
// 2
for (int i = 1; i < 2; i++) {
NSString *mytextureName = [NSString stringWithFormat:#"bird%d", i];
SKTexture *ItsTexture = [SKTexture textureWithImageNamed:mytextureName];
[PlayerCh addObject:ItsTexture];
}
// 3
for (int i = 2; i > 1; i--) {
NSString *mytextureName = [NSString stringWithFormat:#"bird%d", i];
SKTexture *ItsTexture = [SKTexture textureWithImageNamed:mytextureName];
[PlayerCh addObject:ItsTexture];
}
// 4
_PlayerAnimation = [SKAction animateWithTextures:PlayerCh timePerFrame:0.1];
// SKPHYSICS FOR PLAYER
SKPhysicsBody *MYPLAYER = [SKPhysicsBody bodyWithCircleOfRadius:_Player]; // I get a error here.
}
Of course this program has to crash when it runs
[SKPhysicsBody bodyWithCircleOfRadius:_Player] expects a float not an SKPriteNode, what you probably want to do is change this line of code to the following:
SKPhysicsBody *MYPLAYER = [SKPhysicsBody bodyWithCircleOfRadius:_Player.size.width/2];

IBAction float, have a equal different numbers depending on input

New to objective-c and I just can't find the answer to this question anywhere. I'm running a IBAction for a calculation but I need the input from a label to actually equal something else in the equation.
For example:
-(IBAction)calculate; {
float a = 1.05 if ([label1.text] == 1in);
a = 2.07 if ([label1.text] == 2in);
a = 3.07 if ([label1.text] == 3in);
float b = a*([textField1.text floatValue]);
label2.text = [[NSString alloc] initWithFormat:#"%2.f", b];
}
I know I'm not even close to getting it right but I hope you get the idea as to what I'm looking for.
Thank you!
- (void)calculate {
float a;
if ([self.inputField.text isEqualToString:#"1in"]) a = 1.05f;
else if ([self.inputField.text isEqualToString:#"2in"]) a = 2.07f;
else if ([self.inputField.text isEqualToString:#"3in"]) a = 3.07f;
else a = 0.0f;
if (a) {
float b = a * ([self.inputField.text floatValue]);
self.inputField.text = [[NSString alloc] initWithFormat:#"%.2f", b];
}
else self.inputField.text = #"Invalid";
}
- (void)viewDidLoad
{
//...
self.inputField = [[UITextField alloc] initWithFrame:CGRectMake(10.0f, 20.0f, 300.0f, 30.0f)];
[self.inputField setBackgroundColor:[UIColor grayColor]];
[self.view addSubview:self.inputField];
UIButton * doneButton = [[UIButton alloc] initWithFrame:CGRectMake(10.0f, 55.0f, 300.0f, 30.0f)];
[doneButton setBackgroundColor:[UIColor blackColor]];
[doneButton setTitle:#"Done" forState:UIControlStateNormal];
[doneButton addTarget:self action:#selector(calculate) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:doneButton];
[doneButton release];
}
Note: I replaced the output format %2.f to %.2f, cause I guess you may need this format. E.g:
Input > 1in
Output< 1.05
Input > 2in
Output< 4.14
Input > 3in
Output< 9.21

EXC_BAD_ACCESS error in Xcode

I really need you guy HELP , I run my program in Xcode and its successful but later,
Its show me this error: **Thread 1: Program received signal :"EXC_BAD_ACCESS" on my program line that I have **bold below :
- (NSString *) ocrImage: (UIImage *) uiImage
{
CGSize imageSize = [uiImage size];
double bytes_per_line = CGImageGetBytesPerRow([uiImage CGImage]);
double bytes_per_pixel = CGImageGetBitsPerPixel([uiImage CGImage]) / 8.0;
CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider([uiImage CGImage]));
const UInt8 *imageData = CFDataGetBytePtr(data);
// this could take a while. maybe needs to happen asynchronously.
**char* text = tess->TesseractRect(imageData,(int)bytes_per_pixel,(int)bytes_per_line, 0, 0,(int) imageSize.height,(int) imageSize.width);**
// Do something useful with the text!
NSLog(#"Converted text: %#",[NSString stringWithCString:text encoding:NSUTF8StringEncoding]);
return [NSString stringWithCString:text encoding:NSUTF8StringEncoding];
}
Thank you guy .
make sure that imageData is not NULL here. That's the most common cause of what you're seeing. You should reconsider your title to something more related to your problem, and focus on the stacktrace and all the variables you are passing to TesseractRect().
The other major likelihood is that tess (whatever that is) is a bad pointer, or that is not part of the correct C++ class (I assume this is Objective-C++; you're not clear on any of that).
- (NSString *)readAndProcessImage:(UIImage *)uiImage
{
CGSize imageSize = [uiImage size];
int bytes_per_line = (int)CGImageGetBytesPerRow([uiImage CGImage]);
int bytes_per_pixel = (int)CGImageGetBitsPerPixel([uiImage CGImage]) / 8.0;
CFDataRef data =
CGDataProviderCopyData(CGImageGetDataProvider([uiImage CGImage]));
const UInt8 *imageData = CFDataGetBytePtr(data);
// this could take a while. maybe needs to happen asynchronously?
char *text = tess.TesseractRect(imageData, bytes_per_pixel, bytes_per_line, 0,
0, imageSize.width, imageSize.height);
NSString *textStr = [NSString stringWithUTF8String:text];
delete[] text;
CFRelease(data);
return textStr;
}

Resources