Games Useful Formulas

wonder

Expert
Licensed User
Longtime User
Last edited:

ilan

Expert
Licensed User
Longtime User
how to calculate distance between 2 points:

B4X:
Sub distance(x1 As Float, x2 As Float, y1 As Float, y2 As Float) As Double
    Return Sqrt(Power(x2-x1,2)+Power(y2-y1,2)) 'simple distance calculation
End Sub
 

wonder

Expert
Licensed User
Longtime User
how to calculate distance between 2 points:

B4X:
Sub distance(x1 As Float, x2 As Float, y1 As Float, y2 As Float) As Double
    Return Sqrt(Power(x2-x1,2)+Power(y2-y1,2)) 'simple distance calculation
End Sub

Don't use the Power() function:
https://www.b4x.com/android/forum/t...ncesquared-for-ispointincircle-formula.78453/

Correct version:
B4X:
Sub distance(x1 As Float, x2 As Float, y1 As Float, y2 As Float) As Double
    Dim dX = x2 - x1 As Double
    Dim dY = y2 - y1 As Double      
    Return Sqrt((dX * dX) + (dY * dY)) 'simple distance calculation
End Sub

Also, the distance formula should only be used when you really have to know the distance value between two bodies.
For comparisons, always used DistanceSquared.
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
Is a point inside a polygon:

Converted from a C routine taken from here: http://www.codeproject.com/Tips/84226/Is-a-Point-inside-a-Polygon

There is an explanation of it's workings and it's license in the link.

B4X:
Private Sub pnpoly(nvert As Int, vertx() As Int, verty() As Int, testx As Int, testy As Int) As Boolean
    Dim i, j As Int
    Dim c As Boolean = False
    j=nvert-1
    For i = 0 To nvert -1
        If (((verty(i) > testy) <> (verty(j) > testy)) And (testx < (vertx(j) - vertx(i)) * (testy - verty(i)) / (verty(j) - verty(i)) + vertx(i))) Then
            c = Not(c)
        End If
        j=i
    Next

    Return c
End Sub
 
Last edited:

Informatix

Expert
Licensed User
Longtime User
Is a point inside a polygon:

Converted from a C routine taken from here: http://www.codeproject.com/Tips/84226/Is-a-Point-inside-a-Polygon

There is an explanation of it's workings and it's license in the link.

B4X:
Private Sub pnpoly(nvert As Int, vertx() As Int, verty() As Int, testx As Int, testy As Int) As Boolean
    Dim i, j As Int
    Dim c As Boolean = False
    j=nvert-1
    For i = 0 To nvert -1
        If (((verty(i) > testy) <> (verty(j) > testy)) And (testx < (vertx(j) - vertx(i)) * (testy - verty(i)) / (verty(j) - verty(i)) + vertx(i))) Then
            c = Not(c)
        End If
        j=i
    Next

    Return c
End Sub
I have already a lot of these formula already written (in the math classes of libGDx for example). I want to select only the most useful.
 

stevel05

Expert
Licensed User
Longtime User
I have already a lot of these formula already written (in the math classes of libGDx for example). I want to select only the most useful.
That's fine. This one is useful outside of libGDx and can now be found (it is part of my Hotspot library)
 

Informatix

Expert
Licensed User
Longtime User
@Informatix, I know that this can be a lot of work, but what do you think about QuadTree implementation for smart collision detection?
I remember now that I implemented a quadtree in SteeringBehaviors a long time ago, but I finally removed the code because the performance gain was only theoretical. Maybe another algorithm like R-Tree would be better. Anyway, I tested the brute force approach for the collision of hundreds of different shapes and I run at 60 fps on my not-very-fast PC at work, so is there really a need for such an algorithm?
 

wonder

Expert
Licensed User
Longtime User

andymc

Well-Known Member
Licensed User
Longtime User

Informatix

Expert
Licensed User
Longtime User
Well... one of these days, I want to make a game like Epic Battle Simulator. :cool:
So, brute forcing 450,015,000 [ (30,000 * (30,000 + 1)) / 2 ] collision tests would be a little bit too much, no? :D
I decided there won't be a Quadtree algorithm (or similar) in SimpleGameEngine because there's no real need for it. SGE can check collisions for hundreds of polygons without losing one FPS, so... And there's jBox2D for more advanced uses and more demanding needs.
 

