-
Notifications
You must be signed in to change notification settings - Fork 60
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
Plunger mesh, collision and animation #84
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
freezy
added
unity
Concerns the Unity project
geometry
Related to mesh generation
physics
Related to the physics engine
labels
Jun 15, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds mesh generation, collision and animation for the plunger, and closes #35.
Mesh Generation
Mesh generation is somewhat complex, since there are multiple types, and the plunger tip's shape can be customized in the editor. There are a few classes doing that (
PlungerMeshGenerator
,PlungerCoord
andPlungerDesc
). Furthermore, the mesh depends on the plunger's position (see Animation below).Note that we generate the spring and the rod as separate meshes, while Visual Pinball produces one single mesh.
Collision
The plunger's physics bodies consist of a bunch of
LineSeg
s andHitLineZ
s. Three of those are static, the other five are dynamic, i.e. depending on the plunger's position. Since the KdTree where the colliders are stored is static, we had to split those:PlungerCollider
and go into the KdTree.PlungerColliderData
and are attached to the plunger entity.PlungerCollider
implementation then takes inPlungerColliderData
so it can properly test for the nearest hit time.Animation
Since the mesh depends on the plunger's position (it's not just a simple transformation, but vertices are lined up differently), we need to update the mesh when the plunger moves. What's currently happening is that vertices of all positions are packed as a dynamic buffer on the entity (
PlungerMeshBufferElement
), and when updating the mesh, we push the corresponding slice to the mesh.I've asked this question on the Unity forums, and someone explained how to do it like Visual Pinball does it (i.e. upload all vertices to the GPU and just switch the index). Since we rarely do updates, the current approach runs fine, but there is potential for optimization.
Data Components
We have more data structs than usual, here's a brief summary:
PlungerAnimationData
- Only stores the current position of the plunger, and if it has changed. The system that updates the mesh will look at this.PlungerColliderData
- Contains the dynamic colliders, as mentioned abovePlungerMovementData
- The dynamic data of the plunger mechanicsPlungerStaticData
- Static data from the.vpx
file that doesn't change (or only via the (yet-to-be-defined) scripting API)PlungerVelocityData
- Also plunger mechanics data, but only used when calculating the velocity.Systems
There are also a few more systems than usual. In order of execution:
PlungerVelocitySystem
- That would be VP'sPlungerMoverObject::UpdateVelocities()
, as usual.PlungerDisplacementSystem
- VP'sPlungerMoverObject::UpdateDisplacements()
, as usualPlungerAnimationSystem
- Executed once per frame, this calculates the current position and if it has changed. This component is assigned to the children (i.e. the rod and the spring, where the mesh sits).PlungerTransformationSystem
- Updates the meshes, if changed. Also assigned to the children.API
While the API isn't finalized, we still need a way to move the plunger. For now, I've taken Visual Pinball's API, i.e.
PullBack()
andFire()
. Now, there are two parts of this. The first part is the ECS related stuff, i.e. fetching the component data, writing the new values and applying it back to the entity. This went intoPlungerApi
.The second part is computing the new values. The
Fire()
command for example has two dozens of lines dealing with the fire speed and whatnot. This went intoPlungerCommand
.In short, mapping a button to the plunger via the API looks like that:
TODO