Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paintings fixed math #36

Open
wants to merge 6 commits into
base: nds
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion src/game/paintings.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include <PR/ultratypes.h>

#ifdef TARGET_NDS
#include "../nds/nds_include.h"
#endif

#include "sm64.h"
#include "area.h"
#include "engine/graph_node.h"
Expand Down Expand Up @@ -615,6 +619,46 @@ void painting_update_ripple_state(struct Painting *painting) {
* @return the ripple function at posX, posY
* note that posX and posY correspond to a point on the face of the painting, not actual axes
*/
#ifdef TARGET_NDS
s16 calculate_ripple_at_point(struct FixedPainting *fixedPainting, f32 posX, f32 posY) {
/// Controls the peaks of the ripple.
s32 rippleMag = fixedPainting->currRippleMag;
/// Controls the ripple's frequency
s32 rippleRate = fixedPainting->currRippleRate;
/// Controls how fast the ripple spreads
s32 dispersionFactor = fixedPainting->dispersionFactor;
/// How far the ripple has spread
s32 rippleTimer = fixedPainting->rippleTimer;
/// x and y ripple origin
s32 rippleX = fixedPainting->rippleX;
s32 rippleY = fixedPainting->rippleY;

s32 distanceToOrigin;
s32 rippleDistance;

s32 sposX = (floattof32(posX) * ((fixedPainting->size) / floattof32(PAINTING_SIZE)));
s32 sposY = (floattof32(posY) * ((fixedPainting->size) / floattof32(PAINTING_SIZE)));

distanceToOrigin = sqrtf32(mulf32((sposX - rippleX), (sposX - rippleX))
+ mulf32((sposY - rippleY), (sposY - rippleY)));

// A larger dispersionFactor makes the ripple spread slower
rippleDistance = (distanceToOrigin<<12) / dispersionFactor;
if (rippleTimer < rippleDistance) {
// if the ripple hasn't reached the point yet, make the point magnitude 0
return 0;
} else {
// use a cosine wave to make the ripple go up and down,
// scaled by the painting's ripple magnitude
s16 angle = (mulf32(rippleRate, (rippleTimer - rippleDistance))*DEGREES_IN_CIRCLE) >> 12;
s32 value = cosLerp(angle);
f32 rippleZ = mulf32(rippleMag, value);

// round it to an int and return it
return f32toint(rippleZ);
}
}
#else
s16 calculate_ripple_at_point(struct Painting *painting, f32 posX, f32 posY) {
/// Controls the peaks of the ripple.
f32 rippleMag = painting->currRippleMag;
Expand Down Expand Up @@ -648,6 +692,7 @@ s16 calculate_ripple_at_point(struct Painting *painting, f32 posX, f32 posY) {
return round_float(rippleZ);
}
}
#endif

/**
* If movable, return the ripple function at (posX, posY)
Expand Down Expand Up @@ -683,6 +728,16 @@ s16 ripple_if_movable(struct Painting *painting, s16 movable, s16 posX, s16 posY
*/
void painting_generate_mesh(struct Painting *painting, s16 *mesh, s16 numTris) {
s16 i;
#ifdef TARGET_NDS
struct FixedPainting fixed_painting;
fixed_painting.currRippleMag = floattof32(painting->currRippleMag);
fixed_painting.currRippleRate = floattof32(painting->currRippleRate);
fixed_painting.dispersionFactor = floattof32(painting->dispersionFactor);
fixed_painting.rippleTimer = floattof32(painting->rippleTimer);
fixed_painting.rippleX = floattof32(painting->rippleX);
fixed_painting.rippleY = floattof32(painting->rippleY);
fixed_painting.size = floattof32(painting->size);
#endif

gPaintingMesh = mem_pool_alloc(gEffectsMemoryPool, numTris * sizeof(struct PaintingMeshVertex));
if (gPaintingMesh == NULL) {
Expand All @@ -693,8 +748,13 @@ void painting_generate_mesh(struct Painting *painting, s16 *mesh, s16 numTris) {
gPaintingMesh[i].pos[1] = mesh[i * 3 + 2];
// The "z coordinate" of each vertex in the mesh is either 1 or 0. Instead of being an
// actual coordinate, it just determines whether the vertex moves
#ifdef TARGET_NDS
gPaintingMesh[i].pos[2] = ripple_if_movable(&fixed_painting, mesh[i * 3 + 3],
gPaintingMesh[i].pos[0], gPaintingMesh[i].pos[1]);
#else
gPaintingMesh[i].pos[2] = ripple_if_movable(painting, mesh[i * 3 + 3],
gPaintingMesh[i].pos[0], gPaintingMesh[i].pos[1]);
#endif
}
}

Expand Down Expand Up @@ -1284,4 +1344,4 @@ Gfx *geo_painting_update(s32 callContext, UNUSED struct GraphNode *node, UNUSED
gPaintingMarioZPos = gMarioObject->oPosZ;
}
return NULL;
}
}
12 changes: 12 additions & 0 deletions src/game/paintings.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ struct Painting {
f32 size;
};

#ifdef TARGET_NDS
struct FixedPainting {
s32 currRippleMag;
s32 currRippleRate;
s32 dispersionFactor;
s32 rippleTimer;
s32 rippleX;
s32 rippleY;
s32 size;
};
#endif

/**
* Contains the position and normal of a vertex in the painting's generated mesh.
*/
Expand Down