B4A Library ABPhysicsEngine library v0.10

A physics engine for Basic4Android.

[VERSION 1.1] minor update with some bugfixes and speed optimalisations

Please give credit to me if you use this library in your software as it took a lot of research and effort to get the physics right, but still very simple to use.

Features:
- collision detection (Circle-Circle, Rect-Rect, Circle-Rect and Rect-Circle) between groups and within groups
- particles with elasticity, friction, traction, sprite animation, etc...
- Joints with stiffness that connects particles
- groups of particles and joints
- self-buildable forces (like gravity, push, ...) on the engine and individual particles
- vector functions
- ...

*** This is beta and there is a possibility some functions will change in the future***

A demo project with some of the functionalities is added. If you have trouble with the physics, I suggest you google the subject, as I'm not going to explain this stuff. There is a lot of fun in understanding how things work and you're never to old to learn! ;)

Alwaysbusy
 

Attachments

  • ABPhysicsEngine.zip
    315.7 KB · Views: 1,844
Last edited:

Widget

Well-Known Member
Licensed User
Longtime User
A physics engine for Basic4Android.

AlwaysBusy,
Thanks for creating this library. It has a lot of uses. :sign0060:

But your demo doesn't do it justice. When I run the demo I see the wheels float by then Garfield pokes his head in at the top of the screen for a couple of seconds then disappears. I never see more than 1/2 of the Garfield image during the whole demo. The screen is black most of the time. Those are the only 2 animated objects I see.

A lot more people will use this library when they see what it can do. Some people don't have imagination to connect the dots. But not me. I'm going to be connecting the dots and see what I can accomplish with this wonderful library. ;)

If I spent as much work in developing this library as you did, I would spend a couple of evenings creating a couple of dazzling demos that would blow people's socks off. The potential for this library is enormous because it has the potential to animate an ordinary humdrum app so people will enjoy the app more. Your library even works well on the emulator! :sign0098:


Widget
 

alwaysbusy

Expert
Licensed User
Longtime User
The demo was made for a resolution of 800x1280 and 480x800. (these are the devices I have). Is it possible you're running it on a lower resolution (320x480 maybe)?

if so in the tim_Tick function try changing:

B4X:
If panel1.Width = 480 Then
      scale= 0.7
      r.Initialize(0,0,480,800)
Else
      scale = 1
      r.Initialize(0,0,800,1280)
End If

to:

B4X:
If panel1.Width = 480 Then
   scale= 0.7
   r.Initialize(0,0,480,800)
Else
   If panel1.Width = 320 Then
      scale= 0.4
      r.Initialize(0,0,320,480)
   Else
      scale = 1
      r.Initialize(0,0,800,1280)
   End If
End If

This is just a quick demo to show what the beta can do. I'm currently working on a project for a client but I hope I continue on this one soon.
 

Widget

Well-Known Member
Licensed User
Longtime User
The demo was made for a resolution of 800x1280 and 480x800. (these are the devices I have). Is it possible you're running it on a lower resolution (320x480 maybe)?

Ok, that makes sense. I changed the code to:

Scale = Activity.Width / 800
r.Initialize(0,0,Activity.Width,Activity.Height)

and it works fine now. I can actually see the entire animation. Before I was watching just a small portion of the animation and couldn't figure out what was going on. :wav:

Widget
 

Kamac

Active Member
Licensed User
Longtime User
Ok, that makes sense. I changed the code to:

Scale = Activity.Width / 800
r.Initialize(0,0,Activity.Width,Activity.Height)

and it works fine now. I can actually see the entire animation. Before I was watching just a small portion of the animation and couldn't figure out what was going on.

Widget

Had the same problem :sign0137:

Long live HVGA's ;)
 

Kamac

Active Member
Licensed User
Longtime User
- collision detection (Circle-Circle, Rect-Rect, Circle-Rect and Rect-Circle) between groups and within groups

I love 'collision detection' words... I don't know why.


- self-buildable forces (like gravity, push, ...) on the engine and individual particles
- vector functions

Joints with stiffness that connects particles

That's a true 2d newton's physics engine ;)
 

Gawro777

Member
Licensed User
Longtime User
That library looks amazing. Anyone used it since that time? Is the library under further development?
 

vb1992

Well-Known Member
Licensed User
Longtime User
I downloaded and ran the demo / tests,
and they worked fine.

Took a bit to get it going correctly,
since I am new to basic4android

But it looks promising..
 

jota

Active Member
Licensed User
Longtime User
Hello, first apologize for my English "google translator", the library is great, thank you very much for her. But playing a bit I have a question.

By creating various groups with an elasticity type "ice block" I see that taper to stack, how can solve this behavior?. Thank you.

----------------------------------------------------------------------------------

