none
Getting collision at position and Collision groups. RRS feed

  • Question

  • Hello! I have two questions about the Litdev physics engine, so I thought I'd write them in one question.

    The first is about getting if there is a collision object at a position. What I mean by this is checking if there is a collision at a specified position. something like GetCollisionsAt(x,y)

    Here is what I've tried:

    • I tried making an image with all the collisions in it, importing it to the Imagelist, then using LDImage.GetPixel to check collision.
    • That didn't work because the map size was so large (10000x10000) that the image turned out to be huge. Like 1 GB
    • Next I tried making the collisions into graphicswindow boxes in the background, then use Graphicswindow.Getpixel but that didn't work because if I tried getting a pixel out of the graphicswindow, it threw an error. 

    So is there another way?

    The next question is this: Is there any way to add collision objects to a collision group? Make it so collisions object in group 1 only interact with other objects in group 1,  collisions object in group 2 only interact with other objects in group 2 ect.

    This would make things like being able to have things like aircraft and tanks in the same game. so the aircraft interact with the aircraft, and the tanks interact with the tanks, but they don't interact with eachother.

    Thanks!!


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    Sunday, November 15, 2015 5:47 AM
    Answerer

Answers

  • I added and uploaded a new method GetContacts.  It gets a list of all contacts within a distance of a set point.

    Example below based on the previous test case, to get all contacts within 100 pixels of the center of the screen.

    While ("True")
      LDPhysics.DoTimestep()
      contacts = LDPhysics.GetContacts(gw/2,gh/2,100)
      For i = 1 To Array.GetItemCount(contacts)
        TextWindow.WriteLine(contacts[i][1]+" hit "+contacts[i][2])
      EndFor
      Program.Delay(25)
    EndWhile
    

    • Marked as answer by Zock77Editor Monday, November 16, 2015 5:23 PM
    Sunday, November 15, 2015 5:09 PM
    Moderator

