I'm new to Swift SpriteKit programming and the coordinate system is driving me crazy. I create a sprite and I want to move it to the four corners of the screen. So, I set the position to (0,0). That's off the bottom left corner of the screen. Through some manual testing I've developed the chart below. The lower left and upper right are what the iOS simulator report when I touch the screen.
I have 2 questions:
1: Is there a method of determining the coordinates of the lower left hand corner of the view? Maybe I could build a dictionary with the coordinate values and the determine the machine type and then set the offsets. But, that's a lot of work and might not be accurate for new devices. It just seems that there should be a scene or frame property that I can use to put an object at the bottom left of the window.
2: The math doesn't work. In the iPhone5, 300 (lower left x) + 320 (width) = 620, not the reported 727. Same issue is true with the y coordinates. How does this work?
I set as few parameters as possible. I have not changed the anchorPoint or position of the scene.
Device Size LL UR
iPhone4s (320,480) (260,0) (766,764)
iPhone5 (320,568) (300,0) (727,764)
iPhone5s (320,568) (298,0) (727,764)
iPhone6 (375,667) (297,1) (728,765)
iPhone6plus (414,736) (298,0) (728,766)
iPad2 (768,1024) (226,0) (800,768)
iPad Air (768,1024) (224,0) (800,767)
iPad Retina (768,1024) (225,0) (800,768)
Ok, I think I figured this out. Setting scene!.scaleMode=SKSceneScaleMode.ResizeFill allow me to identify the four corners of the screen. So, now I can determine when a sprite crosses the edge of the screen. This doesn't seem to distort my images. I haven't been able to test it on a read device yet, but it leaves a blank area around the iPad2.
Applause for the hard work! haha
If I was going about getting values for the lower coordinates, I would use CGRectGetMinX to get the x-coordinate and CGRectGetMinY to get the the y-coordinate likewise:
CGPoint minimum = CGPointMake(CGRectGetMinX(self.frame),CGRectGetMinY(self.frame));
Then, if you wanted to get the top coordinates just use the same things but say MaxX or MaxY. Yeah, the coordinates are a bit confusing but if you use those then it will be a breeze.
EDIT: If you need to find if a body has exited outside visible space, so far what has worked for me is making a physics body to detect contact with it on the edge
[SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame]
possibly another option you could try is to see the bounds of a UIScreen object.
Related
When Unity builds a VR project, by default it is set to make the two views stereoscopic. It slightly offsets the camera position of one eye to give the user a sense of depth.
For example a square will appear slightly to the left on the right view compared to the left view.
I want to make the camera truly monoscopic by removing the offset that is created when i build the project. Each camera should render all objects in exactly the same position for both eyes.
One of things i tried was creating two camera and setting them to the left and right eye. Then i manually set the position/rotation of one camera until it looked monoscopic
It worked fine on my pixel phone, but as soon as i put the project on my test phone i noticed that the difference in resolutions messed up the view i was going for. The blocks were not in the same position when i looked at both renders.
If anyone has any solutions or ideas as to how i can go about this, i would greatly appreciate it.
Thank you!
You can still use 2 cameras, but instead of offsetting them, you can just make the width of the camera half.
Make 2 cameras, set their positions to exactly the same.
On the left eye camera, set the width to 0.5 and the x position to 0.
On the right eye camera, set the width to 0.5 and the x position to 0.5.
You should now have 2 cameras rendering the exact same thing, but twice across the screen, with no sense of depth.
I'm building a game and I've noticed that when I do something like
node.position = CGPointMake(0, 0) //Bottom Left
node.anchorPoint = CGPointMake(0,0) //Bottom Left
that depending on which phone you have iPhone 4s, 5s, 6s ect. It shows up in different places.
How can I change it so that it uses the bounds of the screen size to work out its calculation like
self.view.frame.width/2
Cheers, I'm guessing its scale issue?
Use bounds
self.view.bounds.size
The answer lies in GameScene.sks. On the right side of the editor, you can see the dimensions of the screen. The point value is the scene's coordinate positions. To make it standard across devices, in App Delegate, under applicationDidFinishLaunchingWithOptions, scene.scalemode = .AspectFill. The scalemode has many different options to stretch the .sks file's dimensions to the device's screen. You can read about them here: understanding scalemodes. The scalemodes are called like so: .ResizeFill . ResizeFit etc..
I want to have a playing area with a square dimension, and a sidebar GUI that can be resized according to the resolution. I drew a picture that might help explain
I tried following a tutorial here but the actual dimensions in the build I run seem to be different from what I configure it to in my editor. Also, how do I get the coordinates of the middle of the square (to instantiate something)? Any help? Thanks!
In the tutorial you linked it says:
When running your game from within Unity's editor, be sure to have the Game >window open and visible in the editor when you run the game. There's >currently a bug in Unity 3.0 (and possibly in earlier versions as well) where >the window resolution reported to the script does not match the actual >resolution of the window inside the editor if the window isn't visible at the >time the play button is pressed, leading to a viewport with the wrong size.
Did you took notice to this?
Also what do you mean by taking the coordinates in the center of the square? If you mean the actual screen, then you should get a point based on the square dimensions like:
Vector2 point;
Rect rect = camera.rect;
point.x = rect.x/2;
point.y = rect.y/2;
But if you want the point in 3D space where the camera is pointing, you can use the default method ViewportToWorldPoint, as shown here: http://answers.unity3d.com/questions/189731/how-would-i-find-a-point-in-front-of-my-cam.html
It should look like this:
float distanceFromViewPort = 1 //change 1 to the distance you wish the instance to appear away from the camera.
Vector3 point = camera.ViewportToWorldPoint(Vector(0.5,0.5,distanceFromCamera));
if you check this thread started from me (Calculate the distance between the iPhone and a door, knowing their physical widths) I accepted an answer, that states, correctly, that if you do not know focal lens data of the camera of the iPhone, there is no easy way to calculate the distance between an iPhone and, let's say, a door, knowing its width.
I have to start another thread now asking:
I know the physical (not only in pixel) size of the screen of the iPhone (for iPhone 5 is 2.31 inches)
Also I know the width of a door.
Now, if I am in the position where the width of the door fits perfectly in the width of the iPhone itself (not of the camera), and the iPhone stands perfectly vertical, is it possible to know the distance between the iPhone and the door in that moment?
Thank you for your kind help!
I assume you mean that there is some outside image capturing device (be it a human eye or another camera) and the image capturing device, the phone, and the door are all in a line such that the phone appears to be the same width as the door.
In this case, you still need a) the distance between the image capturing device and the phone and b) the optical information (such as focal length) of the image capturing device. Just sit down with a pen and paper and draw out the geometry for a little bit and you'll see that.
This is going to involve a trigonometric calculation. I think you might have done R&D on Gyroscope, if not then surely you should refer it.
1) Find angle your phone is making with ground. Like when you point the device's camera to bottom of the object.
2) Now you are having one angle and you are making 90 degree with ground. So basically you are forming a right angled triangle. And you had just found one of your angle near your hand.
3) You can approximate distance of your phone from surface to your hand. So you got one side of triangle and one angle. Thus you can find second side i.e distance between you and object.
Hope this helps. :)
I’m trying to support both landscape and portrait orientations in my iPhone Cocos2D game, but I’m having trouble getting the coordinates to translate properly.
Here’s what I’m doing so far.
I have a GameWorld layer that I always keep in portrait, regardless of the device orientation. The following code is in my DeviceRotated event for UIDeviceOrientationLandscapeLeft. (‘self’ is my GameWorld layer)
[self runAction:[CCMoveTo actionWithDuration: 0.25f position:ccp(80, 0)]];
[self runAction:[CCRotateTo actionWithDuration:0.25f angle:90]];
So that I don’t have to write different code for each orientation I was hoping to use the following in my Sprite class to translate Sprite coordinates.
CGPoint spriteLoc = ccp(0,0);
CGPoint translatedSpriteLoc = [self.parent convertToNodeSpace:spriteLoc];
self.position = translatedSpriteLoc;
However, this doesn’t work.
If the device is in portrait mode with the sprite in the lower left corner and I rotate the device to the left, the sprite appears in the lower right. I want the sprite to be in the lower left in landscape just like it is in portrait.
Am I missing something or is there a better way to translate coordinates?
Well, if you don't mind a "jump cut" when you switch orientations, you can just use the built-in orientation support within Cocos2d. See this post at the Cocos2d forums.
If, however, you need pretty orientation, you may have to do something along the lines of what you were showing above, orienting things manually via rotation using actions.
Without more detail, it's hard to say why your approach doesn't work, but my guess is that you are seeing the sprite positioning you describe as a result of the fact that if you don't change orientation, the lower left in portrait IS the lower right in landscape when rotated left, i.e., it's the same point in GL space: (0,0). You're going to have to move the "origin point" of your GameWorld Layer as well as rotating it.
Try adding a full-screen image to your layer to see what's actually happening when rotating it. That should help you narrow down what you need to do.