Collision bit reaches its maximum value while Initialization in LIBGDX - libgdx

I am using Category_bits for detecting collision in LIBGDX. I have used all the values ranging from 0 to 16384 . Since we have to use the power of 2 while initializing the values, I have exceeded the limit and I am not able to initialize any bits anymore. The range for short_ bit is 0 to 16384 and if I use the next multiple of that number, after type casting it to a short value, the Category_bits doesn't detect collision between objects. Nothing happens when the object collides when I set the value greater than 16384.
Here is how I initialize the values,
public static final short x = 0;
public static final short y = 1;
public static final short z = 2;
public static final short a = 4;
public static final short b = 8;
public static final short c = 16;
.....
public static final short d = 16384;
public static final short e = (short) 32768; // exceeded the limit so type casted the number to short
When I type cast and use it, nothing collision detection happens. I have to detect collision in many things in my game so I need the solution to get rid of this problem. Please help. Thanks in advance.

I would strongly advise re-checking over your code and decide whether you actually need that many different categories. Even if you have that many different types of objects I suspect you could group together those which have the same collision filters into just one bit, you can then use other ways of identifying which "type" of entity is involved in a collision.
If you do really need to use that many different objects then the Box2d World class has a method setContactFilter(ContactFilter filter) which will allow you to specify a custom ContactFilter. This contains a shouldCollide(Fixture a, Fixture b) which returns true if a and b should collide and vice versa.

short also worked for me with minus numbers, because it reaches from -32768 till 32767 so you can also use
-32678, -16384 and so on.
I don't know how to use the contact filters, but I work with UserData which also works pretty good for me.
when you create your body I wrote something like this:
b2body.createFixture(fdef).setUserData("something");
and in my collision detection I used something like this:
public class WorldContactListener implements ContactListener{
#Override
public void beginContact(Contact contact) {
Fixture fixA = contact.getFixtureA();
Fixture fixB = contact.getFixtureB();
int cdef = fixA.getFilterData().categoryBits | fixB.getFilterData().categoryBits;
switch(cdef) {
case BreedingSeason.HERO_BIT | BreedingSeason.TRAMPOLIN_BIT:
if(fixA.getUserData() == "somethingElse" && fixB.getUserData() == "something") {
...
} else if(fixB.getUserData() == "somethingElse" && fixA.getUserData() == "something") {
...
}
break;
}
and don't forget the break after each case in the switch ;)
i hope this helps you

Related

Stack overflow on recursive function

got a Problem with recursive funktions. I made this one in java, that is just pretty basic, but doesn't work tho, due to an Stack overflow error. I mean what this function does is to open the funktion just as often as the size of the difference between a given number and the number you declare in the main funktion, what should really not be a problem for the stack, but well, doen't work the whole time, or whats the mistake here...?
thanks for the answers in advance :)
public class Übung_Baeume {
static int anzAufrufe=0;
static int zahl=23;
public static int zaehleAufrufe(int uebergabe)
{
anzAufrufe++;
if (uebergabe==zahl){
return anzAufrufe;
}
return zaehleAufrufe(uebergabe-1) +
zaehleAufrufe(uebergabe+1);
}
public static void main(String[] args) {
System.out.println(zaehleAufrufe(40));
}
}
ubergabe if not equal to 23 will recurse with ubergabe +1 and unbergabe - 1. Now each of those will do the same so you can just try this out:
zaehleAufrufe(40) ; ==>
zaehleAufrufe(39) + zaehleAufrufe(41) ; ==> neither of these are 23
zaehleAufrufe(38) + zaehleAufrufe(40) + zaehleAufrufe(40) + zaehleAufrufe(42)
Notice that last one.. Even though some of these eventually will hit a base case you see that you on the 3. expansion have 2 zaehleAufrufe(40). Each one of these expands like the above turning also into two zaehleAufrufe(40) and no one of these will even hit a base case.
For recursion to work you need to become simpler problems and in fact yours become several of the same amount and thus infinite recursion.
To open a function as many times as the difference you only recurse once:
public static int zaehleAufrufe(int uebergabe)
{
anzAufrufe++;
if (uebergabe <= zahl) {
return anzAufrufe;
}
return zaehleAufrufe(uebergabe-1);
}
zaehleAufrufe(40) ; ==>
zaehleAufrufe(39) ; ==>
...
zaehleAufrufe(23) ; ==> 18
This almost always means that nothing can stop the recursion from going deeper and deeper. There is no condition that stops when a certain level is reached whether the goal is achieved or not.
In your code you start from 40 and will stop only when you get to 23. But one of your branches is increasing the number:
return zaehleAufrufe(uebergabe-1) + zaehleAufrufe(uebergabe+1);
and will never go down to 23.
Welcome to StackOverflow with a stack overflow :)
P.S. The best thing to do is to reconsider your algorythm. If in a case you are sure you want to use a recursion, but it's branching is unpredictable due to depending on unknown data, you can put a level-limiting value. It is a dirty hack but there are cases when it is useful.
It is importaint to say that with this limit your code will still fail
- it will try to call this function as much as 2^33 times = about 8 billion, which is big enough :)
public class Übung_Baeume {
static int anzAufrufe=0;
static int zahl=23;
static int max_level = 32;
static bool fault = 0;
public static int zaehleAufrufe(int uebergabe, int level)
{
if(level == max_level)
{
fault = 1;
return 0;
}
anzAufrufe++;
if (uebergabe==zahl){
return anzAufrufe;
}
return zaehleAufrufe(uebergabe-1, level+1) +
zaehleAufrufe(uebergabe+1, level+1);
}
public static void main(String[] args) {
int ret = zaehleAufrufe(40,0);
if(fault == 0)
System.out.println(ret);
else
System.out.println("Fault - recursion level limit reached!");
}
}

