LibGDX / Box2D: How to keep track of active Contacts between bodies? - libgdx

I'm making a 2D (side view) car game with LibGDX using Box2D. The car has a front wheel and a rear wheel and it's "AWD". I would like to render a dust effect to each point where a wheel touches the ground when the player is "burning rubber". To do this, my plan was to
Implement ContactListener, and in beginContact(Contact contact) method find the Contacts where one of the fixtures is ground and the other one a wheel of the car.
Save these Contacts to a (wheel-specific) array, which is supposed to contain the active Contacts of the wheel (the wheel can touch the ground in multiple points, hence an array is required).
During each render call, for each Contact in each wheel's array: Get the contact position, and calculate velocity difference between the wheel body and the ground body at that position. If the length of the velocity difference vector is greater than a certain threshold, then draw the dust effect (using the velocity difference vector to define the speed and angle of the dust particles).
When endContact(Contact contact) of my ContactListener is called, remove contact from the array.
The problem is that LibGDX appears to use the SAME Contact instance for every Box2D contact, rewriting the existing Contact even if it's still active! This means, for example, that if the rear wheel touches the ground first and the front wheel after that, the Contact instance I saved to the rear wheel's contact array gets overwritten with front wheel's contact properties. Another problem is that when I receive endContact, I don't know which Contact really ended.
Have I understood something wrong, or is this how LibGDX is supposed to behave? Is it possible to somehow adjust this behavior, or is there an easy workaround?
(One option that comes to my mind is to keep listening to beginContact and endContact calls and just keep track of the ground bodies/fixtures that are touching each wheel, but then I would have to calculate the contact points manually each frame, which sounds somewhat difficult, expensive, and redundant - because Box2D anyway calculates it at each world step).

Maybe you can create a MyContactListener implementing the ContactListener class.
The MyContactListener class contains a MyContactInventory object which is a Singleton object containing your Contact Array and a couple of convenient method:
addContact
removeContact
The beginContact and endContact add/remove Contacts to your Array using the convenient methods above.
Then you call world.setContactListener( new MyContactListener());
Hope it helps!

Related

How to zoom and pan a gameobject not the whole scene in unity

I have several images in my scene, I managed to make the zoom and pan work but the problem is, All of the images are zooming in at the same time. The scripts are place on each gameobject. I used gameobject for the images.
You need apply a filter so you just appli the transformations to one game object, since every one of them are GameObjects I recommend use raycast to sellect an object and then activate his Zoom.cs script just for this object.
I sugest to use just one Zoom.cs in the scene instead of every object having his own copy, then change your function to work with one game object as a paremeter and, when you touch your game object then that object will be the one to suffer the transformation.
If you doesn't want to implement a raycast or change your function at all, then you could set all the images inside a matrix, or use an index system, the central image, or the index you designed, will be the one to suffer the transformations. so you can scroll betwen your gaery and be sure that just the image that you designate in your matrix/index will be the choosen one. The trouble here is that you need to fully control the scroll animation so no one of the images will be in a wrong place like in between two index.

Level part changing in an entity system based game

I am currently working on an entity system based game using artemis and libgdx and I am wondering how I should handle level changing in such a configuration.
For example, with a Mario-like platformer:
First Mario starts the level, the engine instantiates a new World() loading the tilemap and initializing all the objects.
What if Mario goes through a pipe? The world inside the pipe is a new World()? or does the camera only focus on another part of a world?
If we have a new world, artemis doesn't allow detaching an entity from a world, so may I clone the player entity and add it to the new world?
Does someone know how to handle this kind of level part changing (Mario's part / Abe's odyssey screens)? What are the common ways to achieve this using an artemis-like framework?
Think about decoupling the building of each particular level so that your world object can take in a specific level that could either read in the position of entities like the mario blocks and pipes or just define them in that specific level class. Allow your world to set new level as a method or you could create a new world object each time and unattach the old one for garbage collection. For something like the world in the pipe if you didn't want to have to load it while the player was already in the level than you could build the room or area somewhere off in the distance and use the pipe to teleport the character to a spot inside the room.
I would create some LevelTagComponent and add it to all entities that are level-specific. Custom Manager should do the rest - turn off some entities when Mario goes into a tunnek or remove them when level is being completely changed.
I finally decided to add a room object layer defining rectangles (see picture below)
Each room is a rectangle with it's x, y coordinate and width, height dimensions. Each room defined the bounds where the camera is allowed to move, if the player intersect with another room, the camera slide from room n to room n+1 along the x axis or the y axis, if the room is bigger than the screen, the camera is allowed to scroll.
I have been able to improve performance and to solve a lot of issues using this approach, it is possible to only process the entities contained within the current room instead of calculating the full world at each frame.

Drawing all players in real time multiplayer Google Play