ilan

Expert
Licensed User
Longtime User
From your point of view, what are the most useful formula/computations for a game developer? I will add them to SimpleGameEngine. I think that good candidates are intersection computations (does my circle intersect my box?).

I am not sure if the question is pointed to wonder or to anyone but a good and importent formula would be to get the centroid of an polygon

https://en.m.wikipedia.org/wiki/Centroid

If 2 polygons intersect is ofcourse a great feature to have. I wont use it for collisions detection because i dont see the point to try to make your own collisions detection if you have box2d (jbox2d) but it would be good for a puzzle game. (I needed it for my Power Blocks game)
 

Informatix

Expert
Licensed User
Longtime User
I am not sure if the question is pointed to wonder or to anyone

... to any potential user of SimpleGameEngine.

but a good and importent formula would be to get the centroid of an polygon

It is already the case. Look at the doc.

If 2 polygons intersect is ofcourse a great feature to have. I wont use it for collisions detection because i dont see the point to try to make your own collisions detection if you have box2d (jbox2d) but it would be good for a puzzle game. (I needed it for my Power Blocks game)
Box2D is perfect when you want to apply forces to objects and get a realistic behavior or when you want to handle complex simulations involving a lot of polygons. Otherwise, it's a layer of useless complexity. Why should we create a Box2D world with fixtures and bodies to simply know whether a missile hit the player's ship? Box2D is not extremely intuitive and drawing a sprite representing faithfully your Box2D object is not the easiest thing (my jBox2D demo is by far the most difficult demo I had to write for SimpleGameEngine; it took me one hour to solve the problems of scale and rotation).
 

ilan

Expert
Licensed User
Longtime User
Box2D is not extremely intuitive and drawing a sprite representing faithfully your Box2D object is not the easiest thing (my jBox2D demo is by far the most difficult demo I had to write for SimpleGameEngine; it took me one hour to solve the problems of scale and rotation)

This is true. Not like spritekit box2d is just a calculation framework and does not render anything. However you can solve those issues with creating some functions in a code module and store all information u need to your body userdata (this is what i did in my platformer game)

But in my opinion the main feature in box2d is not the physics. Its his collision detection.

You can know where 2 bodies collide (get the exact point) and in what force they collide so even if you want to use your own physics you always can set the body to "sensor" and just get the information when and where they have collided.

So i stil would use it for collision detection.
Get when a polygon intersect with another is not the only information you need in a complex game.

For example if you need to create 1 way walls you can do it by checking if all polygons points pass the wall and the velocity of the character is bigger then 0 then u change the wall to eccept collision... but with box2d its simpler.

Anyway i let everyone decide what is simpler for him. For me spritekit is way simpler and the logic is much better then libgdx and box2d.

I know you dont like apple much but i would recommend you to have a look how spritekit works and maybe implement some feature and logic in SGE.
 

Informatix

Expert
Licensed User
Longtime User
You can know where 2 bodies collide (get the exact point) and in what force they collide so even if you want to use your own physics you always can set the body to "sensor" and just get the information when and where they have collided.
...
Get when a polygon intersect with another is not the only information you need in a complex game.

I agree but that does not make the collision detection routines of SGE pointless. There are millions of games that uses such a collision system. My Diktatour game, for example, which is a complex game and where many things collide.
For me, Box2D is a great tool but it is not enough simple or intuitive to include it in my SGE library. That's why I let it as a separate library and provides a set of functions to handle collisions. If you need Box2D, use it. SGE doesn't prevent that, cf. the provided demo.

Note that we're speaking of SGE+jBox2D, which is a different matter from libGDX, which fully integrates Box2D and where Box2D is much easier to use.

Anyway i let everyone decide what is simpler for him. For me spritekit is way simpler and the logic is much better then libgdx and box2d.

