Circle-Circle impulse resolution not working properly

Recommended Posts

I am doing a little physics project with circle circle collisions for now, and have tried to do impulse resolution for collisions with 2 circles, using the following code. 

relativeVelocity = (other.doVerletVelocity()).subtract(self.doVerletVelocity())
normDirecVel = relativeVelocity.dotProduct(collisionNormal)

restitution = -1 - min(self.restitution, other.restitution)

numerator = normDirecVel * restitution

impulseScalar = numerator / float(1 / self.mass) + float(1 / other.mass)

selfVel = self.doVerletVelocity()
otherVel = other.doVerletVelocity()

impulse = collisionNormal.scalarMult(impulseScalar)

selfDV = impulse.scalarMult(1 / self.mass)
otherDV = impulse.scalarMult(1 / other.mass)

newSelfVel = selfVel.subtract(selfDV)
newOtherVel = otherVel.add(otherDV)

self.oldPos = (self.center).subtract(newSelfVel.scalarMult(dt))
other.oldPos = (other.center).subtract(newOtherVel.scalarMult(dt))

The problem seems to be that whatever value I give to self.mass and other.mass, the output stays exactly the same, the values that I used are:

center = Vector(0, 0)
radius = 1
oldPos = Vector(0, 0)
accel = Vector(0, 0)
mass = 100
restitution = 0.001

center2 = Vector(0, 3.20)
radius2 = 1
oldPos2 = Vector(0, 3.201)
accel2 = Vector(0, -1)
mass2 = 1
restitution2 = 1

the output was:

0.0      0.0      0.0      2.165000000000114
0.0      0.0      0.0      2.1360000000001174
0.0      0.0      0.0      2.1066000000001206
0.0      0.0      0.0      2.076800000000124
0.0      0.0      0.0      2.046600000000127
0.0      0.0      0.0      2.0160000000001306
0.0      0.0      0.0      1.985000000000134
CIRCLE INTERSECTION
0.0      -1.985000000000134      0.0      3.938600000000271
0.0      -3.970000000000268      0.0      5.891800000000408
0.0      -5.9550000000004015      0.0      7.844600000000544
0.0      -7.940000000000535      0.0      9.797000000000681

I changed the values for the masses to make them higher, bu the output still remained the same, if you could get to the bottom of this, it would be much appreciated.

Share this post


Link to post
Share on other sites
23 hours ago, Dirk Gregorius said:

I noticed this:

impulseScalar = numerator / float(1 / self.mass) + float(1 / other.mass)

You are missing parentheses:

impulseScalar = numerator / ( float(1 / self.mass) + float(1 / other.mass) )

 

I tried that, yet it has no effect on the values. When displaying the circles on the screen, using pygame module, it showed that after colliding, the circles moved in opposite directions, but with velocities faster than what they collided with.

Edited by Bob Dylan
I needed to add more information.

Share this post


Link to post
Share on other sites
1 minute ago, Dirk Gregorius said:

Doesn't that appear strange to you? Obviously it should have an effect since :

n/a + b != n / (a + b) for any b != 0

It definitely is strange, yet what I described is the case. I think the error might be in the way that 1/mass is calculated because changing the mass when defining the particle object has not bearing on the resultant impulse.

Share this post


Link to post
Share on other sites
17 minutes ago, Dirk Gregorius said:

What language are you programming in? It might be that it is the type promotion. Try 

float(1.0f / self.mass)

 

Either way, this should be easy to find. Simply debug and step through the code.

 

Are you supposed to deal with penetration in the collision detection phase, or the impulse resolution phase? I ask this because that might be what is throwing this off, because as soon as they collide, the circles make a large jump, which might be affecting their velocities as i am just approximating velocity as current position - old position divided by the timestep, since my velocity verlet implementation does not deal with veloicty explicitly.

Edited by Bob Dylan

Share this post


Link to post
Share on other sites
Just now, Dirk Gregorius said:

You detect penetration in the collision phase. You can deal with it in the impulse phase or in a separate penetration recovery phase.

 

Thanks, I think that is what is causing the value of the velocities to be large after impulse resolution.

Share this post


Link to post
Share on other sites

