Android collision detection accuracy - android

I have one Sprite and multiple bitmaps that work as bullets. Now the problem is that I want the bullet to go really fast but that again brings a problem to my collision detection function, The way it works is that every frame I create a Rect at both the enemy and the bullet and check for overlaps.
Now if a bullet goes really fast it like "jumps" from point to point and that means if a enemy is small and the bullet "jumped" over the enemy it wouldn't get noticed.
What I wanna know is if there is a way to like detect if a collision is going to happen between 2 moving objects or just look if a enemy is in the trajectory of the bullet.
I made the collision detection by looping through an array of enemies and inside of that for loop I loop all bullets and then create a rect for both and check for collisions with Rect.intersects for each enemy and bullet.
The bullets heading is measured by one fixed point and touch input and then calculated by this function:
public void calcPoint(float x, float y) {
double alfa = Math.atan(x / y);//x and y are inputs
bulletPointX = (float) Math.sin(alfa) * speed;
bulletPointY = (float) Math.cos(alfa) * speed;
}
I have no idea how to fix it and I'd like some adivice how to do it and maby an example...

There are several ways to do this, but the easiest is probably predictive collision detection. Basically, you split the movement into time segments and check for each segment. It may not be very efficient if you have a few thousand objects moving around, but for most cases it'll work just fine.
It's known by a few names, also. The link I gave can help with the basics, but to learn more, do a search for:
Frame Independent Collision Detection
Continuous Collision Detection
Sweep Testing

Related

Preventing body from too high acceleration

I am currently creating an Andengine game which features a ball and a blocker just like pong, except in my game the world and blocker aren't squares and are instead circles.
Right now when the ball bounces off of the blocker all physics seem to check out alright but when I move the blocker really fast to the destination and the blocker hits the ball the ball goes flying off the screen at insane speeds.
How would I go about keeping the ball's velocity at the same speed all the time while keeping current physics?
Don't remember exact code but the logic is:
in onManageedUpdate check the speed of your object, then if speed exceed some value you are happy with set the linear speed to this good value.
Other solution could be that you use ContactListener and in postSolve or endContact you set velocity to desired one.
Also check why this speed after bounce is that fast. Is it because a blocker is moving really fast? Does it also happen when blocker is moving slowly?

Android: Achieving smooth roll of ball using accelerometer

I have developed a maze game for Android where you control the ball by tilting the phone.
So I use the accelerometer and integrate the x and y accelerometer values and then move the ball a step in that direction.
I have a problem though, I cannot achieve a very smooth roll. When the ball picks up speed it is to obvious that it jumps in big discrete steps. I have seen other apps like this where the ball rolls fast but smoothly.
So I might have to change my strategy, use some sort of time solution instead. Now the faster the speed the bigger the step I move. Instead maybe I should have a timer that moves the ball 1 pixel every ms if speed is high or only every 10th ms if the speed is low or something along those lines.
Or how do people achieve a smoother roll?
Also: Would you use OpenGL for this?
What you're really doing here is integrating coupled differential equations. Don't worry if you haven't taken enough calculus or physics to know what that means.
People who integrate coupled differential equations for a living have evolved many algorithms to do so efficiently.
You really have four equations here: acceleration in x- and y-directions and velocity in x- and y-directions:
dvx/dt = ax
dvy/dt = ax
dsx/dt = vx
dxy/dt = vy
(sx, sy) give the position of the ball at a given time. You'll need initial conditions for (sx, sy) and (vx, vy).
It sounds like you've chosen the simplest way to integration ODEs: Euler explicit integration. You calculate the values at the end of a step from the values at the beginning plus the rate of change times the time step:
(vx, vy)_1 = (vx, vy)_0 + (ax, ay)_0 * dt
(sx, sy)_1 = (sx, sy)_0 + (vx, vy)_0 * dt
It's easy to program, but it tends to suffer from stability problems under certain conditions if your time step is too large.
You can shrink your time step, which will force you to perform the calculations many more times, or switch to another integration scheme. Search for implicit integration, Runge-Kutta, etc.
Integration and rendering are separate problems.

Best way to store points of a curve created by touch movements?