Pattern creation of collectibles - LibGdx

I am working on a LibGdx running game.I have collectibles/coins in the game.
I have created a coin array,and spawned it throughout the game.
To improve the play,I want to make patterns of coins.Like 2 coins or 3 coins together , coins in vertical or diagonal arrangements etc.
I tried to implement this,but found it a difficult task as a fresher.
Please suggest me some code to implement patterns like triangle with an array of objects.
Like this:triangle with 3 coins in equal distance:
My coin array and method are included here:
I am adding new coins on the basis of last coin passes a particular distance on the screen.
Array<Coin> coins = new Array<Coin>();
private void createCoin() {
coinObj = objectFactory.createCoin(CoinEnum.random());
coinObj.isCollided = false;
coins.add(coinObj);
}
private void spawnCoin() {
if (coins.size == 0) {
createCoin();
} else {
Coin c = coins.peek();
if (c.getY() > (Constants.WORLD_HEIGHT / 8))
createCoin();
}
// remove out of screen coins
if (coins.size > 0) {
Coin cc = coins.first();
if (cc.getY() > Constants.WORLD_HEIGHT) {
coins.removeValue(cc, true);
}
}
}
Also hope someone can tell me the mistakes in my approach if any!
Thanks in advance!
First of all, try to model the CoinPattern:
- What is a CoinPattern?
It is just a pattern, describing an arrangement of multiple coins.
- What do I need to describe it?
As it is just a pattern, you don't need no Coins yet.
In my opinion, a list of Points (or Vector2) should be enough.
Each of these Points could describe the relative Position of the Object (in your case Coin) inside the Pattern.
Now you could create constants for your Patterns. The triangle could look something like this:
public static final PATTERN_TRIANGLE = new Vector2[] {
new Vector2(0,0),
new Vector2(1,0),
new Vector2(0,1),
};
Then you could create a method spawnPattern(Vector2[] pattern, int x, int y). This method should then create a Coin for every Vector2 in the pattern.
The position of each Coin could be calculated like this:
int posX = x + pattern[i].x;
int posY = y + pattern[i].y;
Note, that using this methode, the positions of the Coins are relative to the lower, left corner of the Pattern position.

How to tween alpha of a BitmapFontCache in libgdx?