That means, I would first not worry about penetration recovery and don't support restitution. In particular getting penetration recovery and restitution work correct together can be tricky.

Edited by Dirk Gregorius

Share this post


Link to post
Share on other sites
3 minutes ago, Dirk Gregorius said:

That means, I would first not worry about penetration recovery and don't support restitution.

But there was a rather helpful and in-depth tutorial that showed how to deal with it. Through I will follow your suggestion if my approach to change the penetration handling from the collision detection to impulse resolution section of the code and by the way - thanks for all the help and time that you have taken to help me. I'll let you know how it goes (if you care).

Edited by Bob Dylan

Share this post


Link to post
Share on other sites

One thing I learned over the past years is that with software development in general and physics development in particular is that you want to come to a working solution as fast and easy as possible. From there you want to add one feature at a time always iterating on a working solution.

You are currently seeing yourself what a pain it can be to search for a bug without having an idea where to look. If you use a smart incremental approach this helps to keep problems manageable. That is why I suggest getting the simplest problem working first and then add one feature at a time (e.g. penetration recovery and restitution).

I don't know which tutorial you are referring to so I cannot comment on this. With game physics you have the problem that there are a couple of good references by industry experts and then a bunch of students essentially rewrite these tutorial for studying purposes and quite often introduce errors reflecting their lack of understanding. So when looking at in-depth tutorial make sure they are from people who know what they are talking about.

 

Share this post


Link to post
Share on other sites
1 hour ago, Dirk Gregorius said:

One ... talking about.

 

I will take your advice on board when continuing with this simulation. However, if you want to inspect the tutorial I used, it is the following link.

https://gamedevelopment.tutsplus.com/tutorials/how-to-create-a-custom-2d-physics-engine-the-basics-and-impulse-resolution--gamedev-6331.

Share this post


Link to post
Share on other sites

Hi there, Dirk's advice is trying to describe a method to debug your code. The idea is if you get a working solution, then whenever you make small changes it is easy to know what broke. From here finding problems becomes much easier.

The tutorial you posted is best used by referencing the source code in the final article. The source code is here: https://gamedevelopment.tutsplus.com/tutorials/how-to-create-a-custom-2d-physics-engine-oriented-rigid-bodies--gamedev-8032

Unfortunately I don't have time to manually step through your code to look for problems. The best thing you can try doing is to take Dirk's advice and start with a small working solution. From there you can modify the working code to look more like your own code, and replace piece by piece until something breaks. This can give you clues on where problems lay.

Share this post


Link to post
Share on other sites
7 hours ago, Randy Gaul said:

Hi there, Dirk's advice is trying to describe a method to debug your code. The idea is if you get a working solution, then whenever you make small changes it is easy to know what broke. From here finding problems becomes much easier.

The tutorial you posted is best used by referencing the source code in the final article. The source code is here: https://gamedevelopment.tutsplus.com/tutorials/how-to-create-a-custom-2d-physics-engine-oriented-rigid-bodies--gamedev-8032

Unfortunately I don't have time to manually step through your code to look for problems. The best thing you can try doing is to take Dirk's advice and start with a small working solution. From there you can modify the working code to look more like your own code, and replace piece by piece until something breaks. This can give you clues on where problems lay.

Thanks for your advice, I realise that you do not have much time, but could I trouble you to explain to me when and were you deal with penetration, as I have since got the collision response working with restitution. If you would't mind, could you also explain to me how you would calculate the penetration vector for a circle-circle collision?

As at the moment, I am using this code:

PD = -(self.rad + other.rad) + math.sqrt(centDist)
angle = diff.xAxisAngle()
PDx = PD * math.cos(math.degrees(angle))
PDy = PD * math.sin(math.degrees(angle))
penetrationVec = Vector(PDx, PDy)
dP = penetrationVec.scalarMult(0.5)

self.center = (self.center).add(dP)
other.center = (other.center).subtract(dP)

While it does provide accurate enough collisions, it does however make the circles jump when I represent them on the screen. Thanks again.

Edited by Bob Dylan

Share this post


Link to post
Share on other sites

I'd like to help but the question you're asking is very open ended. There isn't really a specific piece I can help you with here. I'd love to help, but it sounds like you're not sure where the problem is yet.

