Phenomal Physics - Springs and Things [Science Fair Build Jam]

What is it?

This is a package to allow you to build and play with forces between just about anything using the new primitives in Crayta (or good old meshes / voxels meshes if you prefer) and apply physics rules on them and between them in the form of connectors. You can apply these properties in the editor or at run time enabling you to have pre-built structures or to build things ‘on the fly’ during the game.

Why not use Crayta Physics?

Crayta physics is very powerful but sometimes because of its complexity it produces results that can be unpredictable. This package allows you to create more predictable forces between specific objects which may be all you need in many circumstances. Also (if you are brave) you can still enable physics on the entities for more realistic collision results etc (I do not recommend enabling physics / collision for connectors though).

What are the building blocks?

Core concepts are Nodes and Connectors.

Nodes - can be any entity in Crayta (voxel meshes, meshes, shapes, locators or templates including collections of these) but you might want to start with the shape primitives.

Connectors - can also be any entity but the ones that make the most sense are probably those that scale nicely in one axis. In particular the new cylinder shape primitive works well for this but you could use a laser beam or a neon line.

Controller - the controller is usually a locator with the NodeController Script attached. It is used as a ‘control centre’ as the name suggests. It is used for multiple purposes but can be used to set the default value for nodes and connectors that are children (or grandchildren) of the connector or which point to the connector from their properties. It can also be used to spawn nodes and connectors either at the start or on a schedule or during the course of the game as part of an interaction (for example).

Essentials to get it working:

I highly recommend you have a look at and play around with the Phenomenal Physics Showcase Blueprint before trying to build things from scratch. It will hopefully give you some good ideas of what you can do. The package is just one of the exhibits from there - the blueprint is the core piece.

From Scratch:

Install the Phenomenal Physics - Springs and Things package from community packages

Drag ‘General Exhibit Template’ from the templates under the terrain object in your world. This contains a controller and some nodes.

Start the game preview (F5 if you are using a keyboard).

Notice how some of the nodes are already connected. This is done via the editor properties ‘nodesToConnectOnInit’ array’

Note: If you get any issues then look out for items in red - it is probably just that the NodeMain Script and NodeInteract Script need telling where to find the controller template

If there is a problem in any of the templates of entities it will look like this:

Just click on the nodeController property of NodeMainScript and point it at the World–>Terrain–>GeneralExhibitTemplate
Screenshot 2022-10-17 at 10.43.40

Likewise for the NodeInteractScript you may need to set the entity in the binding to the same as above

You can click on a node and then click on another node and this will create a connector between the two nodes. This is done using the NodeInteractionScript which in turn calls scripts on the NodeController. In the showcase game we have used the inventory to allow you to select different connection types (check it out!)

Node Entities

Nodes should have the ‘NodeMainScript’ attached an can optionally have the ‘NodeInteractionScript’ attached also.

MainNodeScript Editor Properties:

Property Group: pre-connected nodes

nodesToConnectOnInit - Entity Array - Table of other nodes (entities with MainNodeScript attached) to connect to

joinTypes - String Array - Table of join types to apply when connecting to the nodes listed in the nodesToConnectOnInit property

springConstants - Number Array - Table of spring constants (k value in Hooke’s law) to apply when connecting to the nodes listed in the nodesToConnectOnInit property [only works with springs]

springDampers - Number Array - Table of spring dampers (sets energy loss in spring motion) to apply when connecting to the nodes listed in the nodesToConnectOnInit property [only works with springs]

springRestLengths - Number Array - Table of rest lengths (used to determine the ‘extension’ in Hooke’s law) to apply when connecting to the nodes listed in the nodesToConnectOnInit property [only works with springs]

Property Group: node parameters

fixedPosition - Boolean - If true then this node will be unaffected by the forces applied by connectors (effectively it will have infinite mass)

overrideControllerDefaults - Boolean - If true then the values below will be used instead of the defaults from the controller (either a parent or provided in the editor property below). [Note: For now all the properties are either used or not, so if you override then you’ll have to set all the values]

gravityForce - Vector - This is a gravityForce used to override the default set in the controller. Typical force would be a negative z (e.g. 0.0, 0.0, -10.0) but you could equally have a upwards force with a positive z or have a constant wind blowing which might imply a non-zero x or y value. [Note: This is applied to the node regardless of whether it has any connectors]

speedAdjust - Number - This increases the force by a specific multiplier. If you are finding things a little slow you can increase this number.

maxForce - Number - This limits the max force that can be applied in any frame update. Might be useful to avoid sudden huge forces.

damper - Number - This acts as a resistance (e.g air resistance / other energy loss) to velocity and will act on each frame update to reduce the overall velocity. This is separate from the spring damper and will act no matter the connector type

mass - Number - Used in force to velocity calculations

nodeController - Entity - optional - If you don’t set this then the node will ‘climb’ up parent entities until it finds one with the ControllerScript attached and use that. You could use a completely separate controller for a group of nodes / connectors to provide a group of nodes / connectors that follow different rules or you can use the overrideControllerDefaults property to do this on a ‘per node’ basis.

Debug - Boolean - If set to true then the print statements in the code will be activated which can be useful for debugging. It may be quite ‘verbose’ though and may slow down performance due to a lot of output to the console.

Connector Entities

Connector entities will typically be cylinders or other shapes / meshes that can stretch nicely

Connector ForceScipt Functionality

This is where the connector exerts force on the nodes at either end

Connector JoinScipt Functionality

This is called to join two nodes together

Controller Entities

This is a controller that has default properties for the nodes and controllers that point to it. It is also used to spawn nodes and connectors.

Future Plans

  • [Done - check showcase] Toolkit of different connector types that can be applied to nodes / connectors
  • [Done - check showcase] Ability to ‘cut’ ropes at a specific point
  • Allow option to run everything (except spawn and attach routines) on client side
  • Option to show forces on the nodes (for debugging / education purposes)
  • Provide other connection types and other interaction types (flexible, rigid, gravity, magnets)
  • Test at large scale and optimise
  • Add more to the package - laser, neon connectors are simple to build

Known Issues

  • Ropes do work if you use the right parameters but I didn’t get a chance to copy across to the final showcase - I’ll make a separate package showing this.
  • Switching Crayta in built physics on for nodes is a bit ‘touch and go’ - this is pretty normal for Crayta physics though!
  • It may take quite a bit of ‘tweaking’ to find the combination of parameters that give a pleasing result
  • The way I’m applying forces to each node is to have a table of forces in each node that the connectors joined to it can add to. It then applies all these forces at once at frame update. I can anticipate this might create additional load if a lot of connectors are attached to nodes and the frame rate drops