Skip to content

Particle Break Callback - #3721

Open
Crepestrom wants to merge 17 commits into
PixelGuys:masterfrom
Crepestrom:particleBreakOnHit
Open

Crepestrom wants to merge 17 commits into
PixelGuys:masterfrom
Crepestrom:particleBreakOnHit

Conversation

@Crepestrom

@Crepestrom Crepestrom commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Implements a Callback when a particle touches a block that destroys the particle
currently implmented on cubyz:flame

test commnd:
/particles cubyz:flame ~ ~ ~ 1000 .{.lifeTime=100}

@Crepestrom
Crepestrom marked this pull request as ready for review October 22, 2026 21:44
@Wunka Wunka moved this to Easy to Review in PRs to review Oct 26, 2026
Comment thread src/particles.zig Resolved
Crepestrom and others added 3 commits October 26, 2026 12:46
Co-authored-by: Wunka <56411012+Wunka@users.noreply.github.com>
Co-authored-by: Wunka <56411012+Wunka@users.noreply.github.com>
Co-authored-by: Wunka <56411012+Wunka@users.noreply.github.com>
@Crepestrom

Copy link
Copy Markdown
Contributor Author

thanks for making this easier to read wunka

@IntegratedQuantum IntegratedQuantum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I do want to note one more time:
What's the point of particle changes, when we still don't have particles anywhere???

Comment thread src/callbacks/particles/_list.zig Resolved
Comment thread src/particles.zig Resolved
Comment thread src/particles.zig Resolved
Comment thread src/callbacks/callbacks.zig Resolved
Comment thread src/callbacks/particles/destroy.zig Outdated
}

pub fn run(_: *@This(), params: main.callbacks.ParticleCollideCallback.Params) main.callbacks.Result {
params.source.currentFrame = 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bad solution. This makes the particle stick around for another frame, likely long enough to make it show up as a black particle for one frame (due to lighting being black inside of blocks)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok i fixed that by changing a different line

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be fixed by changing a different line.
The interface is lacking a way to remove the currently viewed particle. You hacked around it by setting a different property. This is not good design. The API accessible to mods should be self-documenting.

Also consider a particle death callback, maybe we want one particle type (e.g. fire) decay into a different one (e.g. smoke) when reaching the timer. Now instead of removing the particle from the list you'd also still spawn the smoke particle.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok created a proper funciton for deleting partcles that this calls

Comment thread src/particles.zig Outdated
zon.getChildOrNull("onTouch") orelse break :blk .noop,
.{
.particle = .{
.typ = @intCast(types.items.len),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This information is completely useless to the callback, in fact if they were to use it, it would cause a crash due to the data not being initialized.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was mostly for letting the callback know what type of particle it was messing with
i have changed the .position = undefined

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was not the issue. typ points at uninitialized memory.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok i have mirrored how blocks do it where it inits the callback after registering all of the particles

@IntegratedQuantum IntegratedQuantum moved this from Easy to Review to In review in PRs to review Oct 31, 2026
Comment thread src/particles.zig Outdated
}
}
pos = @as(Vec3f, @floatCast(v3Pos - playerPos));
_ = if (hasCollided) particle_manager.typesLocal.items[particle.typ].onCollide.run(.{.source = particle, .pos = pos});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The callback should be evaluated with data about the collision, otherwise you cannot e.g. make water drops split into smaller particles that jump away from the collision when hitting the ground.

Maybe the default collision response could even be part of the callback.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm that is a interesting idea of the colision response being a callback
for now i have just given what direction it colided from as a parameter

Comment thread assets/cubyz/particles/flame.zig.zon Resolved
Crepestrom and others added 2 commits November 3, 2026 11:57
Co-authored-by: IntegratedQuantum <43880493+IntegratedQuantum@users.noreply.github.com>
@Crepestrom

Copy link
Copy Markdown
Contributor Author

updated test command to be more visible

@Crepestrom

Copy link
Copy Markdown
Contributor Author

Also I do want to note one more time: What's the point of particle changes, when we still don't have particles anywhere???

I need this specifically for implementing weather as even the particle tests we do in the playtest sessions looks cool

Comment thread src/particles.zig
var colisionDelta: Vec3f = .{0, 0, 0};
hasCollided = hasCollided or collideOnAxis(&v3Pos, posDelta, hitBox, 0, &colisionDelta);
hasCollided = hasCollided or collideOnAxis(&v3Pos, posDelta, hitBox, 1, &colisionDelta);
hasCollided = hasCollided or collideOnAxis(&v3Pos, posDelta, hitBox, 2, &colisionDelta);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now it will clip into the ground if it collides on multiple axis at once, since or does not execute the right hand side when the left hand side is already true.

Also I think the callback would need to run inside the function to make it handle angled surfaces correctly (see other comment)

}

pub fn run(_: *@This(), params: main.callbacks.ParticleCollideCallback.Params) main.callbacks.Result {
main.particles.particle_system.removeParticle(params.particleIndex);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't call it like that, the parent loop is not properly handling removal during iteration, and the particle that gets swapped into this index will be skipped for this update step, causing it to freeze mid-air for one frame.

Comment thread src/assets.zig
while (iterator.next()) |entry| {
particles.particle_manager.register(assetFolder, entry.key_ptr.*, entry.value_ptr.*);
}
iterator.index = 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't mess with the internal state of the iterator. Get a fresh one instead.

Comment thread src/particles.zig
v3Pos.*[dir] += posDelta[dir];
if (physics.collision.collides(.client, @fromBackingInt(dir), -posDelta[dir], v3Pos.*, hitBox)) |box| {
if (posDelta[dir] < 0) {
colisionDelta[dir] = 1;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a delta value. Please use a more fitting name, maybe collisionDirection?

Though ideally we'd use the raw normal, to make particles bounce correctly on angled surfaces.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

3 participants