All replies

  • 1] You can get a list of all collisions a shape makes in a step (GetCollisions) - perhaps this will work. There can be a lot of collisions and SB arrays can be slow so I deliberately limited this method to collisions for a specified shape.

    Maybe say why you want an exact pixel for a collision?  This isn't really how Box2D works - it uses areas surrounding shapes.

    I do log all collisions so it would be easy to add a new method to return results based on a different criteria to GetCollisions, but I wouldn't want it to a very big array.

    2] This sounds like 2 separate engines running at the same time, is this right?

    All of what you want is certainly possible, but may need tinkering with the underlying Box2D engine and adding more methods to the SB interface.  I will have a look what may be possible but don't want to add so many options that it is too daunting to use in SB.

    You may like to consider having a go with the Box2D engine directly and using a .Net C# interface instead of SB.  This really wouldn't be too hard given your understanding of the physics engine and C#.  I would be happy to help you get started.

    Sunday, November 15, 2015 2:08 PM
    Moderator
  • Added a method LDPhysics.SetGroup

    Here is a test where the red balls on collide with themselves and walls, and the blue balls only with themselves and wall, but red and blue balls do not interact.

    Test beta version uploaded with this.

    If you have some more info on exactly what you are doing with the collision position we can see what would be the best way to proceed.  As usual I look forward to seeing your game.

    gw = GraphicsWindow.Width
    gh = GraphicsWindow.Height
    radius = 20
    count = 20
    
    For i = 1 To count
      GraphicsWindow.BrushColor = "Red"
      ball = Shapes.AddEllipse(2*radius,2*radius)
      LDPhysics.AddMovingShape(ball,0.3,0.8,1)
      LDPhysics.SetPosition(ball,radius+Math.GetRandomNumber(gw-2*radius),radius+Math.GetRandomNumber(gh-2*radius),0)
      LDPhysics.SetGroup(ball,1,"1=0;2=1")
      
      GraphicsWindow.BrushColor = "Blue"
      ball = Shapes.AddEllipse(2*radius,2*radius)
      LDPhysics.AddMovingShape(ball,0.3,0.8,1)
      LDPhysics.SetPosition(ball,radius+Math.GetRandomNumber(gw-2*radius),radius+Math.GetRandomNumber(gh-2*radius),0)
      LDPhysics.SetGroup(ball,2,"1=0;2=2")
    EndFor
    
    While ("True")
      LDPhysics.DoTimestep()
      Program.Delay(25)
    EndWhile
    

    Sunday, November 15, 2015 3:44 PM
    Moderator
  • Id be happy for some advice!

    What I'm trying to do is make AI for my game. The current test AI have 3 "Feelers".. One out the front and two 45 degrees off the front. They are just positions that I check for objects.

    I go through and check the feelers, and if there is an object off to the right, it will turn left. If there is an object to the left, it will turn right. And so on. This mostly works, but when I was testing it I could use LDShapes.GetAllShapesAt. I cant do this in game, because there are background images, inactive shapes, and shadows. So I need to only get shapes with collisions. 

    Here is my test bot code:

    LDPhysics.LoadImagesAsCircles = "True"
    For i = 1 To 5
      Bot[i] = Shapes.AddRectangle(30,50)' Shapes.AddImage("C:/R2D2.png")
      Shapes.Move(Bot[i],i*70,i*70)
      Shapes.Rotate(Bot[i],(i/5)*360)
      LDPhysics.AddMovingShape(Bot[i],0.1,0.7,0.7)
      LDPhysics.SetTire(Bot[i])
      Property["Brake"] = 5
    LDPhysics.SetTireProperties(Bot[i],Property)
    EndFor
    
    Rec = Shapes.AddRectangle(100,100)
    Shapes.Move(Rec,300,100)
    LDPhysics.AddFixedShape(Rec,0,0.7)
    
    Array[1] = -45
    Array[2] = 0
    Array[3] = 45
    Array2[1] = 1
    Array2[2] = 1
    Array2[3] = -1
    
    LDPhysics.SetGravity(0,0)
    
    While 1 = 1
      Program.Delay(10)
      LDPhysics.DoTimestep()
      For i = 1 To 5
        BotPos = LDPhysics.GetPosition(Bot[i])
        TempCount = 0
        For q = 1 to 3
        SensorPos = Zock77Math.GetCircle(50,BotPos[1],BotPos[2],(LDPhysics.GetAngle(Bot[i])-90)+Array[q])
        If LDShapes.GetAllShapesAt(SensorPos["x"],SensorPos["y"]) <> "False" Or SensorPos["x"] > GraphicsWindow.Width Or SensorPos["x"] < 0 Or SensorPos["y"] > GraphicsWindow.Height Or SensorPos["y"] < 0 Then
          TempCount = TempCount + 1
          LDPhysics.BrakeTire(Bot[i])
          LDPhysics.TurnTire(Bot[i],Array2[q] * 8000)
          EndIf
       '   GraphicsWindow.DrawEllipse(SensorPos["x"],SensorPos["y"],2,2)
        EndFor
        If TempCount > 1 Then
          LDPhysics.TurnTire(Bot[i],8000)
          EndIf
        LDPhysics.MoveTire(Bot[i],200)
        EndFor
      EndWhile
    Thanks!


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    Sunday, November 15, 2015 4:57 PM
    Answerer
  • I added and uploaded a new method GetContacts.  It gets a list of all contacts within a distance of a set point.

    Example below based on the previous test case, to get all contacts within 100 pixels of the center of the screen.

    While ("True")
      LDPhysics.DoTimestep()
      contacts = LDPhysics.GetContacts(gw/2,gh/2,100)
      For i = 1 To Array.GetItemCount(contacts)
        TextWindow.WriteLine(contacts[i][1]+" hit "+contacts[i][2])
      EndFor
      Program.Delay(25)
    EndWhile
    

    • Marked as answer by Zock77Editor Monday, November 16, 2015 5:23 PM
    Sunday, November 15, 2015 5:09 PM
    Moderator
  • re-uploaded
    Sunday, November 15, 2015 6:07 PM
    Moderator
  • The beta version doesn't seem to be showing up in SB. It doesn't have any LD Objects.

    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    Sunday, November 15, 2015 6:13 PM
    Answerer
  • Would it make a difference that I'm using ver  1.0?

    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    Sunday, November 15, 2015 6:13 PM
    Answerer
  • Cetrainly - sorry this version 1.2

    Is there a reason you can't use 1.2 (XP can't use .Net 4.5)?

    I could put these mods in a special ver 1.0 build, but I don't generally update the old versions with new stuff.  I could reconsider this if there is interest.  Some new stuff uses .Net 4.5 so it would only be partial convergence.


    Sunday, November 15, 2015 6:18 PM
    Moderator
  • Is the beta download 1.2? Cause it doesn't seem to be working with it.

    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    Monday, November 16, 2015 1:30 AM
    Answerer
  • The liitdev 1.2 dll doesn't seem to be working either.

    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11


    Monday, November 16, 2015 1:34 AM
    Answerer
  • Each version of SB needs the corresponding version of extension.  The 1.2 dll and beta dll (with the mods to LDPhysics) can only be used with SB 1.2.

    Also the download may need to be unblocked before it can be used.  See How to Install section on my website.

    Monday, November 16, 2015 9:08 AM
    Moderator
  • Yep. The .dll was blocked :D

    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    Monday, November 16, 2015 5:23 PM
    Answerer
  • are there instructions anywhere about how to recompile extensions? I need to do that to mine. 

    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    Monday, November 16, 2015 5:24 PM
    Answerer
  • Here is that bot program without the Zock77Math extension:

    LDGraphicsWindow.State = 2
    
    LDPhysics.LoadImagesAsCircles = "True"
    For i = 1 To 5
      Bot[i] = Shapes.AddRectangle(30,50)' Shapes.AddImage("C:/R2D2.png")
      Shapes.Move(Bot[i],i*70,i*70)
      Shapes.Rotate(Bot[i],(i/5)*360)
      LDPhysics.AddMovingShape(Bot[i],0.1,0.7,0.7)
      LDPhysics.SetTire(Bot[i])
      Property["Brake"] = 5
      LDPhysics.SetTireProperties(Bot[i],Property)
    EndFor
    
    Rec = Shapes.AddRectangle(100,100)
    Shapes.Move(Rec,300,100)
    LDPhysics.AddFixedShape(Rec,0,0.7)
    
    Array[1] = -45
    Array[2] = 0
    Array[3] = 45
    Array2[1] = 1
    Array2[2] = 1
    Array2[3] = -1
    
    LDPhysics.SetGravity(0,0)
    
    While 1 = 1
      Program.Delay(10)
      LDPhysics.DoTimestep()
      
      For i = 1 To 5
        BotPos = LDPhysics.GetPosition(Bot[i])
        TempCount = 0
        For q = 1 to 3
          
          SensorPos = LDMath.Rotate(BotPos[1],BotPos[2],BotPos[1]+15BotPos[2]-50,Array[q]+(LDPhysics.GetAngle(Bot[i])-18))'Zock77Math.GetCircle(50,BotPos[1],BotPos[2],(LDPhysics.GetAngle(Bot[i])-90)+Array[q])
        '   If LDPhysics.GetContacts(SensorPos[1],SensorPos[2],5) <> "" Then
        '     TextWindow.WriteLine(LDPhysics.GetContacts(SensorPos[1],SensorPos[2],20))
        '     EndIf
          If LDShapes.GetAllShapesAt(SensorPos[1],SensorPos[2]) <> "False" Or SensorPos[1] > GraphicsWindow.Width Or SensorPos[1] < 0 Or SensorPos[2] > GraphicsWindow.Height Or SensorPos[2] < 0 Then
            TempCount = TempCount + 1
            LDPhysics.BrakeTire(Bot[i])
            LDPhysics.TurnTire(Bot[i],Array2[q] * 8000)
          EndIf
          GraphicsWindow.DrawEllipse(SensorPos[1],SensorPos[2],2,2)
        EndFor
        If TempCount > 1 Then
          LDPhysics.TurnTire(Bot[i],8000)
        EndIf
        LDPhysics.MoveTire(Bot[i],200)
      EndFor
    EndWhile

    The problem with it is I can't go in using

    LDShapes.GetAllShapesAt

    Because of background images and inactive shapes. Thats what I meant when I was asking about getting collisions at a point.

    It would serve the purpose of GetAllShapesAt, but it would only return shapes that were registered as collision shapes.


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    Monday, November 16, 2015 5:50 PM
    Answerer
  • To recompile for a new verson, the main thing is to replace (delete then add) the reference to the new SmallBasicLibrary.dll.

    You may also want to upgrade the .Net version to 4.5 from 3.5, but this may not be required. 

    Everything else should then work.

    I now see what yo mean GetShapesAt (but only Physics Engine movin shapes).  I will have a think.

    I like your prototype program with AI.

    Monday, November 16, 2015 6:49 PM
    Moderator
  • Why Thanks! 

    I found this cool site that I think will use to make them seem a but more intelligent. 

    http://gamedevelopment.tutsplus.com/tutorials/understanding-steering-behaviors-wander--gamedev-1624


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11


    • Edited by Zock77Editor Monday, November 16, 2015 6:52 PM Hyperlink
    Monday, November 16, 2015 6:52 PM
    Answerer
  • There is LDPhysics.GetShapeAt, I also added LDPhysics.GetAllShapesAt and uploaded.
    Monday, November 16, 2015 8:12 PM
    Moderator
  • It works like a champ! Thank you!

    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    Tuesday, November 17, 2015 4:00 PM
    Answerer