Reputation: 1103
We're using Verlet method to move a Tukey. But it only moves it once and not continuously. The codes for updating the position of the Turkey is in Update() method so it should rune every frame. But It only runed once.
Futhermore, we put three times the codes for updating the position of the Turkey Line rendered object in the update method and it seems that the new position of the Turkey only move as if we moved it once.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GenerateTurkeys : MonoBehaviour
{
public LineRenderer lineRenderer;
// Start is called before the first frame update
//int numberOfTurkeys;
static int NUM_PARTICLES = 26;
float fTimeStep;
Vector3[] m_position = new Vector3[NUM_PARTICLES];
Vector3[] m_acceleration = new Vector3[NUM_PARTICLES];
Vector3[] m_oldPosition = new Vector3[NUM_PARTICLES];
Vector3[] m_newPosition = new Vector3[NUM_PARTICLES];
void Start()
{
lineRenderer = gameObject.GetComponent<LineRenderer>();
lineRenderer.GetPositions(m_position);
for(int i = 0; i < m_acceleration.Length; i++)
{
m_acceleration[i] = new Vector3(0.0f, -9.8f, 0.0f);
}
fTimeStep = 5.5f;
}
// Verlet integration step void ParticleSystem::
void Verlet()
{
var random_direction = Random.Range(-1, 1);
for (int i = 0; i < NUM_PARTICLES; i++)
{
m_newPosition[i] = 2 * m_position[i] - m_oldPosition[i] + m_acceleration[i] * fTimeStep * fTimeStep;
m_oldPosition[i] = m_position[i];
}
}
// Update is called once per frame
void FixedUpdate()
{
Verlet();
lineRenderer.SetPositions(m_newPosition);
}
}
Upvotes: 1
Views: 147
Reputation: 548
First of all, FixedUpdate
is used by the physics engine and updates differently from the normal Update
method. Unless what you want to do has to be synced with the physics engine then you should use Update
.
Secondly, your m_position
vector is never updated, you call lineRenderer.getPositions
only in the Start
method. Because of this, your m_oldPositions
will always be the same and the position won't change. To correct this your Verlet
method should also update the m_position
vector after the new position has been computed.
Something like this:
void Verlet()
{
var random_direction = Random.Range(-1, 1);
for (int i = 0; i < NUM_PARTICLES; i++)
{
m_newPosition[i] = 2 * m_position[i] - m_oldPosition[i] + m_acceleration[i] * fTimeStep * fTimeStep;
m_oldPosition[i] = m_position[i];
m_position[i] = m_newPosition[i];
}
}
Upvotes: 3