none
How to create 2D physics from scratch RRS feed

  • Question

  • Hello I was wondering if anyone could help create 2D physics from scratch. I think I can figure it out for the most part, but I don't know how to do things that involve rotating collision boxes/masks. Say for example I have a pinwheel in the shape of a + . This pinwheel needs to be able to rotate, but I need to be able to have objects collide with it at all degrees of rotation. Say I have a player walk into the pinwheel, the pinwheel should rotate as the player pushes on it. My question is how do I modify the collision of the pinwheel so the player or other objects are able to still collide with it as it is rotating?

    A good example of the physics that I want to recreate can be found in this video https://www.youtube.com/watch?v=UjBTU02cCVY  

    This video shows you everything that I wan to do. My problem I I just don't know how to rotate the collision boxes(or whatever they are called) of objects to be able to create the physics.

    Wednesday, September 23, 2020 9:22 PM

Answers

All replies

  • Hello,

    First off I have never work with what you are after but thought you might check out the following two libraries.

    https://github.com/mattleibow/jitterphysics

    https://github.com/bepu/bepuphysics2

    And this one

    http://newtondynamics.com/forum/newton.php 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, September 23, 2020 11:24 PM
    Moderator
  • Thanks. Although after doing some thinking I think I figured out how to make it work, but I don't quite know how to apply the math into my code. So what I think I need to do is use trigonometry to find the angle that I need and then plot points.

    The problem is that I don't quite know how to make that all work. The idea is to define points to use for collision, then I just need to move the points in a way that they create a new angle but I need to keep them at the same distance from the center. So lets say I have two points that create a 45 degree angle. I then need to move one of the points to create a 44 degree angle while at the same time keeping the points the same distance apart.

    Imagine a circle, I have a point in the center and a point on the edge of the circle. The circle had radius of 10. The point on the edge of the circle can move along the edge of the circle. As it moves it remains at the same distance form the center, but every time it moves it creates a new angle. How could I implement something like this? A way that you could look at it is: I need to draw a circle. 

    How do I put this into code or is there some way to detect collisions by detecting if the pixels from one image are colliding with the pixels from another image?

    Thursday, September 24, 2020 9:08 PM
  • I can't answer your question as stated in my first reply have never worked with this.

    have you researched the libraries in the link I provided to see if they will perform the work and/or provide ideas?

    Not knowing how it works from what you have wrote is going to take time and exploration along with trial and error.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, September 24, 2020 10:21 PM
    Moderator
  • yes I looked at what you showed me but they look to be for 3D stuff I need 2D. I did find some code that I tried and it almost worked but there was a problem. I use the code below, x1 and y1 is the center x2 and y2 is the point I want to rotate. I created a timer that executes the rotate function and increases the angle by one each time. I also created a window form to draw it on. The problem is that it doesn't look to rotate correctly and the point moves away from the center.

    private void rotate(double cx, double cy, int angle1, double px, double py)
            {
                var angle = angle1 * Math.PI / 180;
                double s = Math.Sin(angle);
                double c = Math.Cos(angle);
    
                // translate point back to origin:
                px -= cx;
                py -= cy;
    
                // rotate point
                double xnew = px * c - py * s;
                double ynew = px * s + py * c;
    
                // translate point back:
                px = xnew + cx;
                py = ynew + cy;
                x2 = Convert.ToSingle(px);
                y2 = Convert.ToSingle(py);
            }

     
    Thursday, September 24, 2020 11:52 PM
  • Hi LavaCreeperKing,

    Most of the members here may not have experience in developing physics engines, so I suggest you can ask questions about physics engines in stackoverflow.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, September 25, 2020 6:15 AM

  • How do I put this into code or is there some way to detect collisions by detecting if the pixels from one image are colliding with the pixels from another image?

    I had posted a sample using Pixel Perfect Collision in VB.NET

    You can check the result with 2 images (balls) : VB_Direct2D_Collision.exe

    I don't know if it is what you're trying to do....


    • Edited by Castorix31 Friday, September 25, 2020 6:31 AM
    Friday, September 25, 2020 6:30 AM
  • Yes this does look something I want to do. I just need to detect when one object collides with another object. I also need to be able to rotate objects and still detect collisions. If I can somehow use the pixels from an image to detect collisions that will work. How do I do that?
    Friday, September 25, 2020 12:33 PM
  • As I said , in this test I used the Pixel Perfect Collision method

    You can find samples on Google

    I used a similar algorithm as the PixelPerfectTest function at Source: Simple Collision Detection

    Friday, September 25, 2020 1:09 PM