none
Collsion shape for LDPhysics RRS feed

  • Question

  • Hey guys! What would be the best way to use one shape as a collision for another shape. So if I had an image of a triangle, I could make a polygon and use it as the collision object.

    The problem is getting a shape that is active without a collision.

    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

    Tuesday, March 24, 2015 6:43 PM
    Answerer

Answers

  • After a little playing, option 2 is hard due the rotation and in particuar the center of rotation, and option 1 suffers if the zooming has to be too extreme to keep the image shape inside the outer compound polygon.

    So I modified the AddMovingShape method (test beta uploaded) so that if the restitution is negative, then the shape is added with a very small size, and can then be grouped on top of the complex compound shape without any zooming.

    My simple test (crude shapes, but idea should work).

    CompoundImage()
    GraphicsWindow.MouseDown = OnMouseDown
    
    While ("True")
      start = Clock.ElapsedMilliseconds
      
      If (mouseEvent) Then
        ball = Shapes.AddEllipse(8,8)
        Shapes.Move(ball,GraphicsWindow.MouseX-4,GraphicsWindow.MouseY-4)
        LDPhysics.AddMovingShape(ball,0.5,0.5,1)
        mouseEvent = "False"
      EndIf
      LDPhysics.DoTimestep()
      
      delay = 25-(Clock.ElapsedMilliseconds-start)
      If (delay > 0) Then
        Program.Delay(delay)
      EndIf
    EndWhile
    
    Sub CompoundImage  
      GraphicsWindow.PenWidth = 0
      body = Shapes.AddRectangle(100,50)
      cab = Shapes.AddRectangle(40,40)
      wheelFront = Shapes.AddEllipse(25,25)
      wheelMiddle = Shapes.AddEllipse(25,25)
      wheelBack = Shapes.AddEllipse(25,25)
      
      Shapes.Move(body,100,100)
      Shapes.Move(cab,205,110)
      Shapes.Move(wheelBack,120,135)
      Shapes.Move(wheelMiddle,155,135)
      Shapes.Move(wheelFront,210,135)
      
      LDPhysics.AddMovingShape(body,0.5,0.1,1)
      LDPhysics.AddMovingShape(cab,0.5,0.1,1)
      LDPhysics.AddMovingShape(wheelFront,0.5,0.3,1)
      LDPhysics.AddMovingShape(wheelMiddle,0.5,0.3,1)
      LDPhysics.AddMovingShape(wheelBack,0.5,0.3,1)
      
      LDPhysics.GroupShapes(body,cab)
      LDPhysics.GroupShapes(body,wheelFront)
      LDPhysics.GroupShapes(body,wheelMiddle)
      LDPhysics.GroupShapes(body,wheelBack)
      
      img = ImageList.LoadImage("https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQF1O1LqqFetb8IHSM22c0IMTk9X7pJiZLI99QHsWXLBWOn65iUAg")
      LDImage.EffectReflect(img,0)
      LDImage.Resize(img,155,70)
      lorry = Shapes.AddImage(img)
      Shapes.Move(lorry,95,93)
      Shapes.SetOpacity(lorry,50)  
      LDPhysics.AddMovingShape(lorry,0.5,-0.3,1) 'Negative restitution to make the image shape small w.r.t physics engine - the image is still full size
      LDPhysics.GroupShapes(body,lorry)
    EndSub
    
    Sub OnMouseDown
      mouseEvent = "True"
    EndSub

    Thursday, March 26, 2015 7:13 PM
    Moderator

