I been reading about RK4 for physics implementation in a game, so I read in some pages and all people recommend me this page:
http://gafferongames.com/game-physics/fix-your-timestep/
This page shows clearly how this one works, but I can't figure out how to implement in my game, maybe I don't understand that good but I find some things that are not really clearly to me.
In my game, the player decides when change direction in the X-Axis but I can't figure out how with this RK4 implementation change the direction of the object, in the example the point goes side to side but I don't understand how I can control when he goes right or left.
So if anyone can give a little bit of clarity in this implementation and my problem which I do not understand I will be really grateful.
Thanks beforehand
Related
I'm working on the game in C++. Basically, the game is going to be a simulation of the world with some animals, plants, and of course a player itself (Animals move in random directions). I wanna do it to practice OPP, algorithm library, and work a little with collections from STL. I want to mention that I'm rewriting this project, and there is only one concern that keeps bothers me - Checking collisions. The game is going to be two-dimensional (only x,y coordinates).
Because if I keep my organisms in vector it's going to take a linear time to find if there are other organisms on these coordinates. When there is more organisms, this search takes a lot of time (seriously, for 400 animals their turn could take 20-30 seconds). So my question is, which STL collection would fit my problem best. Thank you in advance for your help.
Animals may die, their strength may be modified, and they can also create offsprings.
http://pastebin.com/ccck8LjE
Hello i've written this code to try and simulate the moon earth sun system. I get a circular orbit for the earth but the moon goes mad.
Is there a better way to try and do this whilst still using a euler/leapfrog method?
I didn't take the effort to carefully review your code or test its behavior, but it is well known that the Euler method gains energy in such simulations, so "trapezoidal" is generally used instead.
https://en.wikipedia.org/wiki/Trapezoidal_rule
If I understand correctly, your method is second order in the relationship between position and velocity (I assume with the intent of avoiding that energy gain) but it appears to be first order in the relationship between acceleration and velocity.
You might be able to keep the leapfrog design (rather than trapezoidal) but in trying to do so, you should do a better job of making the extra state explicit to eliminate the use of second derivative.
Edit: I reread the Wikipedia page on leapfrog and reread your code and it is clear you are not using leapfrog (as I said earlier because you only apply it to position\velocity not to velocity\acceleration). Note that page also shows you how to simplify the equations of leapfrog for the situation (that you have) in which acceleration depends on position and not on velocity:
https://en.wikipedia.org/wiki/Leapfrog_integration
Maybe you could try to use Runge Kutta methods if you want to keep using explicit integration : https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods
RK4 is usually enough.
Because this is a complex question that usually results in much confusion (I've asked variations on this question previously, but never asked the question the right way and never got an answer), I'm going to try to make this as clear as possible.
Facts:
I'm using Unity.
I'm can get the Forward, Up and Right vectors easily from any Quaternion rotation.
I can't simply record my own Euler angles, modify them and apply the rotation through a new Quaternion because the object is controlled by physics.
I don't understand maths very well at all unless it's written in code (or pseudo-code), so this would be most beneficial to me.
A C++ style answer would be easiest for me to understand, but I can work out pretty much any kind of code.
I'm NOT trying to get anyone to write the code for me! I'm only asking for the answer in code or pseudo-code because I never learned to read normal maths squiggles; I'm a programmer, not a mathematician.
Unity uses a left-handed coordinate system. X = right, Y = up, Z = forward.
What I'm trying to do:
I'm trying to play an animation on a humanoid bone structure and, using torque (rotational force), push the physics ragdoll into approximately the same pose as the bone structure.
The problem:
I can work fully in Quaternions right up to the point where I need to apply the torque to the rigidbodies. The AddTorque function effectively works in Euler angles, which means I can't use the Quaternions. I can easily extract Euler angles from the Quaternions, but they are unreliable and cause the ragdoll to spaz out severely.
What I need:
I need to calculate reliable 3D Euler angles (as in, ones that don't flip from + to - "randomly") from Forward, Up and Right vectors. I realise this is a bit complicated, but that's why I ask here: I lack the knowledge and experience to work out this problem myself.
Given that the vectors themselves are reliable, I see no reason why it would not be possible to work out reliable Euler angles from them. Also, I don't know what order of rotation I would want or need for the Euler angles, but I believe that would be fairly easy to modify later.
Any help would be much appreciated!
First, I'd like to say that I solved my problem entirely due to #Tobias's efforts. Many, many thanks! All this time I've been approaching the problem from the wrong end. I assumed that I needed to use AddTorque in a particular way (with Euler angles) and worked from there, but #Tobias (and #JarkkoL a little later) pointed out that I needed to use AddTorque differently.
So this is what I did, in UnityScript (effectively JavaScript):
var quat0:Quaternion;
var quat1:Quaternion;
var quat10:Quaternion;
quat0=transform.rotation;
quat1=target.transform.rotation;
quat10=quat1*Quaternion.Inverse(quat0);
rigidbody.AddTorque(quat10.x,quat10.y,quat10.z,ForceMode.Force);
And, against all expectations, this WORKS!! It just... works! Sure, it takes a long time for the rigidbody cube to settle down, but that's because I need a PID controller. Or maybe quat10 needs normalising, not sure. I'll work it out :)
I had no idea you could actually use that part of a quat by itself.
First off, I think you would have better luck in Unity forums for Unity specific questions (: That said, I think you are misinterpreting the AddTorque() interface if this one is what you are using: http://docs.unity3d.com/ScriptReference/Rigidbody.AddTorque.html
Instead of passing Euler angles you pass a vector to the function that's the axis of rotation. I'm not familiar with Unity, but I believe the length of the vector specifies how much torque to add. Euler angles are inherently bad representation for rotations so you should always assume (unless otherwise documented) that well established API's work with quaternions, axis/angle pairs or matrices when it comes to rotations. Euler angles are more of a convenience representation for end users.
I'm having a hard time to understand the need of ccpNormalize, the cocos2d reference says it multiplies the CGPoint to 1. I can't just google it that doens't explain it instead of showing in code.
is it that obvious that I can't find it?
feeling stupid here, please help
The idea of normalising a vector is to have magnitude of 1, this is important if you only care about the direction of your vector.
For example maybe you want to keep all your sprite moving at constant speed regardless of any collisions and so you may want to normalise their velocity after the collision and then multiply by you relevant constant speed.
If you are more interested in reading about the subject try http://en.wikipedia.org/wiki/Normalized_vector
Does anyone have some useful beginner tutorials and code snippets for playing with basic geometric shapes and geometric proofs in code?
In particular something with the ability to easily create functions and recursively draw them on the screen. Additional requirements, but not absolute, support for Objective-C and basic window drawing routines for OS X and Cocoa.
A specific question how would one write a test to validate that a shape is in fact a square, triangle, etc. The idea being that you could draw a bunch of shapes, fit them together and test and analyze the emergent shape that arises from the set of sub shapes.
This is not a homework question. I am not in school. Just wanted to experiment with drawing code and geometry. And looking for an accessible way to play and experiment with shapes and geometry programming.
I am open to Java and Processing, or Actionscript/Haxe and Flash but would also like to use Objective C and Xcode to build projects as well.
What I am looking for are some clear tutorials to get me started down the path.
Some specific applications include clear examples of how to display for example parts of a Cantor Set, Mandelbrot Set, Julia set, etc...
One aside, I was reading on Wikipedia about the "Russell's Paradox". And the wiki article stated:
Let us call a set "abnormal" if it is
a member of itself, and "normal"
otherwise. For example, take the set
of all squares. That set is not itself
a square, and therefore is not a
member of the set of all squares. So
it is "normal". On the other hand, if
we take the complementary set that
contains all non-squares, that set is
itself not a square and so should be
one of its own members. It is
"abnormal".
The point about squares seems intuitively wrong to me. All the squares added together seem to imply a larger square. Obviously I get the larger paradox about sets. But what I am curious about is playing around with shapes in code and analyzing them empirically in code. So for example a potential routine might be draw four squares, put them together with no space between them, and analyze the dimensions and properties of the new shape that they make.
Perhaps even allowing free hand drawing with a mouse. But for now just drawing in code is fine.
If you're willing to use C++ I would recommend two libraries:
boost::GGL generic geometry library handles lots of geometric primitives such as polygons, lines, points, and so forth. It's still pretty new, but I have a feeling that it's going to be huge when it's officially added into boost.
CGAL, the Computational Geometry Algorithms Library: this thing is huge, and will do almost anything you'll ever need for geometry programming. It has very nice bindings for Qt as well if you're interested in doing some graphical stuff.
I guess OpenGL might not be the best starting point for this. It's quite low-level, and you will have to fight with unexpected behavior and actual driver issues. If you emphasize the "playing" part, go for Processing. It's a programming environment specifically designed to play with computer graphics.
However, if you really want to take the shape testing path, an in-depth study of computer vision algorithms in inevitable. On the other hand, if you just want to compare your shapes to a reference image, without rotation, scaling, or other distortions, the Visual Difference Predictor library might help you.
I highly recommend NeHe for any beginner OpenGL programmer, once you complete the first few tutorials you should be able to have fun with geometry any way you want.
Hope that helps