How to implement a physical effect in SimCity 5, t

2019-02-07 00:39发布

问题:

It is hard to describe in plain text, so I recorded a GIF to demonstrate.

https://public.lightpic.info/image/2B1F_582465841.gif

I am currently working on a project that requires such effect. Up to now I have finished a similar effect on iOS with SpriteKit, however unfortunately it turns out the result is not pretty satisfying.

This is my work: in my project there is an object with an irregular shape, a constant force is applied at somewhere below the gravity center of the object and its direction is downward. I have made it a rule that the object's coordinate cannot be changed by the force, so that the object will not be pulled downwards by the force. When the object is tilted, its inertance and the force result an angular momentum, which makes the object upright. Details:

And when the object is being manipulated, the point of action is higher than the gravity center. As a result of combination of the object's inertance and the force, an angular momentum makes the object tilted.

However nothing has proven I am right, because the result I get from SpriteKit's physics engine is that, the object turns out to be a simple pendulum. But intuition tells me I was not wrong, in fact I did not pinned the object's gravity center to the background, and what I really did is to put the object back to the original position every time physics is simulated. But the result slapped my face really hard :(. Therefore, the pendulum swings and swings, the final physical effect just sucks.

Then I worked out a temporary solution: to multiply the angular velocity by 0.95 every time physics is simulated. This solution is clearly not the ideal one, because when the rotation angle tends to be horizontal, the angular velocity is not high enough and becomes upright slowly. However there is an advance: at least the object is able to stop swinging at last.

My further solution is that the force that is applied on the object changes with the tilt level. When the object tends to be horizontal, the force tends to be big, and when the object tends to be vertical, the force tends to be small. A simple function would describe it well: F=1000N×|sin[rotation degrees]|. This helps a lot, but unfortunately the result effect does not seem to be physical at all.

Conclusion: after many days’ study, I failed to implement the effect demonstrated in the GIF, and I feel very ashamed of that. I really hope anyone with great ability would help me. Thank you for reading my long description, I really appreciate your patience.

Addition:

There is a screenshot that shows my approach to implement this effect.

Addition 2:

I have uploaded my implementation, which is a Swift Playground file. Download it at: http://www.mediafire.com/file/qrct5sty2cyvwsy/Swing.playground.zip

PS. As my native language is not English, please forgive my poor grammar.

回答1:

A spring, a swing arm and some rust..

What you are after is a swing arm attached to a spring and a dampener that moves on a trolley.

The spring applies force to return the swing arm back to the upright position.

The dampening (in the demo it is applied at the rotation joint as friction such as found in a rusty joint) is there just to stop it oscillating forever.

Moving the mouse applies a force in the opposite direction at the top of the swing arm.

Pendulum V Swing arm

One of the differences between a pendulum and the spring loaded swing arm is that the frequency of the oscillation will change depending on the amount of angular momentum, minimum tension on the spring, and the position of the swing arm.

Interactive illistration

The illistration shows the swing arm in action, but the characteristics of the swingarm is dependent on many many factors; The height of the swing arm, the strength of the spring, the damping, the mass of the swing arm, where the spring is mounted on the arm and fixed to the moving trolley. I have added some sliders to let you see the different behaviours.

The illistration is not an answer, it is only there to illustrate the concept, you will have to implement the solution in whatever software or library you use.

Springs are very simple, with a linear relationship between spring length and force. Lookup Hook's law.

Damping in the example is just a scalar applied to the delta rotation. dr *= 1-damping

The forces are applied to the swing arm at a location as acceleration and force (newtons in pixel BS units). The pivot is fixed and thus any linear acceleration is lost.

Update. There were a few mistakes in the first post. I applied the spring force as acceleration rather than force, the spring could not be tensioned, and the movement of the mouse was incorrectly converted to acceleration. The differences are subtle but important. All fixed