Android 2d game collision detection - java

I developed a 2d game in android studio using SurfaceView,
it's not complex in context of collision, just need to check collision between a moving-point and some static circles, for detect collision for one circle, I simply check if X of the point is between circle minX and maxX && point Y is between minY and maxY of circle.
So for checking collision in whole of the game, I repeat to check above code for all circles in each frame.
Game work so good when I have for example 10 circles, but if I add 30 circles, its FPS decrease so much and I face so much lag!
What should I do for this problem? should I use Box 2d physics ? what does it do for collision detection that games doesn't face lag problem even if there is so much objects which collide together?
Please help me with more detail, because I was wonder how a game engine work and decided to make a simple one, not only wanted to make and release a game(otherwise I could use a ready game engine).
Thanks in advance

As for how game engines do it, it is probably easiest to look at their source code directly, as at least one version of Unity can do it - you can find the source code of Unity here.
In your case, you could potentially trade off increased memory consumption by your app to make collision detections essentially constant-time regardless of the number of circles, as suggested by #SagarGautam in his comment.
What you can do to achieve this is store a 2D array of booleans indicating all map pixels, setting each element to true if it is inside a circle (as per your previous code) or false otherwise. Doing this during the loading phase of the level/map should be fine, because now during rendering you can just look up the pixel coordinates in the array and see if it is inside a circle or not.

In 3d game i use Colliders so check if there is any collider(mesh,box,etc.)
give tag to that objects.and identify them by tag
Example:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void OnCollisionEnter2D(Collision2D coll) {
if (coll.gameObject.tag == "Enemy")
coll.gameObject.SendMessage("collided", 10);
}
}

Related

How to use particle effects like mentioned?

I am developing a multi-platform game on android studio using libGDX library and java language.
The game requires the player to maneuver a main ship across 2D space using mouse input.
I am using the given ParticleEffect class in libGDX library to show its exhaust but there is a slight problem that looks like this...
This is the standing ship
and
This is when its moving upward
I need every particle to move only downwards respective to the ship regardless the ship is standing or moving.
This means that I need to add the change in (x,y) coordinates of ship into each particle of exhaust but the problem is that the (x,y) coordinates of that class are private and there is no function that lets me make any direct changes to the coordinates of individual particles.
How do I make that happen?
Not sure, if I understood the question right. But probably, you can solve your issue via marking the emitter as attached. In this case if you change draw coordinates of ParticleEffect from (x0,y0) to (x1,y1), then every particle, which is already emitted, will be moved by (x1-x0, y1-y0) as well.
See the screenshot:

Physics-based/Polynomial Animation with Box2d (Java)

I'm creating a 2d game using the Java version of Box2d.
I know that the engine is generally paired with a separate library to handle animation, but I'm looking for something simple, with a more rustic feel, so I'm considering handling the animation from scratch.
The animation I am trying to create is something for the collision of two objects.
I'm using openGl to handle the graphics, and I pretty much have everything set up with the fps and rendering, it's just a matter of tweaking the frames to make it look smooth.
I tried bliting images of some things that I drew myself and it looked atrocious. I think the main problem is that the animation frame images are not linked to the physics forces and so they don't blend well.
What I want to try next is a sort 'spark effect' using a small number of pixels that sort of resemble 'dust' exploding off of the colliding images. If you've ever played dxball, you may recall a similar effect when the ball collided with the paddle at high speed.
As you can see in the above image, the principles are fairly simple. I figure by creating a set of small bodies with fixtures, I can use the box2d engine to produce this effect. I already have code that grabs the point of collision for all bodies, and am familiar enough with parabolic functions that I pretty sure I can create the force direction.
I'm just not sure if there is a better way of doing this, or how to apply a polynomial function to a body like in the image.
Does anybody know how to achieve this functionality with box2d? Any built in methods or documentation that would be helpful?
As a point of reference, the following snippet of code demonstrates the drawing of a small, red box on all of the collision points.
for(Vec2 point : listener.worldmanifold.points){
glPushMatrix();
newpos = point.mul(30);
glTranslatef(newpos.x, newpos.y, 0);
glColor3f(1,0,0);
glRectf(-2, -2, 2, 2);
glPopMatrix();
}

AndEngine - Checking for Overlap with Sprite or Physics Body?

I'm trying to make it so when a sprite which is attached to a physics body overlaps another sprite on the level something happens. The second sprite is NOT attached to a physics body.
More specifically when the two sprites are overlapping I want the game to constantly check to see if the distance between the centers of the sprites is less than a certain amount or not. Then if the distance is small enough something will happen.
I'm trying to use collision checking as a way of optimizing the game so it doesn't have to constantly check distances between every single object of type A and B even if they're not even close. It will only check the distance when they're close enough to be overlapping.
Now what I am wondering is how can I do this? Is there a way to check collisions between sprites as part of AndEngine? Or would it be easier to attach a physics body to the second object also and then just use physics collision detection? But then if I do that can I make it so the collision will be detected but they won't actually physically "collide"?
Yes, see CollisionDetectionExample.java. I suggest you download the whole examples package, it is very useful in the absence of any documentation for AndEngine. Please note that collision detection is not pixel perfect, so it will still detect collisions of transparent parts of the Sprites. There is a library for that, but I am afraid it is outdated.

