Skip to content

Respond to collisions#4

Merged
JoeStrout merged 27 commits into
JoeStrout:mainfrom
pipe01:main
Jul 31, 2023
Merged

Respond to collisions#4
JoeStrout merged 27 commits into
JoeStrout:mainfrom
pipe01:main

Conversation

@pipe01
Copy link
Copy Markdown
Contributor

@pipe01 pipe01 commented Jul 27, 2023

First time writing MiniScript, sorry for any mistakes!

In the collisions.ms file I've added a Separating Axis Theorem collision detection implementation, which allows you to find whether or not two objects are colliding as well as finding out how much they are overlapping by. To aid with this I've also implemented a simple Vector2 type to handle vector math more easily, it's contained in the vector.ms file. It's not super optimized yet, but I'm leaving that for later.

The collision response itself isn't done yet, what's there now is only a naive algorithm that always moves just one object. I would like to add some kind of force system, perhaps even with friction and bounciness.

I would also like to add an example for circles, which is one of the SAT algorithm's edge cases.

Fixes #1

@pipe01 pipe01 marked this pull request as draft July 27, 2023 01:02
@JoeStrout
Copy link
Copy Markdown
Owner

Hey that sounds like a great start! The Rigidbody class already handles forces to some extent, though I don't think it's yet handling off-axis forces (imparting torque). So that's something we will need.

I do worry a bit about the performance of using SAT on every pair of rigidbodies. But I guess let's just get it working first, and we can optimize later.

A vec2 module is a good idea, however, instead of using maps, I would prefer to use a 2-element list — and we can define accessors to allow us to reference elements as .x and .y instead of [0] and [1], like this:

// accessors so you can use pos.x as a synonym for pos[0], etc.
list.x = function; return self[0]; end function
list.y = function; return self[1]; end function

But the nice thing about this is, you don't really need a separate factory function to make these; you can just return [x+dx, y+dy] or whatever, and this is a perfectly valid vector. They also print more compactly and are probably more efficient than maps, too.

Welcome to MiniScript, and thanks for your efforts! I look forward to working with you on this.

@pipe01
Copy link
Copy Markdown
Contributor Author

pipe01 commented Jul 27, 2023

But the nice thing about this is, you don't really need a separate factory function to make these; you can just return [x+dx, y+dy] or whatever, and this is a perfectly valid vector. They also print more compactly and are probably more efficient than maps, too.

If I understand it correctly then that list wouldn't have any methods of the vector type right?. We could then instead make regular functions in the vector module, but that seems a bit more unwieldy. Although it quite handy to be able to create them with just a list, hmm..

@JoeStrout
Copy link
Copy Markdown
Owner

If I understand it correctly then that list wouldn't have any methods of the vector type right?. We could then instead make regular functions in the vector module, but that seems a bit more unwieldy.

Not necessarily. We can add more list extension methods, as needed. For some examples (and to be aware of the list extensions the user will likely already have in effect), see: https://github.com/JoeStrout/minimicro-sysdisk/blob/master/sys/lib/listUtil.ms

One thing we can't do in MiniScript is operator overloading. But notice that the above module (which you can use in your own Mini Micro code by doing import "listUtil" at the top of your script) adds things like list.add and list.plus. It even defines list.dot for doing a dot product. So, much of what you need for a vector class is already there.

@pipe01
Copy link
Copy Markdown
Contributor Author

pipe01 commented Jul 27, 2023

That would work but then we would be polluting the list type with vector methods, for example the vector's add method would collide with list's add method.

@JoeStrout
Copy link
Copy Markdown
Owner

Also take a look at https://github.com/JoeStrout/minimicro-sysdisk/blob/master/sys/lib/matrixUtil.ms, which provides code for 2D matrices. Not sure if we really need that for 2D physics, but it's available.

@JoeStrout
Copy link
Copy Markdown
Owner

That would work but then we would be polluting the list type with vector methods, for example the vector's add method would collide with list's add method.

Sorry, I was unclear. Let me try again:

Use the standard listUtil method. Just import it at the top of any physics script. It's a standard system-supplied library, so always available.

Now you don't need to add a "vector add" method, because it's already there. Same with multiply, dot, etc. Everything you get for free in listUtil is already done.

We can add the .x and .y list extension methods, just to make code more readable (though it also makes it slower, so in tight loops we might want to just use [0] and [1] instead). And if you need any other vector-type list extension methods that aren't already in listUtil, add those in the new physics code. I don't know off the top of my head what those might be, though; listUtil is pretty complete.

Feel free to join me on Discord if you want to chat more interactively! Link's on the MiniScript home page.

@pipe01
Copy link
Copy Markdown
Contributor Author

pipe01 commented Jul 27, 2023

Ohh sorry, okay. I'm stupid and just saw the first couple of methods, I didn't see that there were more. Yeah I can totally use those and add more as I see fit.

@pipe01 pipe01 marked this pull request as ready for review July 31, 2023 00:54
@JoeStrout JoeStrout merged commit 4fa8e9f into JoeStrout:main Jul 31, 2023
@JoeStrout
Copy link
Copy Markdown
Owner

Thanks for your hard work! It looks like all we need now is torques (so blocks will collide & rotate properly).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add simple collision response

2 participants