All replies

  • I'm not sure I really follow.  Do you mean creating a shape that can be moved to 'hit' other shapes using LDPhysics?
    Wednesday, March 25, 2015 6:41 PM
    Moderator
  • Not quite. I meant a LDPhysics shape that could use a polygon shape as its collision shape.

    For example. If you had an image like this car here: 

    And you wanted to put it in to the physics engine, It would have a collision in the shape of a box. You could make it a circle shape, but that wouldn't be perfect either.

    So What I was wondering, is if there is a way to tell it to have the collision of a polygon. like if I made this poligon, and told it to use it for its collision shape:

    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


    Wednesday, March 25, 2015 8:47 PM
    Answerer
  • OK, I see.

    First, all polygons must be convex i.e. all extenal angles greater than 180 degrees.  You can join shapes together using LDPhysics.GroupShapes to create a compound shape with concave boundaries (including circles for wheels).  These shapes may or not be transparent.  Prototype with them not transparent.

    You can then of couse create a rectangular png image of the same overall shape with transparent boundaries.

    So, how to attach the image shape to the top of the compound shape so that it moves with the compound shape - there is no method at present to do this.

    Ideas...

    1] Create a half size image which would be entirely within the compond shape and add it to the compound shape, then use Shapes.Zoom(shapeName,2,2) to zoom it out to fill the required shape visually.  Only the unzoomed outine (enirely with compound shape) would interact (ie. not interact since it is entirely within the compound shape), while it would appear full size due to the zooming.

    2] Move the image shape manually in the game loop (not controlled by physics engine) based on the actual movement of the interacting compound shape.


    Wednesday, March 25, 2015 9:36 PM
    Moderator
  • After a little playing, option 2 is hard due the rotation and in particuar the center of rotation, and option 1 suffers if the zooming has to be too extreme to keep the image shape inside the outer compound polygon.

    So I modified the AddMovingShape method (test beta uploaded) so that if the restitution is negative, then the shape is added with a very small size, and can then be grouped on top of the complex compound shape without any zooming.

    My simple test (crude shapes, but idea should work).

    CompoundImage()
    GraphicsWindow.MouseDown = OnMouseDown
    
    While ("True")
      start = Clock.ElapsedMilliseconds
      
      If (mouseEvent) Then
        ball = Shapes.AddEllipse(8,8)
        Shapes.Move(ball,GraphicsWindow.MouseX-4,GraphicsWindow.MouseY-4)
        LDPhysics.AddMovingShape(ball,0.5,0.5,1)
        mouseEvent = "False"
      EndIf
      LDPhysics.DoTimestep()
      
      delay = 25-(Clock.ElapsedMilliseconds-start)
      If (delay > 0) Then
        Program.Delay(delay)
      EndIf
    EndWhile
    
    Sub CompoundImage  
      GraphicsWindow.PenWidth = 0
      body = Shapes.AddRectangle(100,50)
      cab = Shapes.AddRectangle(40,40)
      wheelFront = Shapes.AddEllipse(25,25)
      wheelMiddle = Shapes.AddEllipse(25,25)
      wheelBack = Shapes.AddEllipse(25,25)
      
      Shapes.Move(body,100,100)
      Shapes.Move(cab,205,110)
      Shapes.Move(wheelBack,120,135)
      Shapes.Move(wheelMiddle,155,135)
      Shapes.Move(wheelFront,210,135)
      
      LDPhysics.AddMovingShape(body,0.5,0.1,1)
      LDPhysics.AddMovingShape(cab,0.5,0.1,1)
      LDPhysics.AddMovingShape(wheelFront,0.5,0.3,1)
      LDPhysics.AddMovingShape(wheelMiddle,0.5,0.3,1)
      LDPhysics.AddMovingShape(wheelBack,0.5,0.3,1)
      
      LDPhysics.GroupShapes(body,cab)
      LDPhysics.GroupShapes(body,wheelFront)
      LDPhysics.GroupShapes(body,wheelMiddle)
      LDPhysics.GroupShapes(body,wheelBack)
      
      img = ImageList.LoadImage("https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQF1O1LqqFetb8IHSM22c0IMTk9X7pJiZLI99QHsWXLBWOn65iUAg")
      LDImage.EffectReflect(img,0)
      LDImage.Resize(img,155,70)
      lorry = Shapes.AddImage(img)
      Shapes.Move(lorry,95,93)
      Shapes.SetOpacity(lorry,50)  
      LDPhysics.AddMovingShape(lorry,0.5,-0.3,1) 'Negative restitution to make the image shape small w.r.t physics engine - the image is still full size
      LDPhysics.GroupShapes(body,lorry)
    EndSub
    
    Sub OnMouseDown
      mouseEvent = "True"
    EndSub

    Thursday, March 26, 2015 7:13 PM
    Moderator
  • Looks great! That should do the trick!

    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

    Friday, March 27, 2015 4:57 PM
    Answerer