Skip to content

Commit 66b89cf

Browse files
committed
Fixed!
1 parent 05270d0 commit 66b89cf

File tree

2 files changed

+28
-30
lines changed

2 files changed

+28
-30
lines changed

apps/typegpu-docs/src/examples/simulation/gravity/compute.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -142,29 +142,28 @@ export const computeGravityShader = tgpu['~unstable'].computeFn({
142142
destroyed: computeLayout.$.inState[currentId].destroyed,
143143
});
144144

145-
if (current.destroyed === 1) {
146-
return;
147-
}
145+
if (current.destroyed === 0) {
146+
for (let i = 0; i < computeLayout.$.celestialBodiesCount; i++) {
147+
const other = computeLayout.$.inState[i];
148148

149-
for (let i = 0; i < computeLayout.$.celestialBodiesCount; i++) {
150-
const other = computeLayout.$.inState[i];
149+
if (d.u32(i) === input.gid.x || other.destroyed === 1) {
150+
continue;
151+
}
151152

152-
if (d.u32(i) === input.gid.x || other.destroyed === 1) {
153-
continue;
154-
}
153+
const dist = std.max(
154+
radiusOf(current) + radiusOf(other),
155+
std.distance(current.position, other.position),
156+
);
157+
const gravityForce = (current.mass * other.mass) / dist / dist;
155158

156-
const dist = std.max(
157-
radiusOf(current) + radiusOf(other),
158-
std.distance(current.position, other.position),
159-
);
160-
const gravityForce = (current.mass * other.mass) / dist / dist;
159+
const direction = std.normalize(other.position.sub(current.position));
160+
current.velocity = current.velocity.add(
161+
direction.mul((gravityForce / current.mass) * dt),
162+
);
163+
}
161164

162-
const direction = std.normalize(other.position.sub(current.position));
163-
current.velocity = current.velocity.add(
164-
direction.mul((gravityForce / current.mass) * dt),
165-
);
165+
current.position = current.position.add(current.velocity.mul(dt));
166166
}
167167

168-
current.position = current.position.add(current.velocity.mul(dt));
169168
computeLayout.$.outState[input.gid.x] = CelestialBody(current);
170169
});

packages/typegpu/tests/examples/individual/gravity.test.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,20 +131,19 @@ describe('gravity example', () => {
131131
let dt = (time_1.passed * time_1.multiplier);
132132
let currentId = input.gid.x;
133133
var current = CelestialBody_4(inState_3[currentId].destroyed, inState_3[currentId].position, inState_3[currentId].velocity, inState_3[currentId].mass, inState_3[currentId].radiusMultiplier, inState_3[currentId].collisionBehavior, inState_3[currentId].textureIndex, inState_3[currentId].ambientLightFactor);
134-
if ((current.destroyed == 1)) {
135-
return;
136-
}
137-
for (var i = 0; (i < celestialBodiesCount_5); i++) {
138-
let other = (&inState_3[i]);
139-
if (((u32(i) == input.gid.x) || ((*other).destroyed == 1))) {
140-
continue;
134+
if ((current.destroyed == 0)) {
135+
for (var i = 0; (i < celestialBodiesCount_5); i++) {
136+
let other = (&inState_3[i]);
137+
if (((u32(i) == input.gid.x) || ((*other).destroyed == 1))) {
138+
continue;
139+
}
140+
let dist = max((radiusOf_6(current) + radiusOf_6((*other))), distance(current.position, (*other).position));
141+
let gravityForce = (((current.mass * (*other).mass) / dist) / dist);
142+
var direction = normalize(((*other).position - current.position));
143+
current.velocity = (current.velocity + (direction * ((gravityForce / current.mass) * dt)));
141144
}
142-
let dist = max((radiusOf_6(current) + radiusOf_6((*other))), distance(current.position, (*other).position));
143-
let gravityForce = (((current.mass * (*other).mass) / dist) / dist);
144-
var direction = normalize(((*other).position - current.position));
145-
current.velocity = (current.velocity + (direction * ((gravityForce / current.mass) * dt)));
145+
current.position = (current.position + (current.velocity * dt));
146146
}
147-
current.position = (current.position + (current.velocity * dt));
148147
outState_7[input.gid.x] = current;
149148
}
150149

0 commit comments

Comments
 (0)