Home | Tutorials | Articles | Categories | Channel list | SDK | Exporting | F.A.Q.

WARNING: This is archived content.

This page contains information about Quest3D 4 and earlier versions. If you want documentation about Quest3D 5.0 or higher go to http://documentation.quest3d.com

Chapter 3.2: Math

From Quest3D

Jump to: navigation, search

Contents

3.2 Math

Values are at the heart of any Quest3D project. They are used to define an object’s color, position and size for example. Values can also describe scores, program settings, menu option etc.

Values in a Quest3D project often change. Regular math can be used to calculate new values.

Set Value

The following statement describes a standard expression in regular math.

Old value = New value

In Quest3D, the above statement must be translated to the following.

Copy New value into Old value

To copy one value into one another, the Set Value channel is used.

Image:Example.158a.jpg Image:Example.158b.jpg

In most practical cases Set Value channels need to be carried out only once. Therefore, they are often called by Trigger channels.

Expression Value

Mathematical expressions can be described in Quest3D using the Expression Value channel. It serves as an advanced calculator. Its properties window contains a box for the actual formula, and help text describing all the usable variables and operators. The Reference Manual explains each of these elements in greater detail.

Image:Example.160a.jpg

The actual value of an Expression Value is equal to the result of the expression inside of the channel. This result is also displayed under ‘Current Value’ in the properties window.

Image:Example.161a.jpg

Brackets ( ) can be used to define priority of one part of the expression over the other. The two expressions in the image below are not the same and therefore do not produce the same result.

Image:Example.162a.jpg Image:Example.161b.jpg

Values connected to the child links of the Expression Value can be used as part of the expression within the channel as well. The first child is presented by the letter ‘A’, the second by the letter ‘B’, and so forth.

Image:Example.163a.jpg

The Expression Value channel can be named after its expression by choosing the ‘Rename Channel to formula’ option in the properties window.

Image:Example.164a.jpg

The following example demonstrates an often used combination of a Set Value, Expression Value and Value channel.

In the example below, every time the spacebar is pressed, ‘1’ is added to the ‘Value’ channel.

Image:Example.165a.jpg

Expression Values can also serve as condition input for If and Trigger channels. In the image below, the structure should be read as ‘if A equals B then execute the following channels’.

Image:Example.166a.jpg Image:Example.165b.jpg

In the lower part of image, the condition ‘A == B’ is met.

Multiple expressions can be linked to oneanother within the same channel using the logical operators AND ( && ) and OR ( || ).

Image:Example.167a.jpg

In the image above, the condition is met if ‘A==1 && B==2’. The expression should be read as, ‘if A equals one and B equals two’.

Expressions can also contain conditions in the form of If Else statements. In the image below, the expression ‘A==B ? C : D’ should be read as ‘if A equals B then the result is C else the result is D’.

Image:Example.168a.jpg Image:Example.167b.jpg

In the lower part of the image, the condition ‘A==B’ is met. The result of the expression is the value of ‘C’.

Envelopes

Besides playing an important role in animation, Envelopes have other uses as well.

A graph in an Envelope channel is a two dimensional mathematical function. For advanced simulations and models, actual formula’s may be used by inserting a number of coordinate pairs into the graph. Quest3D will handle the interpolation between these points.

Image:Example.169a.jpg

The envelope graph in the image above shows the formula ‘y = x²’.

One quantity can be converted to another by means of an Envelope. Put differently, an Envelope describes the relation between two values.

Image:Example.170a.jpg

Character motionset blending may be handled based on walking speed. The graph in the image above scales speed input values between ‘0’ and ‘5.0’ to blending output values between ‘0’ and ‘1.0’. The graph is non-linear.

Envelopes can also be used to limit the range of a value.

Image:Example.171a.jpg

The graph in the image above limits input values to output values between ‘-1’ and ‘1’.

Tutorial

Math is an important aspect of many Quest3D programs. This tutorial is an exercise in working with the following channels: Value, Set Value, Expression Value and Envelope.

Needed Quest3D scene:

  • ..\Tutorials\3.2 – Math\Math.cgr

Needed templates:

  • Variables \ Value \ Set Value
  • Variables \ Value \ Expression Value
  • Variables \ Value \ Value
  • Variables \ Value \ Envelope
  • Logic \ User Input \ Mouse \ Position \ Mouse Screen X