Share this post


Link to post
Share on other sites

I would recommend adding a visual "step" by "step" debug system, so you clearly see when something goes wrong.

You can just use a bool to force to simulate one step or even step a single body pair. Its really hard to solve physics problems when you just look at the final output.

Render your bounding boxes + orientation and render your contact points inluding the surface normal and penetration distance. Start simple like dirk already recommended and build up from there.

Also making a realtime dragging tool to move/rotate two bodies around at realtime helps a lot!

I made such stuff in the past for javascript if you are interested: http://root.xenorate.com/final/jsstuff/

Edited by Finalspace

Share this post


Link to post
Share on other sites
5 hours ago, Randy Gaul said:

I'd like to help but the question you're asking is very open ended. There isn't really a specific piece I can help you with here. I'd love to help, but it sounds like you're not sure where the problem is yet.

Here is the problem, as explicitly as I can describe it: sometimes, when my circles are moving with velocities not perpendicular to the X or Y axes, when they are supposed to collide, they just get stuck in each other.

This is not what it would be like in real life, and I know that I am supposed to somehow find the penetration vector of the circles and them move them apart.

Now what I want you to do is to tell me how to get the penetration vector and then when to move the circles apart from each other.

Share this post


Link to post
Share on other sites
29 minutes ago, Bob Dylan said:

Now what I want you to do is to tell me how to get the penetration vector and then when to move the circles apart from each other.

That's not something answerable on a forum. You're basically asking how to create an entire physics sim. That said, the answers are in the link you posted earlier. If you can't find them in the link, I'm not really sure anyone can help you here.

29 minutes ago, Bob Dylan said:

Here is the problem, as explicitly as I can describe it: sometimes, when my circles are moving with velocities not perpendicular to the X or Y axes, when they are supposed to collide, they just get stuck in each other.

I appreciate the explicit description. I can't help you because what you are describing is not explicit, even though the description of it is clear. Your problem is vague, even though you described it nicely.

There are many reasons why circles may get stuck inside each other, and the problems could be anywhere. There is no simple answer of "how to make them not stick together".

 
Edited by Randy Gaul

Share this post


Link to post
Share on other sites

One thing you can do is to setup similar situations both in Randy's and your simulators and step through them side by side. Or write down an analytical solution and confirm your results in debugger. The problem is that you need to debug your code and nobody else is going to do this for you.