We are creating an app where it is necessary to draw lines (curves) based on touch events but we also need to store the points that correspond to specific lines.
Android.Graphics.Path gave us really nice smooth lines as we dragged our finger. However, there does not seem to be anyway to access the points from the Path objects.
What we do now is basically connect lines as a person drags their finger (using y=mx+b). So if the last recognized touch place was 0,0 and then they move past our threshold to (4,4) we call a function to make points from the line connecting these two ((0,0),(1,1),(2,2),(3,3),(4,4)). These points we add to an ArrayList.
This is giving us kinda choppy curves (obviously cause we are using y=mx+b), but more importantly it slows our program considerably after several lines are drawn.
Is there a better way to generate the points of a curve that follows a user's swipe movements? Or a better way to store these? Path seems to do it so well, is there any reason it hides the actual points?
Thanks!
A way to achieve smoothness is using the Path class' method quadTo.
quadTo(float x1, float y1, float x2, float y2) Add a quadratic bezier
from the last point, approaching control point (x1,y1), and ending at
(x2,y2).
Bezier curves are what you want to solve this problem. You should be able to detect a change in direction of the user's finger and stop the current curve and start a new one at that point. You really don't want to be storing every point along a path as I could just let my finger linger on the screen and clog up the datastructure.

Bullet movement

I'm new at games development and now stalled at one problem.
Trying to make tower defence type game, For now i have made "tower" with some functions, and where is problem with firing bullets.
Idea: touch somewhere on screen and bullet fire that direction. (bullet starting pos in middle of screen, speed must be a constant)
Maybe somebody knows how to calc that direction which bullet should fire.
I think, should calc angle or something to get that direction...
i.e.
this.mPhysicsHandler.setVelocity(angle*SPEED, angle*SPEED);
Thanks.
It depends a little bit if it's 2D or 3D but the idea is basically the same.
I would start by making a Vector class which has to variables, x and y. I would then add a few method for adding and subtracting Vectors. What you would have to do then would be to subtract from the touchPosition Vector the firingPosition Vector which would give you the direction Vector towards which you have to shoot.
If you need more help with this just comment.

How to create 3D rotation effect in Android OpenGL?

I am currently working on a 3D model viewer for Android using OpenGL-ES. I want to create a rotation effect according to the gesture given.
I know how to do single-axis rotation, such as rotate solely on the x-, y- or z-axis. However, my problem is that I don't know how to combine them all 3 together and have my app know in which axis I want to rotate depending on the touch gesture.
Gestures I have in mind were:
Swipe up/down for x-axis
Swipe left/right for y-axis
swipe in circular motion for z-axis
How can I do this?
EDIT: I found out that 3 types of swipes can make the moment very ugly. Therefore what I did was remove the z-axis motion. After removing that condition, I found that the other 2 work really well in conjunction with the same algorithm.
http://developer.android.com/resources/articles/gestures.html has some info on building a 'gesture library'. Not checked it out myself, but seems to fit what you're looking for
Alternatively, try something like gestureworks.com (again, I've not tried it myself)
I use a progressbar view for zooming in and out. I then use movement in x on the GLSurfaceView to rotate around the y axis and movement in y to rotate around the x axis
The problem with a gesture is that the response is not instant as the app tries to determine what gesture the user used. If you then use how far the user moves their finger to determine the amount to rotate/zoom then there is no instant feedback and so it takes time for the user to learn how to control rotate/zoom amount. I guess it would work if you were rotating/zooming by a set amount each gesture
It sounds like what you are looking to do is more math intensive than you might know. There are two ways to do this mathematically. (1) using quaternions (2) using basic linear algebra (but will result in gimbal lock if you arent careful.. but since you are just spinning then this is not a concern to you).. Lets go the second route since its easier.. What you need to do is recieve the beginning and end points of the swipe via a gesture implement and when you have those two points.. calculate the line that it makes. When you have that line, you can easily find the perpendicular vector to that line with high school math. That should now be your axis of rotation in your rotation matrix:
//Rotate around the axis based on the rotation matrix (rotation, x, y, z)
gl.glRotatef(rot, 1.0f, 1.0f, 0.0f);
No need for the Z rotation since you can not rotate in the Z plane with a 2D tablet. The 1.0f, 1.0f are the values that should be variables that represent the x,y of your vector. The (rot) should serve as the magnitude of the distance between the two points.
I havent done this in a while so let me know if you need more precision.

Categories

Resources