Step by step:

  • Open the scene, ‘Math.cgr’. It contains a simple logic structure.
  • Drag a Set Value channel onto the Channel Graph and connect it to the second child link of the Trigger.
  • Add a Value channel and connect it to the first child link of the ‘Set Value’ channel.
  • Change the value of this Value channel to ‘1’.
  • Drag another Value channel onto the Channel Graph and connect it to the second child link of the ‘Set Value’ channel. Rename this Value to ‘Current Value’.

Image:Example.172a.jpg

  • Switch to Run Mode.
  • Press the spacebar. Note the ‘Set Value’ channel is triggered once. Also note the value of the first Value channel is copied into the second Value channel. Both are now ‘1’.
  • Switch to Edit Mode.
  • Add an Expression Value channel and connect it to the ‘Calculations’ channel.
  • Double click on the ‘Expression Value’ channel to open its properties window.
  • In the ‘Expression’ field, enter ’1’. Note the ‘Current Value’ now equals ‘1’. Also, the value of the ‘Expression Value’ channel is ‘1’. (link the expression value to a channel caller so it gets executed)

Image:Example.173a.jpg

Press ‘OK’ to accept.

  • Drag a Value channel onto the Channel Graph and change its value to ‘3’. Connect it to the ‘Expression Value’ channel.
  • Double click on the ‘Expression Value’ channel to open its properties window.
  • In the ‘Expression’ field, enter ‘A’, referring to the Value attached as a child. Note that the ‘Current Value’ now equals ‘3’. Also, the value of the ‘Expression Value’ channel is ‘3’.

Image:Example.174a.jpg

  • Select and delete the Value channel connected to the first child link of the ‘Set Value’ channel.
  • Add another Expression Value channel and connect it to the first child link of the ‘Set Value’ channel.
  • Connect the ‘Current Value’ channel to this ‘Expression Value’ channel.
  • Double click on the ‘Expression Value’ channel to open its properties window. In the ‘Expression’ field, type in the following formula: ‘A + 1’.
  • Select the ‘Rename Channel to formula’ option and press ‘OK’ to accept.

Image:Example.175a.jpg

  • Switch to Run Mode.
  • Press the spacebar. Note the value of the ‘Current Value’ channel is increased by one (‘1 + 1 = 2’)
  • Press the spacebar again. Note the value is increased again (‘2 + 1 = 3’).
  • Switch to Edit Mode.
  • Change the formula in the Expression Value to ‘A < 4 ? A + 1 : 0’. Note that this formula should be read as ‘if A is smaller than four, the result is A plus one, else the result is zero’.
  • Switch to Run Mode.
  • Press the spacebar. Note the ‘Current Value’ is now 4.
  • Press the spacebar again. Note the ‘Current Value’ is now 0.
  • Add a User Input channel add connect it to the ‘Calculations’ channel.
  • Double click on this ‘User Input’ channel to open its properties window. From the first drop-down list, select ‘Mouse Information. From the second drop-down list, select ‘Mouse Screen Pos X’. Press ‘OK’ to accept.

Image:Example.176a.jpg

  • Switch to Run Mode.
  • Move the mouse from left to right. Note the value of the ‘User Input’ channel changes. The maximum value depends on the resolution of your monitor.
  • Switch to Edit Mode.
  • Select and delete the link between the ‘User Input’ and the ‘Calculations’ channel.
  • Drag an Envelope channel onto the Channel Graph and connect it to the ‘Calculations’ channel.
  • Connect the ‘User Input’ channel to the ‘Envelope’ channel.
  • Double click on the ‘Envelope’ channel to open its properties window. Create keys at (0, 0) and (500, 1). Click on the ‘All’ button to view the entire graph.

Image:Example.176b.jpg

  • Press ‘OK’ to accept.
  • Switch to Run Mode.
  • Move the mouse from left to right. Note the value of the ‘Envelope’ channel changes between ‘0’ and ‘1.0’.
  • Switch to Edit Mode.

Finished scene:

  • ..\Tutorials\3.2 – Math\Math – Complete.cgr

Back to Main Index

Personal tools
Navigation