updated sprite wobble anim

This commit is contained in:
turanszkij
2019-08-28 20:14:50 +01:00
parent db340b16ed
commit 0fe8b30ea2
4 changed files with 39 additions and 18 deletions
+3 -2
View File
@@ -130,6 +130,7 @@ TestsRenderer::TestsRenderer()
sprite.params.siz = XMFLOAT2(200, 100);
sprite.params.pivot = XMFLOAT2(0.5f, 0.5f);
sprite.anim.rot = XM_PI / 4.0f;
sprite.anim.wobbleAnim.amount = XMFLOAT2(0.14f, 0.14f);
this->addSprite(&sprite);
break;
}
@@ -401,7 +402,8 @@ void TestsRenderer::RunSpriteTest()
static wiSprite sprite("../images/logo_small.png");
sprite.params = params;
sprite.anim = wiSprite::Anim();
sprite.anim.wobbleAnim.amount = XMFLOAT2(1.2f, 0.8f);
sprite.anim.wobbleAnim.amount = XMFLOAT2(0.11f, 0.18f);
sprite.anim.wobbleAnim.speed = 1.4f;
addSprite(&sprite);
static wiFont font("Wobble animation: ");
@@ -442,7 +444,6 @@ void TestsRenderer::RunSpriteTest()
sprite.params.pivot = params.pivot;
sprite.params.col = XMFLOAT4(2, 2, 2, 1); // increase brightness a bit
sprite.params.sampleFlag = SAMPLEMODE_MIRROR; // texcoords will be scrolled out of bounds, so set up a wrap mode other than clamp
sprite.anim = wiSprite::Anim();
sprite.anim.movingTexAnim.speedX = 0;
sprite.anim.movingTexAnim.speedY = 2; // scroll the texture vertically. This value is pixels/second. So because our texture here is 1x2 pixels, just scroll it once fully per second with a value of 2
addSprite(&sprite);
+11 -14
View File
@@ -126,22 +126,19 @@ void wiSprite::Update(float dt)
// Wobble anim:
if (anim.wobbleAnim.amount.x > 0 || anim.wobbleAnim.amount.y > 0)
{
// offset a random corner of the sprite:
int corner = wiRandom::getRandom(0, 4);
anim.wobbleAnim.corner_target_offsets[corner].x = (wiRandom::getRandom(0, 1000) * 0.001f - 0.5f) * anim.wobbleAnim.amount.x;
anim.wobbleAnim.corner_target_offsets[corner].y = (wiRandom::getRandom(0, 1000) * 0.001f - 0.5f) * anim.wobbleAnim.amount.y;
// All corners of the sprite will always follow the offset corners to obtain the wobble effect:
wiImageParams default_params;
// Rotate each corner on a scaled circle (ellipsoid):
// Since the rotations are randomized, it will look like a wobble effect
// Also use two circles on each other to achieve more random look
wiImageParams default_params; // contains corners in idle positions
for (int i = 0; i < 4; ++i)
{
XMVECTOR I = XMLoadFloat2(&default_params.corners[i]);
XMVECTOR V = XMLoadFloat2(&params.corners[i]);
XMVECTOR O = XMLoadFloat2(&anim.wobbleAnim.corner_target_offsets[i]);
V = XMVectorLerp(V, I + O, 0.01f);
XMStoreFloat2(&params.corners[i], V);
anim.wobbleAnim.corner_angles[i] += XM_2PI * anim.wobbleAnim.speed * anim.wobbleAnim.corner_speeds[i] * dt;
anim.wobbleAnim.corner_angles2[i] += XM_2PI * anim.wobbleAnim.speed * anim.wobbleAnim.corner_speeds2[i] * dt;
default_params.corners[i].x += std::sin(anim.wobbleAnim.corner_angles[i]) * anim.wobbleAnim.amount.x * 0.5f;
default_params.corners[i].y += std::cos(anim.wobbleAnim.corner_angles[i]) * anim.wobbleAnim.amount.y * 0.5f;
default_params.corners[i].x += std::sin(anim.wobbleAnim.corner_angles2[i]) * anim.wobbleAnim.amount.x * 0.25f;
default_params.corners[i].y += std::cos(anim.wobbleAnim.corner_angles2[i]) * anim.wobbleAnim.amount.y * 0.25f;
params.corners[i] = default_params.corners[i];
}
}
+24 -1
View File
@@ -1,6 +1,7 @@
#pragma once
#include "wiImage.h"
#include "wiGraphicsDevice.h"
#include "wiRandom.h"
class wiResourceManager;
@@ -40,8 +41,30 @@ public:
struct WobbleAnim
{
XMFLOAT2 amount = XMFLOAT2(0, 0); // how much the sprite wobbles in X and Y direction
float speed = 1; // how fast the sprite wobbles
XMFLOAT2 corner_target_offsets[4] = {}; // internal use; you don't need to initialize
float corner_angles[4]; // internal use; you don't need to initialize
float corner_speeds[4]; // internal use; you don't need to initialize
float corner_angles2[4]; // internal use; you don't need to initialize
float corner_speeds2[4]; // internal use; you don't need to initialize
WobbleAnim()
{
for (int i = 0; i < 4; ++i)
{
corner_angles[i] = wiRandom::getRandom(0, 1000) / 1000.0f * XM_2PI;
corner_speeds[i] = wiRandom::getRandom(500, 1000) / 1000.0f;
if (wiRandom::getRandom(0, 1) == 0)
{
corner_speeds[i] *= -1;
}
corner_angles2[i] = wiRandom::getRandom(0, 1000) / 1000.0f * XM_2PI;
corner_speeds2[i] = wiRandom::getRandom(500, 1000) / 1000.0f;
if (wiRandom::getRandom(0, 1) == 0)
{
corner_speeds2[i] *= -1;
}
}
}
};
bool repeatable = false;
+1 -1
View File
@@ -9,7 +9,7 @@ namespace wiVersion
// minor features, major updates
const int minor = 28;
// minor bug fixes, alterations, refactors, updates
const int revision = 14;
const int revision = 15;
long GetVersion()