Velocity problem with "childrens"

All topics about ZGameEditor goes here.

Moderator: Moderators

Post Reply
User avatar
Ats
Posts: 770
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Velocity problem with "childrens"

Post by Ats »

Hello, I'm currently rewriting the explosions in my game.

So I'm spawning an explosion model with a velocity, represented by a little dot.

This model spawns several other smoke models with their own velocity.
All the smoke should follow the explosion velocity / direction, all while slowing down their own relative velocity after a second.

My problem is that with this little formula, the smoke velocity should regain the explosion velocity and continue it's way out of the screen with the little dot, but instead, it slows down to zero, and I don't understand why:

Code: Select all

CurrentModel.Velocity.X += (CurrentModel.ParentVelocity[0] - CurrentModel.Velocity.X) * 0.01;
CurrentModel.Velocity.Y += (CurrentModel.ParentVelocity[1] - CurrentModel.Velocity.Y) * 0.01;
Here's the strip down example:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnRender>
    <KeyPress Keys=" " RepeatDelay="0.2">
      <OnPressed>
        <ZExpression>
          <Expression>
<![CDATA[ExplosionModel.Position.X = 0;
ExplosionModel.Position.Y = 0;
ExplosionModel.Position.Z = -30;

ExplosionModel.Velocity.X = random(0,5);
ExplosionModel.Velocity.Y = random(0,5);

ExplosionModel.Scale = 0.2;

ExplosionColor.Color = rnd();

createModel(ExplosionModel);]]>
          </Expression>
        </ZExpression>
      </OnPressed>
    </KeyPress>
  </OnRender>
  <Content>
    <Model Name="ExplosionModel" Position="0 0 -30" Velocity="-8.3256 -0.6193 0">
      <OnSpawn>
        <ZExpression>
          <Expression>
<![CDATA[for (int i=0; i<random(10,4); i++)
{
  SmokeModel.Position.X = 0;
  SmokeModel.Position.Y = 0;
  SmokeModel.Position.Z = -30;

  SmokeModel.ParentVelocity[0] = CurrentModel.Velocity.X;
  SmokeModel.ParentVelocity[1] = CurrentModel.Velocity.Y;

  SmokeModel.Velocity.X = SmokeModel.ParentVelocity[0] + random(0,1);
  SmokeModel.Velocity.Y = SmokeModel.ParentVelocity[1] + random(0,1);

  SmokeColor.Color = ExplosionColor.Color;

  SmokeModel.SmokeScale = 1 + 2*rnd();
  SmokeModel.Scale = 0;

  createModel(SmokeModel);
}]]>
          </Expression>
        </ZExpression>
      </OnSpawn>
      <OnUpdate>
        <ZExpression>
          <Expression>
<![CDATA[if (CurrentModel.Position.X < -20 || CurrentModel.Position.X > 20 || CurrentModel.Position.Y < -20 || CurrentModel.Position.Y > 20)
{
  @RemoveModel();
}]]>
          </Expression>
        </ZExpression>
      </OnUpdate>
      <OnRender>
        <RenderSetColor Name="ExplosionColor" Color="1 0.502 0.7529 1"/>
        <RenderSprite/>
      </OnRender>
    </Model>
    <Model Name="SmokeModel">
      <Definitions>
        <Variable Name="SmokeTimer"/>
        <Variable Name="SmokeScale"/>
        <Array Name="ParentVelocity" SizeDim1="2"/>
      </Definitions>
      <OnUpdate>
        <ZExpression>
          <Expression>
<![CDATA[CurrentModel.SmokeTimer += App.DeltaTime;

if (CurrentModel.SmokeTimer < 0.5)
{
  // Get bigger
  CurrentModel.Scale.X += (CurrentModel.SmokeScale - CurrentModel.Scale.X) * 0.2;
  CurrentModel.Scale.Y = CurrentModel.Scale.Z = CurrentModel.Scale.X;
}
else
{
  // Slow down
  CurrentModel.Velocity.X += (CurrentModel.ParentVelocity[0] - CurrentModel.Velocity.X) * 0.01;
  CurrentModel.Velocity.Y += (CurrentModel.ParentVelocity[1] - CurrentModel.Velocity.Y) * 0.01;

  // Get smaller
  CurrentModel.Scale.X += (0 - CurrentModel.Scale.X) * 0.005;
  CurrentModel.Scale.Y = CurrentModel.Scale.Z = CurrentModel.Scale.X;

  if (CurrentModel.Scale.X < 0.1) @RemoveModel();
}]]>
          </Expression>
        </ZExpression>
      </OnUpdate>
      <OnRender>
        <RenderSetColor Name="SmokeColor" Color="0.502 0.502 0.7529 1"/>
        <RenderSprite/>
      </OnRender>
    </Model>
  </Content>
</ZApplication>
User avatar
Ats
Posts: 770
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Velocity problem with "childrens"

Post by Ats »

All right, I replaced the array per two variables, and now it is working...

Code: Select all

CurrentModel.Velocity.X += (CurrentModel.ParentVelocityX - CurrentModel.Velocity.X) * 0.01;
CurrentModel.Velocity.Y += (CurrentModel.ParentVelocityY - CurrentModel.Velocity.Y) * 0.01;
It's always surprising to come back to ZGE after a while on other languages :lol:
User avatar
Kjell
Posts: 1915
Joined: Sat Feb 23, 2008 11:15 pm

Re: Velocity problem with "childrens"

Post by Kjell »

Hi Ats,
Ats wrote: Sun Jun 12, 2022 6:17 pmMy problem is that with this little formula, the smoke velocity should regain the explosion velocity and continue it's way out of the screen with the little dot, but instead, it slows down to zero, and I don't understand why
It's because when you spawn a clone that contains a array in its Definitions the values of the original array don't get copied to the clone's array. Below is a simple example that demonstrates this.

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <ZExpression>
      <Expression>
<![CDATA[// Assign value between 1 and 9 to cells in array

for(int i=0; i<4; i++)
{
  FooData[i] = 1+rnd()*9;
}

// Spawn a clone

createModel(Foo);]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
  <Content>
    <Model Name="Foo">
      <Definitions>
        <Array Name="FooData" Type="1" SizeDim1="4"/>
      </Definitions>
      <OnSpawn>
        <ZExpression>
          <Expression>
<![CDATA[// Print all values in the array

for(int i=0; i<4; i++)
{
  Print.Text += intToStr(FooData[i])+" ";
}]]>
          </Expression>
        </ZExpression>
      </OnSpawn>
      <OnRender>
        <RenderText Name="Print"/>
      </OnRender>
    </Model>
  </Content>
</ZApplication>
You should use a vec2 or model ( referencing the parent ) or 2 floats ( as you ended up doing ) instead :wink:

K
Post Reply