Farseer and VB.NET

Topics: Developer Forum, User Forum
Jul 27, 2009 at 6:07 PM

I am facing some problems when using Farseer with VB.NET. Well, first, here is my code:

 

    Dim bmp As New Bitmap(500, 500)
    Dim gfx As Graphics = Graphics.FromImage(bmp)

    Dim sim As New PhysicsSimulator(New Vector2(0.0, 1.0))
    Dim b1 As Body = BodyFactory.Instance.CreateRectangleBody(sim, 50, 150, 1)
    Dim b2 As Body = BodyFactory.Instance.CreateRectangleBody(sim, 50, 150, 1)
    Dim g1 As Geom = GeomFactory.Instance.CreateRectangleGeom(sim, b1, 50, 150)
    Dim g2 As Geom = GeomFactory.Instance.CreateRectangleGeom(sim, b2, 50, 150)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        b1.Position = New Vector2(100, 100)
        b2.Position = New Vector2(200, 100)

        b1.ApplyForce(New Vector2(10, 0))
        b2.IsStatic = True

        g1.CollidesWith = CollisionCategory.All
        g2.CollidesWith = CollisionCategory.All

        g1.CollisionGroup = 10
        g2.CollisionGroup = 19912

        g1.CollisionEnabled = True
        g2.CollisionEnabled = True

        b1.IsStatic = False

        sim.Add(b1)
        sim.Add(g1)
        sim.Add(b2)
        sim.Add(g2)



        b1.Updated = New Dynamics.UpdatedEventHandler(AddressOf Redraw)
        b2.Updated = New Dynamics.UpdatedEventHandler(AddressOf Redraw)

        PictureBox1.Image = bmp
        Redraw()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    End Sub

    Public Sub Redraw()
        gfx.Clear(Color.CornflowerBlue)

        gfx.DrawRectangle(Pens.White, b1.Position.X - 25, b1.Position.Y - 75, 50, 150)
        gfx.DrawRectangle(Pens.White, b2.Position.X - 25, b2.Position.Y - 75, 50, 150)

        Me.Text = b1.Moves & "-" & b2.Moves

        PictureBox1.Refresh()
    End Sub


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        sim.Update(0.5)
    End Sub

 

 

It basically should just display two rectangles, one moving to the other and then there should be a collision. But that doesn't happen. The first box just moves through the other one if it wasn't there. Oh and another Problem: If I compile the defaul Class Library VB.NET can't find the implementation of body.ApplyForce since it's not "unique" ( i am using german VB, the error message is: "ApplyForce" ist mehrdeutig, da in class "FarseerGames.FarseerPhysics.Dynamics.Body" mehrere Arten von Membern mit diesem Namen vorhanden sind. ). So I fixed that issue by renaming the methods (to ApplyForce and ApplyForceRef).

But well, what am I doing wrong? I just want a collision.... =(

Coordinator
Jul 28, 2009 at 7:44 AM
Edited Jul 28, 2009 at 7:47 AM

Try removing the following lines:

        g1.CollidesWith = CollisionCategory.All
g2.CollidesWith = CollisionCategory.All

g1.CollisionGroup = 10
g2.CollisionGroup = 19912

g1.CollisionEnabled = True
g2.CollisionEnabled = True

b1.IsStatic = False

sim.Add(b1)
sim.Add(g1)
sim.Add(b2)
sim.Add(g2)

You already add the bodies and geometries to the physics simulator further up (in the factories) and there is no need to set all the other things since they are default or not needed for a collision.

Edit: I'm not familiar with the details of the VB.net language, but the trick of providing a "ref" version is done a lot of places in the engine - you should have more than one error if it can't overload properly. I will take a look at it and see what can be done about it.

Coordinator
Jul 28, 2009 at 7:48 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.