On the WWDC2014, the Sprite kit has a lot of new improvements!
One of the most interesting things about physics field! That's the physics field!
This means that our game of writing virtual physics on the Sprite kit will become very easy!
Here I'm based on the demo on WWDC2014 's official video on what's new in the Sprite kit,
I made a demo with Swift language.
For example, the following:
Put the GitHub link first:
Https://github.com/songrotek/PhysicsFieldsWorld.git
I also recorded the effect of the video you can first see as fast:
Http://v.youku.com/v_show/id_XNzI2MzUzNzYw.html
Here's a look at physics field!
1. Overall situation
The physics of the Sprite kit Physics field One has a common 10:
Enum fieldtype{
Case Lineargravityfield//Linear gravity field
Case Radialgravityfield//radiation gravity field
Case Dragfield//Resistance field
Case Vortexfield//Eddy Current Field
Case Velocityfield//velocity field
Case Noisefield//noise field
Case Turbulencefield//disorder field
Case SpringField//Spring field
Case Electricfield//electronics field
Case Magneticfield//magnetic field
}
2. General parameters of the field
Then all types of fields are created and set in the Skfieldnode Class!
Regardless of the type of field, there are a number of common set of parameters. For example, the following
field, the object beyond the area is not affected by the field, the default is unlimited
var region: skregion!
Field strength, default is 1
var strength: cfloat
The attenuation exponent of a field used to calculate the force magnitude of a distance, starting from the minimum radius. Default is 0
var falloff: cfloat
The minimum impact radius of the field, which is a very small number by default
var minimumradius: cfloat
Assuming enabled, the farm works by default is true (seemingly not so well under swift, the beta issue is expected
var enabled: Bool
The exclusivity of the field, assuming that it is true, the other field cannot function in the region of this field. Assuming that the impact range of two fields is coincident, you cannot infer which field action
var exclusive: Bool
The Action object category of the field, default is all
@see Skphysicsbody.fieldbitmask
@see Skemitternode.fieldbitmask
var categorybitmask: UInt32
3. Creation of the Farm
For example, create a spring field:
Let SpringField = skfieldnode. SpringField ()
SpringField. Strength = 0.05
SpringField. falloff =-5
SpringField. position = Fieldcenter
SpringField. name = "SpringField"
AddChild (SpringField)
The same way as other fields, that is, the setting of the parameters must be in detail.
4. Every kind of field!
4.1 Linear field linear Gravity field
is in accordance with Newton's second law F=ma
It can be used to create gravity in all directions, of course, with physics world gravity can also be set, but this field can set the range.
4.2 Radiation Gravity Field Radial Gravity fields
To simulate the moon going around the earth, it has to use this, the gravitational field at one point.
4.3 Resistance field Drag field
In this field, like an object, the friction slows down and the friction depends on the speed of the object.
4.4 Eddy Current Field Vortex field
This field can be used to make a tornado effect, the field of force around the center, assuming that the strength is just counterclockwise, assuming that the negative is clockwise
4.5 Noise field Noise field and disturbance field turbulence field
The noise field is the force of each position at random, there is a smoothness parameter to adjust the noise force smoothness degree
The noise field is very close to the disturbance, but the effect on the object is small.
4.6 Spring Field
A spring that connects the object to the center of the field and moves back and forth around the center.
4.7 Speed Field
You need to add a sktexture texture that determines the direction of the force for each field location point.
The effect is to get the velocity field of a river, so that an object can move along the river as it is placed in the river.
4.8 electric field electric field and magnetic field magnetic field
is Lorenz Force f=q (E * VB), in which QE is the electric field Force, QVB is the magnetic force
In order to simulate the electric field and the magnetic field, the physicsbody of the Sprite kit is added charge which is the charged amount.
But for now, in my personal experiment, there is no simulation of electromagnetic fields under Swift, wait for Apple to be intact.
5 Summary
Physics field is a very cool feature that is worth using, very convenient!
Note: This article is original article, if need reprint, please indicate source, from Songrotek's blog "