SpriteKit: physics body "alpha" shape issues - sprite-kit

I am working with my son on a basic game based on the book Dive Into SpriteKi by Paul Hudson.
Following the book, we created a sprite for a space ship and a space junk satellite. It works, but we notice that sometimes ship goes through the junk. We tried to add sprites with the scene editor and turned on the physics:
Remarkably the ship node added using scene editor have completely wrong physical body outline generated, but doing
player.physicsBody = SKPhysicsBody(texture: player.texture!, size: player.size)
does fix it. On the other hand, doing the same for the junk satellite sprite always produce the same convoluted physical body.
My only guess is there is something wrong with how (or when) the texture loaded and analyzed. It would explain why doing it repeatedly fixes the rocket ship... but what is wrong with the satellite? And how to make sure texture is properly prepared so that alpha mask for physics body works reliably?
I will appreciate any advice on how to deal with this issue (-s).
I tried to use alphaThreshold: and it makes some difference for the ship outline, but seems to have no effect for the satellite outline.
I tried to re-draw the satellite texture from scratch to rule-out an issue with original file from the book. With a completely new file of similar shape the issue is exactly the same.
We also tested iOS 12 and iOS 13 with the same results. The "canonical" code from the book have the same exactly issue.
Please see our game complete implementation on GitHub.
A fast way to reproduce the issue out of our game:
Create new project in Xcode with Game template
Add the satellite asset to the assets catalog:
Add Color Sprite to the GameScene.sks, choose satellite for texture, and set body type to "Alpha mask" in Physics definition section.
The outcome in our case looks like this:
Expected outcome is an accurate outline of the sprite.

Related

Using Minecraft character with skin into a unity game

I've decided to use Minecraft like characters in my small game since I do not know how to make 3d models (nor I want to learn how to do such thing in the near future).
However the task now seem a little harder than expected:
I've tried looking in the asset store for prefabs to use but without success.
So, I've decided to try and make a model on blender(by not knowing a thing about non parametric 3d modeling, my knowledge of blender is extremely limited) and import it into my unity game.
And surprisingly, I managed to create the model using McPrep, export it and import it into unity maintaining objects that drive the bones (the output is a bit messy but I think I can manage to clean it up a little).
However the imported version does not have any skin and appears in a gray shade.
Turns out that the output does not keep materials/textures with it!
I've tried getting the texture used by blender and it returns the same skin I fed into mcprep so, by using the same skin, I've tried creating a material with it by getting the .png and using it as texture in a unlit texture material.
However, the result is a bit messy as shown here (left is Blender, right is Unity):
How may I make the texture on unity3d be better and right? (I've heard there is a way using a C# script but I really don't know what it is nor how to do it)
EDIT:
Thanks for the answers before, I did set the filter to point obtaining the texture to be a bit better. However the part that should be transparent is displayed in black on top of the other part (I think).
The image on the right is only filter point and the one on the left is point + alpha is transparency and the transparent shader using unlit transparent
ANSWER FOUND:
As Bart said, make sure the textures' Filter Mode is set to Point, but additionally:
Minecraft player characters are made of 2 layers, the second layer usually has lots of transparency and is used for clothing or other relief detail. So you need to use a transparency-capable shader on your material in Unity.
You're running into a filtering issue. In your case you want no filtering to happen. So select your texture, and in the inspector change the import settings so that your "Filter Mode" is set to "Point". In this case it will do no filtering of the input and your large pixels should appear sharp as you want.

swift physicsbody is not matching up to image size

I have two nodes, player and platfrom. Both have a physicsBody around them. My player is still and the platform scroll by an SkAction. when the player reaches about half-way on top of the platform it falls threw it.
player.physicsBody=SKPhysicsBody(rectangleOf:player.frame.size)
player.physicsBody?.isDynamic=true
player.physicsBody?.allowsRotation=false
player.physicsBody?.affectedByGravity=true
platform.physicsBody=SKPhysicsBody(rectangleOf:platfrom.frame.size)
platform.physicsBody?.isDynamic=false
the platfrom set up is.
plat.size=CGSize(width:(self.frame.width)*2,height:(self.frame.height)/3)
plat.anchorPoint=CGPoint(x:0,y:0)
plat.position=CGPoint(x:0,y:0)
plat.zPosition = 2
addChild(platform)
I tried using this instead but player still falls down
plat.physicsBody=SKPhysicsBody(rectangleOf:CGSize(width:platform.width,
height: platform.frame.height))
I want the nodes to act like a solid object, in that they don't overlap each other when the player moves. Also it there an alternative method I can use. I don't really like working with physics.
An alternate to working with physics is to use the recently added to iOS 10 SKTilemapNode to create the background. Each tile can be tagged with data to indicate if the tile is ground or not. You then focus on how the player moves relative to the tiles. There are currently not a lot of examples to show this working, the WWDC 2016: What's new in SpriteKit shows this briefly. Other resources include a top down example which shows at least how to build the background tilemap and some background info on why physics engines don't provide the best experience for platformers. Kenney's is a good source of tiles for platformer artwork.