Hola, primero disculparme por mi ingles "google traductor", la libreria es buenisima, muchas gracias por ella. Pero jugando un poco tengo una duda.

Al crear varios grupos con una elasticidad tipo "bloque de hielo" veo que se estrechan al apilarlos, ¿como se puede solventar este comportamiento?. Gracias.


The code I use:


Sub Globals
Dim ph As ABPhysicsEngine
Dim group(10) As ABGroup
Dim part(10) As ABParticle
Dim GendBot As ABGroup
Dim PendBot As ABParticle
Dim bucle,bucle2 As Int
Dim f As ABForce
Dim t As Timer
Dim c As Int
Dim ca As Canvas
End Sub

Sub Activity_Create(FirstTime As Boolean)

ca.Initialize(Activity)
ph.Initialize (1)
f.Initialize(False,0,.3,0,"grav")
ph.AddGlobalForce(f)
ph.Damping = 1

'Stop down to screen out
GendBot.Initialize("GendBot",True)
GendBot.AddParticle(creaParticle(part(0).RECTANGLE,50%x,100%y,100%x,40,0,True,True,0,0,0,111,True,"GendBot"))



ph.AddGroup(GendBot)
c = 0
t.Initialize ("t",100)
t.Enabled = True
End Sub

Sub t_tick
ph.Step()
Dim lGrupos As List
Dim lParticulas As List
Dim rD As Rect
Dim p As ABParticle
Dim g As ABGroup
ca.DrawColor(Colors.Black)
lGrupos=ph.GetGroups
For bucle = 0 To lGrupos.Size -1
g=lGrupos.Get(bucle)
lParticulas= g.GetParticles
For bucle2 = 0 To lParticulas.Size -1
p=lParticulas.Get(bucle2)
If p.name = "C" Then
rD.Initialize((p.CenterX - (p.Width/2)), (p.CenterY - (p.Height/2)), (p.CenterX + (p.Width/2)), (p.CenterY+ (p.Height/2)))
ca.DrawRectRotated(rd,Colors.Blue,True,2,0)
ca.DrawRectRotated(rd,Colors.Yellow,False,2,0)
Else
rD.Initialize((p.CenterX - (p.Width/2)), (p.CenterY - (p.Height/2)), (p.CenterX + (p.Width/2)), (p.CenterY+ (p.Height/2)))
ca.DrawRectRotated(rd,Colors.Red,True,2,0)
End If
Next
Next
Activity.Invalidate
End Sub




Sub Activity_Click
'Add a box each time you tap the screen, the elasticity is TYPE "ICE CUBE"
Dim bb As Int
If c < 10 Then
group(c).AddParticle(creaParticle(part(c).RECTANGLE,50%x,32,32,32,0,True,True,0,0,0,1,False,"C" ))
group(c).AddCollidable(GendBot)
For bb = 0 To c-1
group(c).AddCollidable(group(bb))
Next
ph.AddGroup(Group(c))
c= c+1
End If

End Sub




Sub addGroups(a() As ABGroup)
Dim l As List
l.Initialize2(a)
ph.AddListOfGroups(l)

End Sub

Sub creaParticle(TypeP As Int,centerX As Double, centerY As Double, Width As Double, Height As Double, Radius As Double, Collidable As Boolean,seeCollisionAsHit As Boolean, rotation As Double, elasticity As Double, friction As Double, mass As Double, isFixed As Boolean, Name As String) As ABParticle
Dim p As ABParticle
p.Initialize(TypeP,centerX,centerY,width,height,radius,collidable, seeCollisionAsHit, rotation,name)

p.Elasticity = elasticity
p.Friction = friction
p.isFixed = isfixed
p.Mass = mass
Return p
End Sub
 

Jim

Member
Licensed User
Longtime User
I was unable to get collision detection to work even though the group was collidable with the other group. I also tried setting isSolid and mass = 1 to see if that would have any affect. Applying forces seems to work.
 

aggelos

Member
Licensed User
Longtime User
hello,just my opinion.

I tried the library.
the demo is working as expected.

the problem is when i tried various collisions using 3 wheels the
results are not the expected.
objects collide and the paths/trajectories are not correct
they seem unnatural.

an example of an error is that when 2 wheels collide under specific poitions
and conditions they go towards the same direction.
this is not normal since the objects have the same mass and the mass
is taken into calculation(yes i set the parameter to true,and tried different
values for elasticity and friction)

Maybe all your calculations are correct.
but still the result seems unnatural.


always friendly
Angelos
 

PaulR

Active Member
Licensed User
Longtime User
Thanks for this library, it's great!

One issue I have is that when particles are moving too fast the collision detection doesn't always work and they can end up on the other side of a particle it should have collided with.

The only feature request I would have would be per pixel collision detection to allow for different shaped sprites to be used with accurate collision detection.
 
Top