I am animating some text in my libgdx application and would like a label text to fade-in and move (e.g. similar to this jsfiddle).
I can move, and change alpha of other objects (e.g. Sprites) and can move BitmapFontCaches. However I can't get alpha of the BitmapFontChage to change.
Declaration:
message = new BitmapFontCache(messageFont, true);
message.setWrappedText("some text", 10.0f, 10.0f, 10.0f);
message.setAlphas(0.0f);
In my screen class, I override the render method, and call .draw() on a renderer class, which is (among other things) essentially just a message.draw(batch); call.
#Override
public void render(float delta) {
...
try{
batch.begin();
feedbackRenderer.draw(batch);
tweenManager.update(delta);}
finally{
batch.end();
}
}
Then as a part of a timeline I call these two Tweens. (yes, they are wrapped in .push( ) and I do start my tweenManager:)
Tween.to(message, BitmapFontCacheAccessor.POSITION_X, animationDuration)
.target(35.0f)
Tween.to(message, BitmapFontCacheAccessor.ALPHA, animationDuration)
.target(1.0f)
The BitmapFontCacheAccessor tries to setAlphas() of the BitmapFontCache as such:
public class BitmapFontCacheAccessor implements TweenAccessor<BitmapFontCache> {
public static final int POSITION_X = 1;
public static final int ALPHA = 2;
#Override
public void setValues(BitmapFontCache target, int tweenType, float[] newValues) {
switch (tweenType) {
case POSITION_X:
float y = target.getY();
target.setPosition(newValues[0], y);
break;
case ALPHA:
target.setAlphas(newValues[0]);
break;}
}...
It moves the label (==> .setPosition(x, y) works!), but does not even touch the alpha. This exact same approach works for Sprites, which fade in nicely.
Is there perhaps some catch when tweening alpha for the BitmapFontCache? Is it possible?
Many thanks!
After a good hour of debugging I have found the reason for this funny behavior.
Libgdx's BitmapFontCache does not have a getAlphas() method
Therefore, to get the alpha channel I used getColor().a
However, these two are not always synced. The behavior is quite random, I myself am not quite sure when it syncs and when it doesn't (f.ex. in the question above, the fade-outs would work, but fade-ins wouldn't)
The solution is to change and declare BOTH alpha channels.
Definition of BitmapFontCache:
message = new BitmapFontCache(messageFont, true);
message.setColor(1,1,1,0);
message.setAlphas(0);
and inside TweenAccessor:
case ALPHA:
//first alpha channel
target.setAlphas(newValues[0]);
//second alpha channel
Color c = target.getColor();
c.a = newValues[0];
target.setColor(c);
break;
To you, hopeless SO wanderer, I address this answer so that you can spend some of the finite number of minutes of your life better than I did.

Pointer to a function with Android

I have a problem to solve with Android, but it's really confusing.
Using the function below:
function accumulate(combiner, nullValue, list){
if(list.length == 0){
return nullValue;
}
var first = list.removeFirst();
return combiner(first, accumulate(combiner, nullValue, list));
}
Develop the function sumOfSquares which returns the sum of squares of a list (Example: 1² + 2² + 3²...)
sumOfSquares([1,2,3,4,5])
returns the number 55.
In this case, the function accumulate must be used. The variable "combiner" is a "pointer to a function". The implementation of the function "combiner" is part of the solution.
I have no problem with the basics, doing the sum of squares, etc, but the part "pointer to a function" really confused me.
If anyone can tell me which is the way to get to the answer, I will be thankful :)
I have done until the code below:
public class MainActivity extends Activity{
protected void onCreate(...){
....
List<Integer> list = new ArrayList<Integer>();
//Fill the list with values
long value = accumulate(sumOfSquares(list), 0, list);
//Show the value
}
private int sumOfSquares(List<Integer> list){
int sum = 0;
for(int i = 0; i < list.size(); i++){
sum += Math.pow(list.get(i), 2);
}
return sum;
}
private long accumulate(int combiner, long nullValue, List<Integer> list){
if(list.size() == 0){
return nullValue;
}
int first = list.get(0);
list.remove(0);
return combiner(first, accumulate(combiner, nullValue, list));
}
private long combiner(int first, int rest){
return first + rest;
}
}
In some languages, the notion of a pointer to a function makes sense, and you could write the code pretty much as you've given it in the example. Not in Java, though, which is what underlies Android. (Android is a bit of a weird choice for this, by the way...)
What you want to do in Java (without giving you the whole solution) is to define a
private int combiner(int first, int rest);
method that takes the first element of the list and the solution to the smaller problem defined by the rest of the list, and produces the answer from these two bits. In other words, if first is the first element, and rest is the sum of the squares of everything except the first element, what is the sum of the squares of the whole list (in terms of first and rest)?
Now your accumulate method does almost exactly what you've written out. It just removes the first element, recursively calls itself on the rest of the list, and returns the value of combining the first element with the result of the recursive call.
The nullValue is there to give you the sum of the squares of an empty list.
If you want to look up more of the details of the theory, you're basically doing functional programming but in an imperative language :)

