Log In  

Hi all, just a quick noob question. I'm struggling with making two objects (asteroids in this case) coming down a trajectory on x,y and colliding, deflect each other a little more realistically (but without having to implement a physics engine!). I don't have the maths skills as of yet, I'm guessing something to do with angles/trigonometry?

At the moment I'm basically forcing the leftmost asteroid to the left and the rightmost object to the right, but 90% of times this just looks very unnatural.

Would someone kindly point me some resource I could check up on/read/watch on how to approach this? I don't have the smarts to figure it out on my own, but will be more than ok researching it on my own - just need to be pointed in the right direction. Any tips or pointers would be greatly appreciated.

Thank you in advance!

Pedro.

P#114546 2022-07-19 17:08

Wikipedia is very good for subjects that don't have much room for partiality (theoretical math, programming algorithms, the actual equations in physics, etc.). The article on elastic collisions would be helpful if you don't want the asteroids to slow down:
https://en.wikipedia.org/wiki/Elastic_collision

Particularly, the section with the header "Two-dimensional collision with two moving objects" starts with equations that would probably work if you don't mind using that many trig functions. You would just need to either figure out the contact angle or a decent approximation (like the angle from one center of mass to the other).

If you do want them to slow down, the article on inelastic collisions has the 1-dimensional versions, which you could use to give a decent guess as to how it would work, but really I'd just advise setting an arbitrary percentage of momentum loss and multiply the velocities by that each collision if you do want slow-down.

P#114550 2022-07-19 17:22

Awesome thank you, I think this is exactly what I was asking for. Will try to take it from here. Thanks

P#114560 2022-07-19 20:18

Hello, @matamouros. I think it is simpler than this.

Take the direction the shot hit from, add that with the direction the object came from and divide by 2.

For instance, object approaching acceleration x+2, y+0, shot hits at x-1, y-2. So the resulting object would be x+.5, y-1.

P#114615 2022-07-21 19:51

@dw817
The question wasn't about shooting an object. The use case here is asteroids. Besides the possibility of having different masses, they also might have a large enough average radius for the player to be able to tell what side of each asteroid was hit. The reason the formula on wikipedia is so complicated is because it's for collisions where the object hit each other from any angle.

That's not to say it's perfect. The images clarify that it's for round objects (like balls), but it's still going to result in much more pleasing results in context. The main downside is just that it doesn't include a calculation for how spin each object gets (which would require at least polygonal representation of collision shapes).

@matamouros
That said, dw817 is correct that in some cases adding the movement vectors would work. Mainly, it'd work if your asteroids are round, small, and all have the same mass (or weight if you want to think of it that way). In that case, dividing the speeds after addition would be a way to represent the momentum lost due to friction. You'd also do it for both objects unless one is destroyed, using the original movement in both cases. Dividing by 2 seems a bit high to me for asteroids, though. I would start with multiplying by .8 myself. You could also approximate mass difference by multiplying the results in each case by (mass of self)/(mass of other), and just accept that the resulting angles would be wrong, but I would recommend that only if you either can't figure out how to use the full formula or end up with the game running too slowly.

P#114618 2022-07-21 22:00

Thanks both, really appreciate it. To be honest I'm still catching up to all this. I think I managed to implement the correct inelastic collision algo, things seem to behave correctly 70-80% of times, and then the rest of times I have weird behaviours that I'm putting down to wonky collision detection. I would like to try a simpler approximation for sure as I think it will be less cycles. One other way I thought of was to get the collision angle between two asteroids and then send the collider on a perpendicular angle to that and the collidee on a parallel angle to the collision angle. I would try and eyeball speeds based on rough size of asteroid (there's three sizes: big, medium and small) and same for speed. Probably silly, I would have to try it and see where I thought it wrong. Point is, there's a lot to try and just my spare time to do it, so will take me a while. Thanks for the pointers and will very likely come back to this thread at some point. Thank you!

P#114642 2022-07-22 17:49 ( Edited 2022-07-22 17:49)

[Please log in to post a comment]