How can I achieve a node that will desaturate everything below it?

I am building a game where everything behind the player is greyed out as if it lives in a memory. But I don't know how I can achieve this effect. Is this where shaders are being used for?
Now I can create a SKLightNode to create the lighting and make it dark around the edges. But I like to field of view for the character to be 120 degrees. Everything outside of that angle should be greyed out.
Of course in the future I like the view to be blocked by obstacles but that is outside of the scope of this question.
A desaturation shader for SpriteKit can be found at my blog post on that subject. Note that this works in terms of an input texture, so you may need to adapt things to work on top of a tiled background. Also note that there is a new iOS 9 API to support capture of the output of a whole node, which may be useful to you in implementing this.

Unity3D: Can I layer scenes or can I mask scene elements to prevent overlap?

I have a work-in-progress spaceship interior built with Unity (utilizing pre-built assets). It won't move much. The space around it will, similar to how I made an HTML/Three.js version of this concept (although linked example does move ship around space scene while scales are also animated).
With the three.js version, I was constantly fighting with "space" entering the ship; warp beams and planets colliding with the front info screen or passing through the chair (destroying the illusion of massive scale). I was able to resolve most issues, but it was a challenge when everything was in the same scene.
In Unity, is there a way to keep the ship scene separate from the surrounding space "scene", whether it is part of the same scene or in a separate one on another layer, or the same scene with masks that prevent overlap? I don't need a very literal answer, but any guidance that leads to a solution will be promptly accepted.
Yes, it's a very basic and useful aspect of Unity - the layers system.
As you guess you can find endless doco about it at Unity, cheers
Note too - Unity of course has superb occlusion culling built-in. Almost always this will "just work" for you.
Note that (if I read you right) you're doing a scene with a tremendous difference in scales (like "10 cm" objects AND "1000 km" objects at the same time). As you probably know you basically can't do this in a game engine, it's tricky to pull off.
Just BTW this is more a question for gamedev.com

Textures to Object in Unity / Vuforia

I'm trying to do some Augmented Reality in Unity using the Vuforia plugin. I've managed to get everything working (I'm using a 3d model of a car), only the position of the car relative to the marker is wrong when I come to preview it (click the 'Play button at the top and watch through the computer's camera - is 'preview' the right name for this?).
It should be sitting on the the marker in the center, but instead it's floating above the marker and off to the side by quite a bit. The positioning is definitely right in the program itself, so I'm not sure why this isn't reflected in the preview.
Also: at the moment the car is simply an untextured grey object. I realise the textures are included in a subfolder, however I can't drag the entire car's folder (including the Textures subfolder) into the ImageTarget, only the .3ds file itself. Does anyone know how to apply the original textures to the car?
Thanks so much for your help in advance!
Ben
Well, to start answering your question about the drifting away part, you've probably got a Rigidbody attached to your Car? Well, if that's the case, then Go to the Inspector where the Rigidbody is and you'll see constraints.
To avoid it floating off, you might want to check Freeze Positions for whichever direction it needs. Probably Y axis.
Now, I'm not entirely sure if you're using Vector.AddForce to move your objects, but if you are then just check if your car can move if you've put all constraints on.
If you can't then in your code trying using Object.Rigidbody.SetActive(true) when you need it to start moving, and Object.Rigidbody.SetActive(false) when its done.
On the other hand, if you aren't using Vector.AddForce() then remove Rigidbody component if you've used it.
As for the material. If you've got the material in your folder that you've downloaded then just drag and drop it into Assets. Now sometimes your car may be one solid mesh, but some times it may be multiple meshes. So which ever the case. Drag the material from your Assets onto the Hierarchy panel over your car mesh/meshes and it should turn into that color.
Hope it helps. :)