Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
SpriteKit wrapper
-
@mikael whaaaa so a nice effect with a so little code. I'm jealous, super
-
Does anyone have any of the old skscene examples from the early pythonista1.6 betas. At one point I think I had copied them after the scenekit or whatever it was called module was abandoned by omz, but I'd have to dig around in the backups.
Also, iirc, there was a nice level editor that was supposedly pure python.Just thinking this might give some other thoughts on things to implement. I remember a clock with falling numbers, and maybe a breakout clone.
Anyway, this is some cool stuff @mikael.
-
-
If you search for old scripts, try https://github.com/tdamdouni/Pythonista
-
@JonB, oh man, yes, looking at some of the scripts in tdamdouni repo, they import an sk module which clearly has many or all the nodes defined. And there are pysk files which define game levels.
Now if we could just find the sources these refer to.
-
-
Yeah, that GitHub has a bunch of the other examples, including that clock I remember, and pysk files. The sk module was a c module.
-
-
In retrospect, the editor was not pure python, but the load_sk or whatever was. It is possible (haven't tried) that the pysk editor still works -- for many versions after the sk module was removed, when you opened a pysk, pythonista had a custom editor, very similar to opening a pyui. Iirc, it let you add backgrounds, by adding assets on a grid, and also let you add pins and specify gravity or physics... But it has been a while. Take a look at the pysk files which are just json.
-
Some more demos.
First, here is the script by @jbking that got me started on the SpriteKit path. Below you can see the same implemented in the wrapper, with every touch dropping a random rectangular or circular block. With couple of blocks placed on the platform at the start, this becomes a surprisingly addictive game of "can I clear the platform".
Technically, the interesting bit here is how easy it is to make the scene work with rotation by just placing the anchor point in an appropriate place - in this case at (0,5,0), equal to the center of the bottom edge.
Another oldie but goodie is dropping random doodles:
Making this happen lead to surprisingly many challenges with using paths as physics bodies:
- SpriteKit has a method that creates a physics body from a path, but this method is very finicky, requiring the path is a counterclockwise and convex, which is rarely what you have at hand.
- Thus I included a method to create a convex hull surrounding the points of the path.
- SpriteKit also has a method for creating a smooth spline over a set of points, but no way to continue the path thus created, and the famously "opaque" CGPath is quite hard to inspect & manipulate in Python.
- So I included a bit kludgy methods that get the points out of a ui.Path, and the Bézier definitions out of the spline created by SpriteKit.
- With these, the API feels mostly natural, and you can provide either a ui.Path or a set of points to create the ShapeNode, and choose to have smoothing applied or not.
- Convex hull is not a 100% solution for paths, as concave spots or arcs curving out between two points are not accurately represented.
- Thus there is an option to use a texture-based body instead of the convex hull. According to Apple, this is the least performant option, but probably ok in most cases, so I made it the default.
- In the doodle demo, above, every other doodled object uses a hull and every other a texture as the physics body, and I find it hard to tell which is which.
-
-
Time for the space race!
Features tested:
- Animation Actions - Many but not all of the multitude of actions have been enabled, with a little addition of Python syntax - you can choose to use a
set
instead of Action.group, and alist
instead of Action.sequence, like in this "pulsing" example for the race course buoys:
A = Action node.run_action( A.forever([ A.scale_to(1.2, timing=A.EASE_IN_OUT), A.scale_to(1.0, timing=A.EASE_IN_OUT), ]) )
- EmitterNode can be used for particle effects like the ship's thrust.
- Background tiling is a custom feature of the CameraNode - you provide a tile and a factor for relative movement, like here:
self.camera.layers.append(Layer( Texture('background.png'), pan_factor=0.2, alpha=0.4))
The layers in the
layers
list are ordered from closest to furthest away.- EffectNode is used for the background performance, to cache all the individual tiles as one bitmap.
Also planning to use this as a testbed for various field effects, inverse kinematics and remote play.
- Animation Actions - Many but not all of the multitude of actions have been enabled, with a little addition of Python syntax - you can choose to use a
-
Introducing Vertigo Vortex! Coming to you courtesy of "little" help from @JonB.
This demonstrates both visual warping and a vortex field effect.
You can read more about warping in Apple docs. Here I included a custom spiral warping effect, which resembles some of the shader effects except it is much nicer to code in Python than in the shader quasi-C.
Applying the effect:
spritenode.warp = WarpGrid(21,21).set_spiral(-math.pi*2)
Parameter of
set_spiral
tells how much the points on the outer circle move counter-clockwise; in the example case, full 360 degrees clockwise. Spiral effect then happens when points closer to the center are rotated less.Sequences of warps can be animated, e.g. like this:
warps = [ WarpGrid(21,21).set_spiral(-math.pi/1.25/10*i) for i in range(11) ] spritenode.run_action(Action.warps(warps, duration=3.0))
About the magic numbers 21,21 in the grid initialization - I do not know how detailed the grid must be, but I expect some detail is needed for most effects, as different grid sizes produce very different results. You can see the difference below, where all the images except the origonal apply the same 90-degree counter-clockwise effect, only with different level of granularity:
With the vortex physics field effect, I had to cheat, since I could not get the SpriteKit vortex field to work.
With field strength 0.003, the field was still throwing the ship around much too hard, yet with strength 0.002 the field turned off completely.
So instead of fighting this, I opted to manually apply a tangential force in the
update
. Works fine here, but could be a performance topic if you need more complex things. -
Did you play with the falloff parameter of the vortex? The field strength falls off as
pow(distance - minRadius, -falloff)So depending on what your scaling parameters are, you might try a smaller falloff, and maybe a larger minRadius. I think the default falloff is 2, meaning as your distance doubles, the strength falls off by 4x. Looks like you used 100 meaning when the distance doubles, the strength falls off by 2**100! Your cheat effectively used falloff of 0. I bet if you crank the strength back up, and set falloff to 0, or something in the .5-2 range, it will work as expected.
-
@JonB, thanks for checking the code. Falloff 100 was just last of a series of trying different values, but even with falloff 0, the result I see is the same: strength 0.002 looks like no field at all, strength 0.003 is completely overpowered.
I thought I might wait and see if iOS 13 brings any change to this.
-
Do you set the mass of your space ship? Also, have you played with region /minimum radius?
-
This is becoming even more awesome ! Congrats @mikael for those wonderful spiral effects ! Do you think you should be able to make a 2D light system ?
Cordially,
SmartGoat -
@mikael I think you wanted a radial_gravity field instead of vortex. At least that’s what you simulated. Vortex forces are along the tangent direction, instead of radial. What you might want is a co-located vortex and radial gravity, to create a sort of whirlpool.
The vortex_field.node.minimumRadius also prevents the field from blowing up at the center if you use a positive falloff.
-
class LightNode(Node): enabled = node_relay('enabled') ambient_color = node_relay('ambientColor') light_color = node_relay('lightColor') shadow_color = node_relay('shadowColor') falloff = node_relay('falloff') category_bitmask = node_relay('category_bitmask') def __init__(self, **kwargs): self.node = SKLightNode.node() super().__init__(**kwargs)
And following added to SpriteNode def
lighting_bitmask = node_relay('lightingBitMask') shadowed_bitmask = node_relay('shadowedBitMask') shadow_cast_bitmask = node_relay('shadowCastBitMask')
Which allows one to do
ship.lighting_bitmask=1 ship.shadow_cast_bitmask=1 L=LightNode(parent=scene, position=(270,500)) L.falloff=2 L.category_bitmask=1 L.z_position=100 L.ambient_color=UIColor.redColor() texture=SKTexture.textureVectorNoiseWithSmoothness_size_(.5,CGSize(1400,1400)) n=texture.textureByGeneratingNormalMap() bg=SKSpriteNode.spriteNodeWithTexture_normalMap_(texture,n) bg.lightingBitMask=1 bg.shadowedBitMask=1 scene.node.addChild_(bg) bg.zPosition=-10
-
- I have set range, which seems to work fine
- I have not set minimum distance, as the default is a very small value, and not an issue yet
- I tried playing with density, but it does not have an impact as vortex sets the velocity regardless of mass
- I am sure I want the tangential ”flinging away” vortex field instead of the radialGravity (which would be more like the gravity of a black hole, planning on adding that later)