Passing Vector.<T> references as Object, casting causes a copy to be made

I'd like to be able to pass Vectors around as references. Now, if a method takes a Vector.<Object>, then passing a Vector.<TRecord>, where TRecord inherits directly from Object does not work. Where a method takes just plain Object; say vec: Object, then passing the Vector is possible. Once inside this method, an explicit cast at some stage is required to access vec as a Vector again. Unfortunately, a cast seems to make a copy, which means wrapping one up in multiple Flex ListCollectionViews is useless; each ListCollectionView will be pointing to a different Vector.
Using Arrays with ArrayCollection presents no such problem, but I lose out out the type safety, neatness (code should be clean enough to eat off of) and performance advantages of Vector.
Is there a way to cast them or pass them as references in a generic manner without copies being made along the way?
Note in this example, IRecord is an interface with {r/w id: int & name: String} properties, but it could be a class, say TRecord { id: int; name: String} or any other usable type.
protected function check(srcVec: Object): void
{
if (!srcVec) {
trace("srcVec is null!");
return;
}
// srcVec = (#b347e21)
trace(srcVec.length); // 4, as expected
var refVec: Vector.<Object> = Vector.<Object>(srcVec);
// refVec = (#bc781f1)
trace(refVec.length); // 4, ok, but refVec has a different address than srcVec
refVec.pop();
trace(refVec.length); // 3 ok
trace(srcVec.length); // 4 - A copy was clearly created!!!
}
protected function test(): void
{
var vt1: Vector.<IRecord> = new Vector.<IRecord>; // (#b347e21) - original Vector address
var vt2: Vector.<Object> = Vector.<Object>(vt1); // (#bbb57c1) - wrong
var vt3: Vector.<Object> = vt1 as Vector.<Object>; // (#null) - failure to cast
var vt4: Object = vt1; // (#b347e21) - good
for (var ix: int = 0; ix < 4; ix++)
vt1.push(new TRecord);
if (vt1) trace(vt1.length); // 4, as expected
if (vt2) trace(vt2.length); // 0
if (vt3) trace(vt3.length); // vt3 is null
if (vt4) trace(vt4.length); // 4
if (vt1) trace(Vector.<Object>(vt1).length); //
trace("calling check(vt1)");
check(vt1);
}
This is not possible. If a type T is covariant with type U, then any container of T is not covariant with a container of type U. C# and Java did this with the built-in array types, and their designers wish they could go back and cut it out.
Consider, if this code was legal
var vt1: Vector.<IRecord> = new Vector.<IRecord>;
var vt3: Vector.<Object> = vt1 as Vector.<Object>;
Now we have a Vector.<Object>. But wait- if we have a container of Objects, then surely we can stick an Object in it- right?
vt3.push(new Object());
But wait- because it's actually an instance of Vector.<IRecord>, you can't do this, even though the contract of Vector.<Object> clearly says that you can insert Object. That's why this behaviour is explicitly not allowable.
Edit: Of course, your framework may allow for it to become a non-mutable reference to such, which is safe. But I have little experience with ActionScript and cannot verify that it actually does.