collision & touch + shoot methods for Android

I really need help. I am making a game app for my final year project. It is a simple game where you have to shoot a ball into a target by rebounding of walls or angled blocks. However i need help in 2 areas:
the shooting mechanism is similar to that of stupid zombies. There is a crosshairs where you touch on the screen to indicate which direction you want the ball to be shot at. On release the ball should move into that direction and hopefully gets into the target and if not gravity and friction causes it to come to a stop.
The problem is how do I code something like this?
I need the ball to rebound of the walls and I will have some blocks angled so that the ball has to hit a certain part to get to the target. The ball will eventually come to a stop if the target is not reached.
How can I make a method to create the collisions of the wall and blocks?
I have spent the last weeks trying to find tutorials to help me make the game but have not found much specific to the type of game I am making. It would be great if sample code or template could be provided as this is my first android app and it is for my final year project and i do not have much time left.
Thank you in advance
akkki
Your question is too generic for stack overflow no one is going to do your project for you. Assuming you have basic programming experience if not get books and learn that first.
Assuming you already chose Android because of your tag, and assuming 2d game as it is easier.
Pre requests:
Install java+eclipse+android sdk if you havent already.
Create a new project and use the lunar landar example, make sure it runs on your phone or emulator.
Starting sample:
The lunar landar has a game loop a seperate thread which constantly redraws the whole screen, it does this by constantly calling the doDraw function. You are then supposed to use the canvas to draw lines, circles, boxes, colours and bitmaps to resemble your game. (canvas.draw....) Lunar landar does not use openGL so its slower but much easier to use.
Stripping the sample:
You probably don't want keyevents or the lunar spaceship!
Delete everything in the onDraw function
Delete the onKeyUp, onKeyDown
Delete any errors what happen
Create a new
#Override
public boolean onTouchEvent(MotionEvent event){
return false;
}
Run it you should get a blank screen, this is your canvas to start making your game... You mentioned balls, break it down to what a ball is: A position and direction, create variables for the balls x,y direction_x and direction_y the touch event will want to change the balls direction, the draw event will want to move the ball (adding the direction x,y to the ball x,y) and draw the ball (canvas.drawCircle(x,y,radius,new Paint())) want more balls search and read about arrays. Most importantly start simple and experiment.
2 collisions
Collisions can be done in the dodraw function and broken down to: moving an object, checking if that object has passed where it is supposed to go and if so move it back before anyone notices.... There are many differently techniques of collision detection:
If your walls are all horizontal and vertical (easiest) then box collisions checks the balls new x,y+-radius against a walls x,y,width and height its one big if statement and google has billions of examples.
If your walls are angled then your need line collision detection, you basically have a line (vector) of where your ball is heading a vector of your wall create a function to check where two lines collide and check if that point is both on the wall and within the radius of your ball (google line intersection functions)
or you can use colour picking, you draw the scene knowing all your walls are red for example, then check if the dot where the new ball x,y is, is red and know you hit
Good luck, hope this helped a little, keep it simple and trial and error hopefully this gets you started and your next questions can be more specific.

OpenGL: Create a sky box?

I'm new to OpenGL. I'm using JOGL.
I would like to create a sky for my world that I can texture with clouds or stars. I'm not sure what the best way to do this is. My first instinct is to make a really big sphere with quadric orientation GLU_INSIDE, and texture that. Is there a better way?
A skybox is a pretty good way to go. You'll want to use a cube map for this. Basically, you render a cube around the camera and map a texture onto the inside of each face of the cube. I believe OpenGL may include this in its fixed function pipeline, but in case you're taking the shader approach (fixed function is deprecated anyway), you'll want to use cube map samplers (samplerCUBE in Cg, not sure about GLSL). When drawing the cube map, you also want to remove translation from the modelview matrix but keep the rotation (this causes the skybox to "follow" the camera but allows you to look around at different parts of the sky).
The best thing to do is actually draw the cube map after drawing all opaque objects. This may seem strange because by default the sky will block other objects, but you use the following trick (if using shaders) to avoid this: when writing the final output position in the vertex shader, instead of writing out .xyzw, write .xyww. This will force the sky to the far plane which causes it to be behind everything. The advantage to this is that there is absolutely 0 overdraw!
Yes.
Making a really big sphere has two major problems. First, you may encounter problems with clipping. The sky may disappear if it is outside of your far clipping distance. Additionally, objects that enter your sky box from a distance will visually pass through a very solid wall. Second, you are wasting a lot of polygons(and a lot of pain) for a very simple effect.
Most people actually use a small cube(Hence the name "Sky box"). You need to render the cube in the pre-pass with depth testing turned off. Thus, all objects will render on top of the cube regardless of their actual distance to you. Just make sure that the length of a side is greater than twice your near clipping distance, and you should be fine.
Spheres are nice to handle as they easily avoid distortions, corners etc. , which may be visible in some situations. Another possibility is a cylinder.
For a really high quality sky you can make a sky lighting simulation, setting the sphere colors depending on the time (=> sun position!) and direction, and add some clouds as 3D objects between the sky sphere and the view position.

Categories

Resources