Games linear velocity in B4i - X2

developer_123

Active Member
Licensed User
Longtime User
Hello!

I'm having a problem with the code in B4i exactly with the linear velocity function of my character. I have the code that I already created in B4A and B4J, where in a new character class in some events I need to confirm the linear movement in X or in Y independently (bw.body.linearvelocity.x = 1, or bw.body.linearvelocity.x = 3). In B4A and B4J it works, but when I run it on my iphone (the compilation does not cause a problem), my character simply does not move. I tried setting the full vector (X2.CreateVec2 (1,0)) but my character doesn't move either. Setting this up in the main game class does work on the iphone. The downside is that I already have the code created in the character class that works on android and pc, and it would be very tedious to rewrite all the code and re-establish the logic if I pass the velocity vector to the game class. Why doesn't this command work in my character class? I appreciate any comments!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You shouldn't modify this vector:
1600756129130.png


Assign a new vector.
 

developer_123

Active Member
Licensed User
Longtime User
You shouldn't modify this vector:
View attachment 100354

Assign a new vector.

Erel, thanks for your response, but then of create a new vector, how do I set this vector in the body? I was trying put this code bw.Body.LinearVelocity=X2.CreateVec2(1,0) (if this you mean create new vector), but when I run I get an error and the application closes (all this is done in the character class)

Error Logs
x2utils$ResumableSub_MainLoop.resume (java line: 847)
java.lang.RuntimeException: java.lang.NullPointerException
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:120)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
at anywheresoftware.b4a.objects.B2World$B2ContactListener.beginContact(B2World.java:267)
at org.jbox2d.dynamics.contacts.Contact.update(Contact.java:330)
.
.
.
If I create the new vector in the GAME class all works fine, but inside the character class appear the previous error logs....

I was thinking that for any reazon this only can set in the Game class, but cheking the Mario example, I can See this code in all of classes, so Which can be the drawback?
 
Last edited:

developer_123

Active Member
Licensed User
Longtime User
Apart from using a complete vector, it would be easier for me to give values to each component in X or Y. How could I do it without this error appearing? In case it is necessary to create a complete new vector I have no problem, I only ask for ease of writing.
 

developer_123

Active Member
Licensed User
Longtime User
java.lang.... anywheresoftware.b4a.BA ? Do you talk about B4a or B4i ?
I'm talking about of three, B4I, B4J and B4A. If you can see the messages above, I have the same code in B4A and B4J, and it works ok. But the same in B4i, don't move my character. I'm going to change all my code, because I don´t know which are the trouble.
 

developer_123

Active Member
Licensed User
Longtime User
Is working!!! Firstly I was trying put the vector creating a new one using bw.Body.LinearVelocity =X2.createVec2 (x,y) and does not work, but reading again the Erel response I check he cretate a vector like other variable, and then modify it. Using this way it works!! but..... Why in the mario example is not like that ??? Should be the same way, is say :


(the next code is inside the Enemy class )

B4X:
Public Sub HitFromTurtleShield_Start (ft As X2FutureTask)
    If HitState Then Return
    HitState = True
    bw.FlipVertical = True
    'zero the filter bits to cause the enemy to fall out of the screen.
    bw.Body.FirstFixture.SetFilterBits(0, 0)
    bw.Body.LinearVelocity = x2.CreateVec2(0, 5)            <--------------------- ???? Is not creating other vector like variable
    x2.AddFutureTask(Me, "Hit_End", 2000, Null)
    bw.mGame.HitEnemy(bw.Body.Position)
End Sub

Anyway is working, and I appreciate your help! Thanks!
 

developer_123

Active Member
Licensed User
Longtime User
Not sure that I fully understand. The code you posted in post #7 works, right?
Yes, this code es from Enemy class of Mario example, and obviusly works fine, but when I create the same logic in my own app does not works (is say change directly the vector from the character class). But if I create a new vector using "dim v as B2vec2=bw.Body.LinearVelocity.CreateCopy" and use this new vector to give the X and Y velocity works fine. I don't know why this behavior, but with your previosly support in post #2, now all works like I need!
 

ilan

Expert
Licensed User
Longtime User
Yes, this code es from Enemy class of Mario example, and obviusly works fine, but when I create the same logic in my own app does not works (is say change directly the vector from the character class). But if I create a new vector using "dim v as B2vec2=bw.Body.LinearVelocity.CreateCopy" and use this new vector to give the X and Y velocity works fine. I don't know why this behavior, but with your previosly support in post #2, now all works like I need!

i know there is a difference how b4i is handling classes then b4a but i am not sure if this cause the problem.
it is hard to give you support without seeing the code or at least an example that reproduce the issue you have.

try to make a simple exmaple project and post it here.
dim v as B2vec2=bw.Body.LinearVelocity.CreateCopy

btw why do you need do it like this? you should not put a reference to the body velocity and change the variable to change the body velocity. update the body velocity on the body directly and not on a referenced variable.

EDIT: you should store somewhere your bodies in a global variable like a list and then go through the list and update the body parameters and not create variables that store the bodies parameters and update those variables. you should really learn the logic how game making works. there are so many projects in this forum. try to learn the logic then everything will be much more simpler.
 

developer_123

Active Member
Licensed User
Longtime User
i know there is a difference how b4i is handling classes then b4a but i am not sure if this cause the problem.
it is hard to give you support without seeing the code or at least an example that reproduce the issue you have.

try to make a simple exmaple project and post it here.


btw why do you need do it like this? you should not put a reference to the body velocity and change the variable to change the body velocity. update the body velocity on the body directly and not on a referenced variable.

EDIT: you should store somewhere your bodies in a global variable like a list and then go through the list and update the body parameters and not create variables that store the bodies parameters and update those variables. you should really learn the logic how game making works. there are so many projects in this forum. try to learn the logic then everything will be much more simpler.
Hi ilan, thanks for your comments. I don't need to do it like that (dim v as B2vec2 = bw.Body.LinearVelocity.CreateCopy) in fact this was the solution I got from Erel's answer, initially I was setting it directly without variable as you suggest, but when executing it in b4i my character was not moving.

In effect I am creating the bodies in an array as global variable - mCharacter (quantity of characters) - (there are several characters with the same characteristics) and then checking the list to update the parameters. The variable thing was a solution that allowed me to make the algorithm work in B4i. I have been based on the examples of multiplatform games in X2 and thus I have achieved the first functions of my characters. Of course, as you say, I am learning step by step to implement the correct logic. I hope I can make significant progress if I continue to study the examples.
I will try to put the simple code of the algorithm I refer to.
 
Top