Games Useful Formulas

wonder

Expert
Licensed User
Longtime User
ps. i hope @wonder is not mad at me that i turned his thread to a real chaos
Not at all, I'm always happy when one of my threads turns into a meaningful discussion.
 

ilan

Expert
Licensed User
Longtime User
Sprites can move like vehicles. I saw in a video zombies moving in a realistic way when they turn (smooth curve, not a sharp turn), facing the direction of the move. How do you do that?

i think i misunderstood your question @Informatix.
after reading it several times i understand that you ask how i could move an sknode on my screen like a car where i apply force to it and then if i want to turn right left it should turn smooth?? and not how to move sknodes automatically on a path right?

so i would do it with a simple function

i would apply a force to the skspritenode physicbody according to his zrotation. (angle of the node)

so something like this:

B4X:
    car.PhysicsBody.ApplyForce(createVector(car.ZRotation))

Sub createVector(angle As Float) As Vector
    Dim rad2angle As Float = (180/cPI)*angle
    Dim v As Vector
    v.Initialize(Sin(rad2angle)*speed,Cos(rad2angle)*speed)
    Return v
End Sub

now when i rotate the car by clicking on left/right buttons, the force is applied to the car according to his zRotation (sknode angle in radians).
although the car front in the texture needs to be on angle 0 (so pointing up)
 
Last edited:

ilan

Expert
Licensed User
Longtime User
I use also actions in SGE and I have also a FollowPath action. After seeing the list of parameters of FollowPath in SpriteKit, I added a new parameter to my function (OrientToPath) to do the same.

Great! Its very awesome from you to listen to other user and try to make your libs better and simpler for them

Oh no. It's not the purpose of a joint. And what is the second body the joint should be connected to? There's none.

Actually i was thinking of a body that follow the path (without any physics) and a car is connected to him.

Yesterday i tried the 4 pathfollowing functions and they are really cool. The node rotates automatically according to the direction it moves. Unfortunatly the body must be not effected by gravity what means sharp curves.

I tried some different ways. I created a magnetic field and set the charge value of the body so it is attracted to the magnet field.
The magnet field is following the path and the body is following the magnet.

There are also other ways to do it (electric field, ....) i need to investigate it a little bit more but it is really easy to use.

I could not try the joint option since ispritekit crashes when i use joints so this bug need first to be fixed but i think it should work.
 

Informatix

Expert
Licensed User
Longtime User
With the same velocity, a human doesn't take a bend like a tank because of its shape, mass, legs, hips, etc., and a tank doesn't turn like a car, which doesn't turn like a motorbike, etc. To reproduce these realistic moves, agents in game may have "steering behaviors" associated to a few properties (mass, size, velocity...):
https://gamedevelopment.tutsplus.com/series/understanding-steering-behaviors--gamedev-12732
You can reproduce these behaviors by applying forces to Box2D objects, as you do in your example, but it's a lot easier to use behaviors as you don't have to compute anything.
 

ilan

Expert
Licensed User
Longtime User
Steering behavior is very interesting.
I dont know if there are built in behaviors in spritekit but it should not be to hard to implement them. (At least simple functions)

Like creating my own followpath function where the body is effected by physics.

Should be an interesting task to try.
 

ilan

Expert
Licensed User
Longtime User

hi,

B4X:
Private Sub pnpoly(nvert As Int, vertx() As Int, verty() As Int, testx As Int, testy As Int)

i understand that vertx() and verty() are the polygon path and tesx/testy is the point that is being checked (if its inside the polygon) BUT what is nvert As Int ??

thank you
 

stevel05

Expert
Licensed User
Longtime User
No reason, it was just a straight port of the c code.
 

ilan

Expert
Licensed User
Longtime User
No reason, it was just a straight port of the c code.

Yes i saw the original code i just would have done it differently.

I would use an arraylist of arrays of doubles()
And an array of doubles() for the point and like this use only 2 arguments but maybe i am wrong.
 

ilan

Expert
Licensed User
Longtime User
maybe something like this:

B4X:
    Dim point() As Double = Array As Double(120,80)
 
'...
    Dim polygon As List
    polygon.Initialize
    polygon.Add(Array As Double(0,0))
    polygon.Add(Array As Double(200,10))
    polygon.Add(Array As Double(180,200))
    polygon.Add(Array As Double(40,140))
    polygon.Add(Array As Double(0,0))
  
    Log(pnpoly(polygon,point))
'...

Private Sub pnpoly(polygon As List, point() As Double) As Boolean
    Dim i, j As Int
    Dim c As Boolean = False
    j = polygon.Size-1
    For i = 0 To polygon.Size -1
        Dim lpoint() As Double = polygon.Get(i)
        Dim lpoint2() As Double = polygon.Get(j)
        If (((lpoint(1) > point(1)) <> (lpoint2(1) > point(1))) And (point(0) < (lpoint2(0) - lpoint(0)) * (point(1) - lpoint(1)) / (lpoint2(1) - lpoint(1)) + lpoint(0))) Then
            c = Not(c)
        End If
        j=i
    Next
    Return c
End Sub

EDIT: actually i am using this for my Power Blocks game where i create lots of polygons and i need to know if they collide with each other. with libgdx i could not solve it so i used my own function.
 

Informatix

Expert
Licensed User
Longtime User
I would use an arraylist of arrays of doubles()
Arrays are faster than lists and use less memory.

EDIT: actually i am using this for my Power Blocks game where i create lots of polygons and i need to know if they collide with each other. with libgdx i could not solve it so i used my own function.
Did you look at the numerous functions of the Intersector class? It contains all you need for intersection between polygons.
 

ilan

Expert
Licensed User
Longtime User
Arrays are faster than lists and use less memory.

ok thanks, i didnot knew that

Did you look at the numerous functions of the Intersector class? It contains all you need for intersection between polygons.

yes i have and if you remember i also explained my problem with using libgdx math functions. they are very good but for my needs i could not use them

https://www.b4x.com/android/forum/threads/geo-zone-determination-point-in-polygon.53929/#post-427544

this is the game i used point in polygon function: https://play.google.com/store/apps/details?id=www.sagital.powerblocks
 

wonder

Expert
Licensed User
Longtime User
When will u have that streetfighter game finished.
You remember it!! Thank you so much!!

Hard to say... As it was my first big B4A project, the code is absolutely horrible, by today's standards. It lacks logical structure.
If I would ever release it, I would certainly not maintain it. That would be a nightmare.
Also, I'm using copyrighted assets... so... mmm... yeah....
I promise I will dust it off and re-upload it one of these days...
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…