> For the complete documentation index, see [llms.txt](https://smartui-docs.pixelperfectstudio.mx/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://smartui-docs.pixelperfectstudio.mx/miscellaneous-addons/parabolic-tween/getting-started.md).

# Getting Started

Calculating and Drawing a Parabolic Trajectory Based on Initial Velocity

In this tutorial, we will walk you through the process of calculating a **parabolic trajectory** based on an **initial velocity** and **launch angle**, and drawing this trajectory before the object is launched. We'll use **Construct 3** and the **ParabolicTween plugin** by Pixel Perfect Studio.

By the end of this tutorial, you'll have a system that calculates the trajectory of an object (like a bird or projectile) based on user input, and visualizes the path before the object is actually launched.

{% hint style="success" %}
The **ParabolicTween** plugin is capable of the following:

1. **Calculating parabolic trajectories** <mark style="color:green;">**based on initial velocity and angle**</mark>. This can be combined with the Physics behavior to move the object.
2. **Calculating parabolic trajectories** <mark style="color:green;">**based on a 2D destination (X, Y).**</mark> This can also be combined with the Physics behavior to move the object.
3. **Moving an object along a parabolic trajectory** within a specific period of time by tweening the object. The <mark style="color:green;">**movement is kinematic**</mark>, meaning any physics interactions are ignored.

For more details, refer to the [ACE documentation](/miscellaneous-addons/parabolic-tween.md).
{% endhint %}

#### Prerequisites:

* [**ParabolicTween Plugin**](https://store.pixelperfectstudio.mx/product/parabolic-tween-addon-for-construct-3/): A plugin by Pixel Perfect Studio that allows you to simulate parabolic motion with customizable gravity and velocity.

## Step 1:​ Layout Set Up

Add the following objects to your layout:

<figure><img src="https://3242409510-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9bZkqK9vYvPkvVDUO4Tp%2Fuploads%2F9bMXaSvvd5pNrSKLFOEn%2Fimage.png?alt=media&amp;token=08bd2a28-3d3d-449e-b055-1fd9d1bbbdfa" alt=""><figcaption></figcaption></figure>

1. Add a Sprite object that will be our projectile. We are using an object called ***Bird***. <img src="https://3242409510-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9bZkqK9vYvPkvVDUO4Tp%2Fuploads%2FiiRszdoi2J1VP0wlDLB0%2Fg15.png?alt=media&amp;token=49793553-1343-4a8d-b389-d57e67e1dbc3" alt="" data-size="line">
   1. Add the **`Physics`** behavior to the Bird.
   2. Add the **`Parabolic Tween`** behavior to the Bird.

<figure><img src="https://3242409510-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9bZkqK9vYvPkvVDUO4Tp%2Fuploads%2FBXJNNB4ac9Su2Rf621ak%2Fimage.png?alt=media&amp;token=35418004-b52a-4a41-b16b-0b88a584ec08" alt=""><figcaption></figcaption></figure>

2. Add a Sprite object called ***Path.*** We will use it to draw a point in time where the projectile will be when launched. <img src="https://3242409510-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9bZkqK9vYvPkvVDUO4Tp%2Fuploads%2FT7G5mxRln2FXXaVbV9UW%2Fpath25.png?alt=media&amp;token=eec8292b-f36f-4139-a4fa-03b17e0ad236" alt="" data-size="line">
3. Add a Slider and name it ***Initial Velocity***
4. Add a Slider and name it ***Launch Angle***
5. Add a **`Text`** object, and copy it over each of our Sliders to identify them.
6. Add a **`Button`** and name it ***Throw***. When pressing it, the Bird will be thrown at the velocity and angle set in the scrollbars.

<figure><img src="https://3242409510-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9bZkqK9vYvPkvVDUO4Tp%2Fuploads%2F3PEDeQlgvKBAgBkEhUnV%2Fimage.png?alt=media&amp;token=ed59bf38-7d37-4a79-b47e-e6c53eac6680" alt=""><figcaption></figcaption></figure>

Your scene object list should look like this:

<figure><img src="https://3242409510-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9bZkqK9vYvPkvVDUO4Tp%2Fuploads%2FWEVZknrYSlsk2gjh5SNf%2Fimage.png?alt=media&amp;token=6c4d9bca-155e-4f12-a5ab-0937e8fd5be2" alt=""><figcaption></figcaption></figure>

## Step 2: Configure slider's value, and the Bird physics behavior

1. Configure the Bird´s Physics
   1. Disable the Bird´s physics behavior.&#x20;
   2. Set the immovable flag to false.

<figure><img src="https://3242409510-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9bZkqK9vYvPkvVDUO4Tp%2Fuploads%2Fq6Zxuanx1JqU2lMs2OvJ%2Fimage.png?alt=media&amp;token=0eeeca7a-eac5-4974-b5c5-8ec335ea9bda" alt="" width="230"><figcaption><p>Bird´s attributes. </p></figcaption></figure>

{% hint style="warning" %}
The angle of gravity can be changed for the calculated parabola. Nevertheless, when using physics the angle must always be 90º, which points downwards in the canvas. For simulated trajectories or tweening, you can change the gravity angle to fit your needs.
{% endhint %}

2. Configure the Sliders
   1. InitialVelocity:
      1. Min value: 100
      2. Max Value: 500

<figure><img src="https://3242409510-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9bZkqK9vYvPkvVDUO4Tp%2Fuploads%2FDL2KYTjEtNoZyhpdfTg6%2Fimage.png?alt=media&amp;token=c8eb1d10-1ceb-424c-afbe-eb8e483e913f" alt="" width="230"><figcaption></figcaption></figure>

2. LaunchAngle:
   1. Value: 270 (start angle)
   2. Min value: 270º (upward)
   3. Max value: 360º (to the right)

<figure><img src="https://3242409510-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9bZkqK9vYvPkvVDUO4Tp%2Fuploads%2F9uUNrRAAjju19JGscxxB%2Fimage.png?alt=media&amp;token=4cb6c6ff-79b1-4a7e-9e0d-0d1031999d5d" alt="" width="227"><figcaption></figcaption></figure>

## Step 3: Create a function to calculate the trajectory

1. Create a function called **`CalculateTrajectory`**. It will be called each time the value of the sliders changes, to redraw the trajectory.
2. Add the **`Bird.Set Parabola by Velocity`** action to the function as follows:
   1. Use the "default" tag
   2. Set the Velocity from our slider called **`InitialVelocity.Value`**
   3. Set the Angle ity from our slider called **`LaunchAngle.Value`**
   4. Set the gravity to 10. This value <mark style="color:orange;">**must match our physics world gravity**</mark>

<figure><img src="https://3242409510-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9bZkqK9vYvPkvVDUO4Tp%2Fuploads%2FPqLN8wj6BSIcwga1nR3Z%2Fimage.png?alt=media&amp;token=8074e20c-76a0-4b2e-a5ad-8aa7882c3640" alt=""><figcaption><p>Calling: Set Parabola By Velocity </p></figcaption></figure>

<figure><img src="https://3242409510-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9bZkqK9vYvPkvVDUO4Tp%2Fuploads%2FSWtENqpz2X2sTbI9UeM0%2Fimage.png?alt=media&amp;token=941c1616-3f09-4cf8-b16e-4850c08227ac" alt=""><figcaption><p>Stablishing the world gravity to match the parabola's gravity.</p></figcaption></figure>

3. Call **`Path.destroy`** to clean up our layout from path objects. *<mark style="color:red;">This approach is only for educational purposes. It's recommended to implement optimized algoritms to maintain a good performance.</mark>*
4. Add a Repeat action from the system plugin. We will create 20 points in the path, each at 0.1 seconds of the predicted trajectory.

<figure><img src="https://3242409510-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9bZkqK9vYvPkvVDUO4Tp%2Fuploads%2FYbJRfMhPLWeshz5hmHvK%2Fimage.png?alt=media&amp;token=36ee24a1-49d5-47af-ac77-658e504a2197" alt=""><figcaption></figcaption></figure>

5. Create a Path object within the Repeat, and add the following parameters:
   1. **Layer**: Use the **`Bird.LayerName`**
   2. **X**: Read the predicted X coordinate of the parabola calculated by calling the expression **`GetXAt`** passing the "*default*" tag name, and the **`loopindex`** multiplied by 0.1 seconds.
   3. Y: Read the predicted X coordinate of the parabola calculated by calling the expression **`GetYAt`** passing the "*default*" tag name, and the **`loopindex`** multiplied by 0.1 seconds.

<figure><img src="https://3242409510-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9bZkqK9vYvPkvVDUO4Tp%2Fuploads%2FRpV1pStBgpx8CBv2cAiX%2Fimage.png?alt=media&amp;token=4f83186e-60e5-4b80-8ef4-37b913483e2b" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3242409510-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9bZkqK9vYvPkvVDUO4Tp%2Fuploads%2FwS3EruiaHBv3cTGWPxKN%2Fimage.png?alt=media&amp;token=04ae0013-dbbb-40f3-81b5-7985752e931b" alt=""><figcaption><p>CalculateTrajectory function</p></figcaption></figure>

## Step 4: Add the Interaction with the UI

Each time our slider's value changes, the function CalculateTrajectory should be called, triggering the path redraw with the new values set for angle and velocity.

Add the following code:

1. Add the condition **`InitialVelocity.On Changing`**&#x20;
   1. Call **`CalculateTrajectory`** for this event.
2. Add the condition **`LaunchAngle.On Changing`**&#x20;
   1. Call **`CalculateTrajectory`** for this event.

<figure><img src="https://3242409510-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9bZkqK9vYvPkvVDUO4Tp%2Fuploads%2FrfHmXGBpX3FOQQ9Vl0VT%2Fimage.png?alt=media&amp;token=4b1b3dc6-b19c-42a4-8c00-44e2c62cf2be" alt=""><figcaption><p>On Changing even triggers for each slider.</p></figcaption></figure>

3. Add the button **`Throw.On Click`** condition to throw the bird. We will apply the calculated velocity to the physics behavior of the bird, to throw it following the calculated trajectory.
   1. Enable the **`Bird.Physics`** behavior.
   2. Call the action **`Bird.Physics.Set`** Velocity to pass the parabola's calculated velocity's vector components.
      1. **X component**: Pass the **`Bird.ParabolicTween.GetVelocityX`** expression using the tag name "*default*".
      2. **Y component**: Pass the **`Bird.ParabolicTween.GetVelocityY`** expression using the tag name "*default*".

<figure><img src="https://3242409510-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9bZkqK9vYvPkvVDUO4Tp%2Fuploads%2FlDY1T3c0iB9LtMkvVzwy%2Fimage.png?alt=media&amp;token=59e87795-c88b-4983-8b2a-661088da8c8f" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3242409510-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9bZkqK9vYvPkvVDUO4Tp%2Fuploads%2F2rmAQbm7y79RaXFIA0aV%2Fimage.png?alt=media&amp;token=72d509da-05d5-4bf8-bc3e-a6747ded177a" alt=""><figcaption><p>Event button <code>Throw.On Click</code> calls the <code>Physics.Set Velocity</code></p></figcaption></figure>

## Step 5: Test your game

Run the game, and play with the sliders. You should be able to change the initial velocity and angle and see the predicted trajectory path. And when pressing **Throw,** the Bird will move at the indicated velocity along the calculated trajectory.

<figure><img src="https://3242409510-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9bZkqK9vYvPkvVDUO4Tp%2Fuploads%2FKdd9d42uQMemDz2RNhfC%2Fimage.png?alt=media&amp;token=15e462e4-642d-43e3-92db-af0887d50f59" alt=""><figcaption></figcaption></figure>

### Source Code

{% file src="/files/LZOhmKpTfeZ4lie1nG34" %}