Edited by Dirk Gregorius

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Forum Statistics

    • Total Topics
      628676
    • Total Posts
      2984172
  • Similar Content

    • By Kerrick
      TL;DR: noob non-coding teacher somehow thinks they can build a narrative educational game in WordPress; plz halp how do I games?
      I'm mostly a teacher with no coding background--I played with teaching myself Java for a bit but couldn't really code anything from scratch. I'm interested in developing (as a hobbyist) an educational game that would be a sort of choice-based narrative branching storyline, a bit like Fallen London/Storynexus. Because it's so storyline based and I don't need crazy 3d animation, I'm considering just building it as a WordPress site with a couple of gamification plug ins to handle inventory and choice consequences. The unique hook is that the game requires (suggests really) that the player accomplish real world building challenges to accomplish your goals. I.e. Your character has to cross a river? Get some popsicle sticks or cardboard or whatever and build a model bridge. Take a photo of your bridge and upload it to your portfolio to continue (and maybe I don't develop this feature right away).
      I'm in this for three reasons: the educational value for families and teachers, the storyline and world I'm building that I'm super excited about, and the fame and massive wealth (just kidding but it has to have to potential to pay for itself).
      Before I sink too much of my life into this, I want to know more about what I'll need to do to make it work.
      Specifically my questions are:
      1) I get that WordPress isn't optimal for developing games. But can I do it or will I have to learn a new engine because I can't make do what I want? (And if so, what's a better engine with low-to-no coding prereq that will still allow me to sell my game?)
      2) How do I budget for a project like this? (The link to the Reddit post about legal fees was very useful thank you)
      3) What are the additional considerations when creating a game like this intended for children with adult supervision? (Obviously privacy, and I need to cover my assets in case some kid takes "go build a bridge!" too literally and gets hurt with mama's table saw in the garage...)
      4) Is this not the forum for this since what I'm talking about is something more like educational narrative fiction and certainly not the spectacularly complex and amazing projects you all are working on?
      Thank you for any and all thoughts.
    • By Vegermy
      What are some of the best ways to actually sell your prototype or a Proof of Concept to a company? Would a power point presentation be best? Or maybe a video like a teaser trailer to go along with a power point presentation? I'm just trying to get some ideas of how to best go about this.
    • By turanszkij
      I am porting a DX11 game engine to DX12, and have some questions about root signature usage. The engine clearly defines a global bind slot range for resources, to avoid using shader reflection. This means, that both the application, both the shaders explicitly know the binding slots for everything (constant buffers, textures, etc.). This would trivially translate to a global root signature in DX12, and I have created it as a big root signature like the following: There are 12 descriptor tables, so for every shader stage there are a sampler descriptor table and a CBV_SRV_UAV table. I though about keeping a "staging" descriptor heap, to which I would place descriptors at resource load time. Then when calling PSSetShaderResources(), which is a DX11-style function, the function would fill out a GPU-visible descriptor heap which is created to match the layout of the root signature tables. The nice thing about is that each frame I would only have a single call to set the root signature and to set root signature tables. The command list would just fill it with descriptors before rendering.
      I've found out however, that filling out descriptor tables can only be done on the CPU timeline with ID3D12Device::CopyDescriptors(). This complicates things and I can only think of populating the GPU descriptor heap, and after each draw call, I would need to copy the whole table and CopyDescriptors() again when there is a new call to PSSetShaderResources(). Am I on the right path if I also want to keep DX11-style bindings? 
      I also thought about each shader having a custom root signature and only ever copying the descriptors which are required by the shader, but in this case I will need to rebind the whole root signature each time a shader changes. I would also have to do a shader reflection step and modify my shader-asset pipeline (because I don't want to depend on d3dcompiler when I run the engine) which I really want to avoid.
      Which would be the preferred way? I read the Nvidia Do's and Don'ts but they say these which are contradicting in my case:
      Minimize the number of Root Signature changes Don’t bloat your root signature and descriptor tables to be able to reuse them UPDATE: The second way (unique root signature with PSO) I would also have to bind resources strictly after I have set the right PSO, because the table is now unique. 
    • By thesargento
      Hi there. I’m a veteran video gamer but an MMORPG fan since a few years back. Most recently I was hooked on Guild Wars 2, Marvel Heroes Omega and Naruto Online. But it turns out that GW2 came up with completely unnecessary mounts so I stopped playing. MHO was shut down without any solid explanation so I’m left with Naruto even though is not a real MMORPG.
      The monetization of these kinds of games –pay-to-win systems, loot-boxes, and general micro-transactions- have had influenced me in the past to even mount premade Private Servers of games like Aion and Jade Dynasty. But nowadays these tendencies are on the rise so I really want a gaming experience free from it completely.  
      I miss games more focused on player skills but I also like the MMO character growing style. Whit that in mind, I remember Guild Wars, the original, to be a really good gaming experience.
      So my question is: Where to start to turn an MMORPG into a single player game?
      -I’m not interested in any commercialization or nothing like sort.
      -I’ve been able to mount premade private servers.
      -I’ve modded quite a few games via tutorials and just experimenting.
      Have some knowledge of HTML, PHP, XML, Javascript and LUA.
       
      Thnx in advance, I appreciate any help/guidance
    • By @Teejay_Cherian
      This is for a dissertation im working on  regarding procedural generation directed towards indie Developers so if you're an indie dev please feel free to share your thoughts
      Does run-time procedural generation limit the designer's freedom and flexibility? if( Have you ever implemented procedural generation ==true){ talk about  some of the useful algorithms used}  else {explain why you haven't} Do you think indie Devs are taking advantage of the benefits provided by procedural generation? What are some of the games that inspired you to take up procedural content generation? If there is anyway i can see your work regarding proc gen please mention the link ( cz i need actual indie developers to make a valid point in my dissertation) Thank You So Much
  • Popular Now