I know you dont like apple much but i would recommend you to have a look how spritekit works and maybe implement some feature and logic in SGE.
Could you explain what's great in iSpriteKit? I'm very interested but I cannot try it (I don't have any device from Apple). If there are good ideas, I will take them for sure. However one thing is clear: I won't add a physics engine to SGE, just to make Box2D simpler. It's too much work and the SGE audience is very limited.
 

ilan

Expert
Licensed User
Longtime User
Note that we're speaking of SGE+jBox2D, which is a different matter from libGDX, which fully integrates Box2D and where Box2D is much easier to use.

ohh i was talking about box2d + libgdx to be way more complex then spritekit so if SGE is harder to use with jbox2d i understand why you add your own collisions system to SGE. (that will be for sure very useful in lot of games, as you mentioned)

Could you explain what's great in iSpriteKit? I'm very interested but I cannot try it (I don't have any device from Apple). If there are good ideas, I will take them for sure. However one thing is clear: I won't add a physics engine to SGE, just to make Box2D simpler. It's too much work and the SGE audience is very limited.

actually its the combination and logic how spritekit handle sprites and phsyic bodies. i fully understand why you wont invest to much work on making an own physic engine. but i could imagine how we can use jBox2d with a game engine and try to make it simple as spritekit works.

this is also not a simple and quick task but it will make game making much much simpler.

after using both iSpriteKit and Libgdx+box2d i will list the main reason why i would define spritekit as the big winner in simplicity.

1. create a simple template. in spritekit you just create an SkView and add everything to it. its like a big panel where you add it to your Page like you would add a panel. so you always can hide it or do what ever you want with it.

2. skscene: an skscene is also like a panel where you add childs to it (sknodes) so its like a page in your skview
if you have a menu you create an skscene and add all childs to it. now when you want to show the menu you just say

B4X:
    Dim GameView As SKView
    Dim MenuScene As SKScene

GameView.PresentScene(MenuScene)

this will present only the MenuScene and all other Scenes (GameScene, SettingScene, LevelSelectScene,...) are paused and you dont need to care about them at all.
its really simple like this to move from Game to Menu and then back to Level Selection Page,..

you can also do it with Transition:

B4X:
    Dim GameView As SKView
    Dim MenuScene As SKScene
    Dim fadeout As SKTransition

    fadeout.Initialize
    fadeout.FadeWithColor(Colors.Black,1)

    GameView.PresentScene2(MenuScene, fadeout)

now try to do it with libgdx and you will see you need to think about a lot of stuff !!

so creating a scene and showing it is way simpler!

3. in spritekit you create a node. a node can hold textures or can be a labelnode... but the awesome thing in sprite kit is that every node has his own physicbody.
even a gamescene has his phsyicbody. if you dont set it its like just a simple sprite but if you set it it will react to gravity and collisions.

so you dont create body and attach a image to it (like in box2d and libgdx) you create a SkSpriteNode for example. set his texture and use his own PhysicBody for your needs. you can set his physicbody very simple as a circle or polygon or rect,... but you dont need to bother the size and the rendering of the image.

the center of the SkSpriteNode and PhysicBody is the Same Center as his Texture so the x/y of both are the center of the node. this means that the image will always be a rect but the body can be what ever you want and everytime the body rotates the texture rotates with him. you dont write a single code to do it.
SpriteKit takes the x/y and zRotation of the Node and if the physicbody rotates it rotates together with the texture.

this is really awesome!

even the Skscene has a physicbody. in my iSpriteKit Pong Game i created an PhysicBody for the SkScene from his Frame size. so if the ball hits the frame it will collide with it

B4X:
    Dim GameScene As SKScene

    Dim body As SKPhysicsBody
    body.Initialize
    body.BodyWithEdgeLoopFromRect = GameScene.Frame
    GameScene.PhysicsBody = body

scr1.jpg


look how simple it is :)

4. in spritekit you can create very simple multiple bodies and attach them to the same SkSpritenode.

this is how i create a Pipe in my new Game

pipe.png


the 2 yellow lines are 2 physicbodies that are attached to the same SkSpriteNode. so what means if i rotate the node the texture rotate together according to the SAME centerPoint.
you also can change the AncherPoint but i had no use for that until now.

so very very simple.

5. u can create SKLabelNode and use it as a label but the great thing is that it also has a physicbody so you can apply force, impulse... to it and move the label on your screen like its a ball. (with some imagination you can find very useful ways you can use a label with a physicbody)

6. the new spritekit update has new cool features like wrap transition that allow you to wrap your skspritenode and can give you a very nice effect without a need of creating a sprite animation.

(scroll to minute 34:45)



there are a lot other feature that makes the whole framework simpler to use. i just wrote few of them.
 
Last edited:
Top