Advertisement

temporary force, how to best code

Started by February 26, 2015 11:16 AM
4 comments, last by Madhed 9 years, 11 months ago

Ok.

Lets say i have a sprite that have a weight * gravity.

How do you best code some "random" input force on the sprite?

Do you code the force like a particle system, min-force, max-force, force direction, and delta time the force has to develop.

Basically i am coding a small flight game and i want to simulate different wind-forces affecting the airplane.

I know how to do this, but i dont know if i am doing a good coding pattern for this.

airplane has 4 forces = lift weight drag and thrust.

I want to simulate a small wind force blowing the airplane up, i don´t want to include this is my basic formula for flying. I just want to push the airplane around i guess.

How would your approach be for this?

Add a 5th force, "external", that can be anything coming from other objects. Then add a "wind object" to your world that is invisible but adds a force to all objects (like the airplane) that are close enough, scaled depending on how close they are. And then you can add different wind objects that blow in different directions to create cool effects.

And if you want them to be temporary, code the objects so they have a lifetime and their forces fade out and disappear after a while.

Advertisement

Many thanks Erik, then i am not so off at all.

I will use the "external" force.

I will prob do as you suggest with a wind-object that lives over time with different dir and vel.


I will prob do as you suggest with a wind-object that lives over time with different dir and vel.

An alternative which may make object creation/deletion unnecessary is to change the values of the wind object over time, rather than its existence.

I.e., update external forces each frame, noting that the forces may be zero, and always "add-in" those forces.

That approach would allow you, for instance, to define a variable wind-force based on location, and update the force based on the plane's location.

Either approach should work, but the alternative will require only updating the forces, rather than creating an object when required, and updating its forces, and deciding whether to add the force, and deleting the object when no longer required. You'll have creation, update, "add force" decisions, and deletion routines in either case, so the code size isn't really a consideration.

I.e., rather than:

1. Do I need a wind object?

2. If so, does the wind object exist?

3. If not, create the object.

4. If the object exists, what values should I assign?

5. Update the airplane - including "Is there a wind object for which I have to add forces?"

6. If there is a wind object, get the values and add them in.

7. Do I need the wind object any more?

8. If not, delete it.

Just:

4. assign/determine values - N.B., may be zero.

6. add the values to the airplane forces.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

I think the word you are looking for is an "impulse". This is a momentary force that dies away or is otherwise altered at the point of contact between two or more objects. See the following link for an explanation:

http://hyperphysics.phy-astr.gsu.edu/hbase/impulse.html

Note the time component (dt) such that if you increase the time difference you end up with a greater impulse.

You could even make a function that represents a vector field.

And return varying forces based on the particle's position in a very generic way.

Think about wind turbulences and the like.

This topic is closed to new replies.

Advertisement