# JimmyDeemo

Member

261

156 Neutral

• Rank
Member
1. ## Newbie

Firstly i think there is a little confusion here as to what shaders are and how they are used. I believe in general that shaders aren't something done by artists who use modelling software to produce models. They are essentially mathematical computations that get applied at different points in the rendering stage to give certain effects. Check out this page for a brief description of rendering basics, it finishes up by talking about shaders. This has a couple of meanings too. What do you mean by 'limits'? In general there are no limits to the game play aspects that people think up, just different ways of implementing them. If you are talking about limits in terms of pushing graphical processing and effects then you are limited by the engine you use. Unreal engine its probably one of those that are at the fore front of pushing what's possible, just look at some videos of the latest version. You seems to have the right attitude towards where you want to be. Remember that if you want to be a Game Programmer using C++ then you first need to be a regular programmer using C++. Having a greater understanding of the ins and outs of your chosen language is a great benefit, and doesn't mean that you can't put a game twist on things. The point is not to overwhelm yourself, because when i do that i get disinterested.
2. ## External forces and RK4

Apologies, what i meant was 'In each derivative calculation, the momentum is just the same as the forces, because time isn't taken into account.'. I will take some time an do as you suggested. The slight hurdle i have got to get over is that your method looks a little different to mine (and the articles i followed), but i am sure its all there. Once i get my head around it i will post again.
3. ## External forces and RK4

Thanks for your reply, as well as the code but i am afraid i find you answer quite cryptic. So let me just see if i am on the right track here. What you are saying is that for a given state of a Rigid body object, forces would be accumulated in the cycle (e.g. +thrust, -drag, etc.), when it comes to each of the four derivatives it can be considered that the forces are the same and are not influenced by the time. Is that right? So therefore the momentum is just the same as the total forces acting on the object. If i have understood that right, then that's great and I'm pretty sure i can sort my code for that. But i just wondered why this was the case?
4. ## External forces and RK4

So i started this thread a while ago, and the replies dried up. I don't think i made it clear as to my problem so i wanted to re word it so that i might get some more responses. Firstly let me point out that i know RK4 is over kill, but it's mainly just so that i can learn and practise my coding techniques, so while i understand there are good intentions to pointing out other systems lets try and keep this about RK4. So i have implemented a system similar to that detailed on http://gafferongames.com but i am still confused as to where external forces fit into the system. From my understanding the way RK4 works is that it needs to calculate the force of a derivative implicitly from the current state, rather than explicitly applying forces once per update. So if that's the case how do i say apply some thrust to a ship for example? Let me break it down furthur i have the position and momentum of the RigidBody prior to any calculation, i also have the velocity of the current derivative step. What i want to be able to do at this point is apply things like collision input and 'push' forces, and the end result should be to calculate the forces for this current derivative step. I also have the current time step also. Here is some code for you, hopefully its clear enough, please just ask if anything is confusing. /** * Integrate a state forward by a delta time. * * This function uses the RK4 integration method to update the state by a given * time. * * @param state Reference to the state that needs integrating. * @param deltaTime Time to move the state on by. */ void CPhysController::Integrate(CRidgidBody::CRBState &state, float deltaTime) { //RK4 uses 4 derivatives of the current state at different points within the delttime. //Each evaluation uses the previous derivative to calculate next. CRidgidBody::CRBDerivative a = Evaluate(state, deltaTime, CRidgidBody::CRBDerivative() ); CRidgidBody::CRBDerivative b = Evaluate(state, deltaTime*0.5f, a); CRidgidBody::CRBDerivative c = Evaluate(state, deltaTime*0.5f, b); CRidgidBody::CRBDerivative d = Evaluate(state, deltaTime, c); //Using a wighted sum that comes from the Taylor Series expansion, the best overall derivative //is calculated. This can then increment the state. state.UpdatePosition( 1.0f/6.0f * deltaTime * (a.mVelocity + 2.0f*(b.mVelocity + c.mVelocity) + d.mVelocity) ); state.UpdateMomentum( 1.0f/6.0f * deltaTime * (a.mForce + 2.0f*(b.mForce + c.mForce) + d.mForce) ); } CRidgidBody::CRBDerivative CPhysController::Evaluate(CRidgidBody::CRBState &state, float deltaTime, const CRidgidBody::CRBDerivative &prevDerivative) { //First move the state on base on this time step value, using euler integration. state.UpdatePosition( prevDerivative.mVelocity * deltaTime ); state.UpdateMomentum( prevDerivative.mForce * deltaTime ); //Prep our derivative CRidgidBody::CRBDerivative derivative = CRidgidBody::CRBDerivative(); derivative.mVelocity = state.GetVelocity(); Resolve(state, derivative); return derivative; } void CPhysController::Resolve(CRidgidBody::CRBState &currentState, CRidgidBody::CRBDerivative &derivative) { //??? } Any help understanding this would be great. I did think that perhaps i should be coming at the problem from a different angle, perhaps someone here could enlighten me.
5. ## Getting my head around RK4

