# Collision response with spring physics in RK4

I'm trying to figure out what's in the title. I've gotten regular spring physics working and now I'm trying to do something similar with actual collisions (player character colliding with the ground in a game at the moment).

I have an RK4 integrator running in my update loop and I think the only thing I'm missing is the formula that I should use. I've been reading this and I think I need to use the following formula: F=nkd-bn(n.v) where n is the contact normal, k and b are the spring damping coefficients, d is the desired distance of seperation, and v is the relative velocity between the two points connected by the spring.

I need it split up in an x and y value so this is what I've made of it so far:

var n = [0, -1];  // collision normal
var k = 1, b = 1; // spring coefficients
var sceneStates = [[posX, posY], [velX, velY]]; // current position and velocity of colliding object
var force = [0, n[1]*k*sceneStates[0][1]-b*n[1]*(dot(n, sceneStates[1]))]; // [x,y]


I hope this is enough information to let you known where I'm stuck.

• First remark: I think that the specific integration method (RK4) is irrelevant in the question. – b.Lorenz Dec 6 '18 at 20:31
• Second: It might be better answered at other SE-s (like game dev or overflow)) – b.Lorenz Dec 6 '18 at 20:32
• Third: you should try to use mathematical formulas, as the code snippet is of little help in the present form. And also you would be wise to specify the meaning of the vectors in your formula – b.Lorenz Dec 6 '18 at 20:34
• I'll see if I can post this elsewhere. I posted something similar on overflow earlier and they said I should ask here. I mentioned RK4 to give a complete idea of what I'm working with. The code snippet is just what I have, I'm not sure how else I can explain what I have and what the context is (math/physics are obviously not my strong suit) – Dries Dec 6 '18 at 21:08
• Well. First thing you should do is to explain more clearly what you are trying to do: are there actual springs simulated? Or you are only using them as a simple model for contact forces when two bodies touch? – b.Lorenz Dec 6 '18 at 21:12