I am building a real time multilayer with Libgdx and Google play services BaseGameUtils and at a point where I am able to bring players into the waiting room and start the game but when I tested the game it only shows one player on the screen.
My question is:
is the game room (Google API) responsible for drawing all participants on the game room screen or do I have to implement it somehow in my game screen
Once the minimum required players have entered the room, this is how I start the game screen
public void startGame(){
Log.d(TAG, "startGame()");
gameActivity.setScreen(new Play(gameActivity));
}
I don't know much about networking things. But what i know is, that the connection is extremly limited, so try to send as few data as possible. For a 3D game maybe send a Vector3 position, Vector3 direction, to draw the player at the right position with the right rotation. For a 2D game use Vector2 as you don't have the z-Axis. Maybe you also need to send an int class, which defines which class the player has (Knight, Whizzard...).
So basicly you send only data for the logic, never objects, like models, textures...
The drawing is always performed on the client. The server does only some logic, like validating the positions...
LibGDX is not a part of Android, any Google API calls will not render content in a LibGDX game. Your calls might affect what is displayed in another application that relies on the same service (like the "Play Games" app). If you want to display something in your game you'll have to code that functionality yourself.

How do I create a simple on-mouse-click rotating object in flash

This question may get downvoted or go unanswered because it's not the greatest and incredible silly but anyway,
I'm taking an intro level Webanimation course this semester at UNI and had I know what their expectations are for our hand-in projects i would have never taken it up.
Basically the teacher taught us stuff to the extent of masking/tweening and very few mouse-event and basic function codes.
Now she is expecting us to make a god damn ENTIRE PIPE GAME. The one where there are a bunch of rotating pipes and you gotta rotate them in place before a timer runs out and then the water flows through them.
For this project I have to somehow figure out the following (even though she didn't teach any of this):
-creature a grid of rotate-able pipes (one mouse click I assume would do a 90 degree classic tween rotation of the object)
-creature some sort of logic hit-box value chain to make pipes decide when to fill with water (they fill with water (a.k.a turn blue inside as an animation) once they are connected to another water filled pipe, for example)
-creature multiple levels and a menu screen
-add a music track.
Now i know this site is for specific help only and you basically can't ask for help on an entire project, so for now if somebody could just help me out with the following:
How do I create a rotating pipe on mouseclick?
So I have my pipe movieclip created and I have my Mouse Event code ready but I don't have the faintest on how to make a tween within the pipe and connect it to the code so that it rotates on mouseclick.
So this far, let's say for one of the pipes, instance pipe_1, I want to do this:
pipe_1.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
trace("Mouse clicked");
}
I also have the simple tween of rotation already created within the instance of the pipe, but dunno how to connect it to code.
I'm supposed to figure out what to put inside the function but I honestly have no clue. Hours of googling have come up with nothing either except a 12 dollar purchasable source code for an even more complicated pipe game.
I hope somebody can at least help a bit, and thanks.
The way to rotate a clip is via it's rotation property. It defaults to 0.
If you were set the rotation property of your tile to 90, you'd rotate your pipe tile 90 degrees.
for example :
pipe_1.rotation += 90;
A tween is a means of changing a property of a given DisplayObject over time. So what you want to do is tween your rotation property 90 degrees over time.
Here is a tutorial on Tweening - http://www.republicofcode.com/tutorials/flash/as3tweenclass/
I think it'd be more beneficial for you to take the time to learn about it, than to have me just write a few lines of code to solve your problem.
StackOverflow is a place where you can ask a question, AFTER you have tried something and have hit an issue.
I have provided you with the basic concept of what you need to do, and if you take the time to learn about tweening, you'll be able to achieve your goal rather simply.
There are also tweening libraries such as TweenLite and TweenMax that simplify tweening. Not sure if your class will allow you to use them, but worthwhile to check out for your own benefit.
You can find TweenLite here :
http://www.greensock.com/tweenlite/
Are you talking about a frame by frame tween? or tweening with code?
for frame by frame tweening, you can try to do this:
pipe_1.addEventListener(MouseEvent.CLICK, f1_MouseClickHandler);
function f1_MouseClickHandler(e:MouseEvent) {
pipe_1.gotoAndPlay(2); //if the tween starts at frame 2
}
For code tweening, just call the tween function inside that handler function

How can I get the object Type from a HitTest?

For practice, I'm coding a really simple 2D platforming game in Flash using AS3. I'd like to define two different types of terrain surfaces that the player can walk on based on classic platforming elements. Type1: the player can walk on, and if the player jumps, they will hit their head on it and bounce back to the ground. Type2: the player can also walk on, but if the player jumps and hits their head, they will simply pass through the surface and not bounce back to the ground.
I am using hitTestPoint to resolve collisions for this. My question is: What would be the best method to test for what TYPE of ground I am colliding with? Each ground type has it's own Class associated with it in my Flash IDE and all the different terrain surface types are in the same movie clip on the stage.
Currently I'm testing to see if it hit one type of ground surface, then i'm testing if it hit the other, and then based on those results, I process what I want to happen. This seems to work okay right now, but I'm imagining that I may want to create more than 2 types of ground to collide with. For example, moving platforms. It seems like the code will start to get complex
Eg.
if(_groundType1.hitTestPoint(_player.x, _player.y, true))
{
if(_groundType2.hitTestPoint(_player.x, _player.y, true))
{
//don't hit the players head
}
//hit the players head
}
Try using instanceof - http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary389.html