Hi all, I've not actually had chance to come back to this since my last post, but will do soon. @cannonicus Thanks for you detailed post. I'm not sure that you understood where my confusion comes from. I am quite happy with how the integrator works, and its ins an outs. I am confused about applying forces to an object so i can actually make use of it. Perhaps it might be best to deal with this by example. I have an object that i want gravity to act upon. When using Euler integration i would apply this force each update, solve and then integrate to give me the new position of the object. Where does this application of force fit within the RK4 integrator? I have to put it all in one function right? The equivalent of [color="#1C2837"]Glenn Fiedler's static void forces() function. Is it simply a case of storing external influences on an object and then solving them in each evaluation step?

7. ## Getting my head around RK4

In all honesty, its simply as a learning experience, i don't intend my little game to be particularly physics heavy at all. The code i am writing is mainly for portfolio/demo purposes and i though i would push the boat out a bit. In addition i am creating my own little library of stuff so i figured i'd write it as reusable. After reading the articles on Glenn's blog it seemed that implementing an RK4 integrator might not be much more work anyway. I have done [color=#1C2837][size=2]euler before, but am i so far off the mark with this that i am best to stick with it? [color="#1C2837"] [color="#1C2837"]I also know it might be a good idea to simply use an existing framework, but as i said its for learning and Glenn's blog provided what seemed like a good explanation. In particular the rotation aspect of it, as this was something that confused me even in euler integration.
8. ## Getting my head around RK4

So i want to add some physics to my little demo game, and after reading Glenn Fiedler's blog here, i decided to try and implement an RK4 integrator. So i generally understand how RK4 work, how you get the derivatives from the current object state, applying forces at different points in the time step. Then you move on the simulation based on some kind of weighted average of those 4 derivatives. My problem is understanding how external influences are applied to an object in the system, as Glenn Fiedler's example seems to be dependant on time rather on an object moving under the influence of external forces. Let me give you an example, here is a function and description from the source code in Glenn's article titled 'Physics in 3D': /// Evaluate derivative values for the physics state at future time t+dt /// using the specified set of derivatives to advance dt seconds from the /// specified physics state. static Derivative evaluate(State state, float t, float dt, const Derivative &derivative) { state.position += derivative.velocity * dt; state.momentum += derivative.force * dt; state.orientation += derivative.spin * dt; state.angularMomentum += derivative.torque * dt; state.recalculate(); Derivative output; output.velocity = state.velocity; output.spin = state.spin; forces(state, t+dt, output.force, output.torque); return output; } /// Calculate force and torque for physics state at time t. /// Due to the way that the RK4 integrator works we need to calculate /// force implicitly from state rather than explictly applying forces /// to the rigid body once per update. This is because the RK4 achieves /// its accuracy by detecting curvature in derivative values over the /// timestep so we need our force values to supply the curvature. static void forces(const State &state, float t, Vector &force, Vector &torque) { // attract towards origin force = -10 * state.position; // sine force to add some randomness to the motion force.x += 10 * Mathematics::sin(t*0.9f + 0.5f); force.y += 11 * Mathematics::sin(t*0.5f + 0.4f); force.z += 12 * Mathematics::sin(t*0.7f + 0.9f); // sine torque to get some spinning action torque.x = 1.0f * Mathematics::sin(t*0.9f + 0.5f); torque.y = 1.1f * Mathematics::sin(t*0.5f + 0.4f); torque.z = 1.2f * Mathematics::sin(t*0.7f + 0.9f); // damping torque so we dont spin too fast torque -= 0.2f * state.angularVelocity; } From what i can gather 't is the time since the start of the simulation. In this function he uses the time to give a cube object motion. So firstly i want to ask, is this an integral part of the RK4 implementation? Does the force calculation of a derivative need to be derived from a total time value? What i want to know is how do you use this model in a simple example such as gravity and say a ships thrusters acting against it? My thought is that forces acting on an object would be stored as a variable in the State class, so you would add gravity as a force acting down each update, and the thrusters added to this store acting in the opposite direction on each update the thrusters are active. The forces function would then be modified to be interested in the time step rather than the total time. Giving you something that looks like this: static void forces(const State &state, float dt, Vector &force, Vector &torque) { //Force of derivative is equal to the external forces over the time step. force = state.totalExternalForce * dt; //...omited the rotation for clarity... } Am i on the right lines here? Have i misunderstood something fundamental? Thanks for reading my gibberings, any help understanding this would be much appreciated.
9. ## Calculations on the CPU or the GPU?

Ahh i see. Ok thanks a lot for the comments guys. Much appreciated.
10. ## Calculations on the CPU or the GPU?

Phew that's good to know. I see what you mean about the matrices, it does seems like it would be expensive to do it like that. However, bearing in mind what you said would it be more efficient to calculate the model matrix in the vertex shader? This is constantly changing, unlike (in my case) the projection and view matrices. I suppose this is really an optimisation question isn't it as both methods have the same output; optimisation it probably overkill in my situation as I'm producing a small demo and nothing that complicated.
11. ## OpenGL Calculations on the CPU or the GPU?

So i have been putting together a little game as a (re)learning experience. This time round i have been using OpenGL3 and shaders and there are a few things i'm confused about regarding calculations performed in regular code and those in the shader. So with that in mind, can anyone give me a hand understanding the following questions. 1. Right now my simple shader looks like this: #version 330 uniform mat4 projectionMatrix; uniform mat4 viewMatrix; uniform mat4 modelMatrix; layout (location = 0) in vec4 in_Position; layout (location = 1) in vec4 in_Color; smooth out vec4 pass_Color; void main(void) { gl_Position = projectionMatrix * viewMatrix * modelMatrix * in_Position; pass_Color = in_Color; } So i set my projection, view and model matrix in my main code and simply multiply them in the shader, is that right? Should i be passing more 'raw' data to the GPU and calculating the matrices there? 2. Right now in order to translate and rotate my models (just a cube right now), I use the model matrix. I start with an identity matrix and then apply each transform to it before passing the matrix to the shader (see above). Is that the right idea or should i be using the model matrix for something else AND applying my transformations via another matrix. Any pointers on this would be much appreciated, thanks in advance.
12. ## Why doesn't OpenGL like this?

Its not a big deal right now, as using pointers to memory I've created myself doesn't seem to be a problem. Will check out those debugging tools though. Cheers for the link.
13. ## Why doesn't OpenGL like this?

Hmmmm well i can't find it in the [font=Arial, Lucida, sans-serif][size=2]OpenGL 3.3 Reference Pages which would suggest it has been depreciated? I'd rather not use depreciated functions if i can. Doesn't it seem strange you would need to bind the program when [/font][color="#1C2837"]GetUniformLocation takes the same parameter?
14. ## Why doesn't OpenGL like this?

Hmmm yes i thought so, see my edit above. Its a strange one to me as i can't see the difference as far as OpenGL is concerned, surely it just wants the value to look up the program. I am hoping using a vector of pointers (boost::ptr_vector) will work when i want to have more shaders. Or perhaps having a manger of this kind is the wrong approach?