Accelerometer Values from Android/iPhone device - java

I'm trying to map my movements with a android device into an OpenGL scene.
I've recorded accelerometer values for a simples movement: Moving the phone (lies flat on a table) 10cm forward (+x), and then 10cm backward (-x).
The problem is that this values when used to calculate velocity and position, makes only the opengl cube go forward. Seems like the negative acceleration recorded was not enough to reduce the speed and invert its movement.
What can be the problem?
This is my function that updates the velocity and position every time new data comes in:
void updatePosition(double T2) {
double T = 0.005;
Vec3 old_pos = position.clone(), old_vel = velocity.clone();
velocity = old_vel.plus(acceleration.times(T));
position = old_pos.plus(old_vel.times(T).plus(acceleration.times(0.5 * Math.pow(T, 2))));
}
This is the X,Y,Z accelerometer values over the entire captured time:
alt text http://img25.imageshack.us/img25/7528/forces.jpg

Your function takes the parameter T2 which I am assuming is the accelerator input? but you are using T which you have declared as double T = 0.005; so your function will be always going forward at a static rate.

Related

Shooting prediction algortithm in java

I have an issue in my current game dev hobby. I have two units, which are hostile in 2D space. One is shooting directly at the opponent, so if he moves it misses. But the other should predict it's opponents movement and shoot "ahead".
Let's assume first unit is A and second one is B. I can calculate their distances and I have their viewing angle and I have the speed at which they are moving. (player speed and bullet speed are different constants)
I tried with approximations to calculate distance between A and B and then use the Bv and orientation angle to calculate where the B will be in the next second and then scale that by the distance of two players divided by the bullet speed. But this is very inefficient and does not work well.
float distanceK = MathUtil.distance(unit.x, unit.y, opponent.x, opponent.y) / Constants.BULLET_VELOCITY;
float x = (float) (opponent.x + (Constants.UNIT_FORWARD_VELOCITY * distanceK * Math.cos(opponent.orientationAngle)));
float y = (float) (opponent.y + (Constants.UNIT_FORWARD_VELOCITY * distanceK * Math.sin(opponent.orientationAngle)));
float angleToRotate = MathUtil.angleBetweenUnitAndPoint(unit, x, y);
In the example above I then use angleToRotate variable to determine how much do I have to rotate to hit opponent, but the rotation too takes some time (54deg/s)
I would need a more optimal solution for this problem.
a) predict opponent movement when you are standing still.
b) predict opponent movement when you are moving.
You could use a vector representation of the space. So BA would represent the vector between A and B from A's point of view. You could then add the vector Bv of unit B's velocity. The resultant vector would be the vector between B and Bvp the predicted position of B at some time t in the future. As for a moving calculation, you'd need to also account for the movement vector of A.

Java LWJGL Rendering pistol model at right side of screen

Hi everyone I'm working on weapon rendering and I got stuck in part when I have to calculate gun.y and gun.rot.x. Rotation on y axis and calculating x,z of gun works good. But now is question how I can get gun.rot.x and gun.y. My calculation of gun x and y looks like:
float offsetX = (float) Math.sin(Math.toRadians(camera.getRotation().y + 25));
float offsetZ = (float) Math.cos(Math.toRadians(camera.getRotation().y + 25));
gun.x = camera.x + offsetX;
gun.z = camera.z - offsetZ;
Y rotation of gun is really simple:
gun.getRotation().y = 360 - camera.getRotation().y;
I tried to calculate gun.y with code like this:
float offsetY = (float) Math.sin(Math.toRadians(camera.getTransform().getRotation().x + 25));
gun.y = camera.y - offsetY
But it seems to not work correctly.
What you are trying to do is rendering a viewmodel (the gaming term). this i generally done by having another camera parented by the player's camera. In other words, you would use a separate camera to render the model so that it looks close to the face (and also to avoid the gun going through walls for example) and then depending on your implementation add your viemodel camera's transformation to the main camera's transformation.
If you are using the fixed function pipeline (glMatrixMode(), glTranslatef() and so on) all you have to do is apply the transformation (call glTranslatef() and glRotatef()) without reseting the identity matrix (glLoadIdentity()). For example:
{ /* your rendering code */
glLoadIdentity();
camera.applyTransformation();
render_scene();
glPushMatrix(); // ensures that the transformation isent
// directly applied to the matrix should
// you want to render more thing with the
// main camera after the view model
viewmodelCamera.applyTransformation();
render_viewmodel();
glPopMatrix(); // complementary call for glPushMatrix()
// marks the end of this matrix operation
// on the matrix stack
possibly_render_more_things() // if you wish
} /* end of rendering code */
If you are using matrices for your camera (which you should if you intend to properly use modern OpenGL), all you have to do is add the two MVP (Model View Projection) matrices, your base camera's and your modelview camra's, and pass the result of these to your shader for the gun rendering.
Hope this helped!
EDIT: Just thought I'd mention, the second camera is basically like working in model space for your gun model, you would use the units that you are currently storing in gun.x (for ex) (also don't use public variables), and make those the camera's transformation.

Steering Behaviors: Arrive behavior problems

I'm working on implementing steering behaviors in an OpenGL Android project of mine, but I'm having a little trouble understanding why my Arrive behavior code isn't behaving like it should.
During the update loop my program calculates a steering force with which to update the position of a mobile agent. Currently, it behaves identically to the "Seek" behavior, in that it moves to the position defined in Targetpos, but instead of slowing down and stopping when it nears the point like it should, it keeps moving forward and overshoots the target over and over again.
Any help on this would be greatly appreciated, below is the code that's supposed to return the proper steering force for the agent.
Deceleration is simply an enum of 1-3 encoding different rates at which the agent should slow.
private Vector2 Arrive(Vector2 Targetpos, Deceleration deceleration) {
Vector2 ToTarget = sub(Targetpos, agentPos);
//calculate the distance to the target
float dist = ToTarget.len();
if (dist > 0.2f) {
final float DecelerationTweaker = 0.3f;
//calculate the speed required to reach the target given the desired
//deceleration
float speed = dist / ((float) deceleration.value() * DecelerationTweaker);
//make sure the velocity does not exceed the max
speed = Math.min(speed, agentMaxSpeed);
Vector2 DesiredVelocity = mul(ToTarget, speed / dist);
return sub(DesiredVelocity, agentVelocity);
}
return new Vector2(0, 0);
}

android: major prob with detecting angle of device inclination

For a project I need to detect the inclination (pitch) of an android device. For that, I use the acceleration sensor and the magnetic field sensor and everything works fine. The device is able to move forward and backward hanging on a pendulum. If I have the angle of the pendulum/device, I can calculate the acceleration. The problem is: All that works only stationary. If the device is in a car which is moving, it doesnn't work any more. When the car is braking, the pendulum moves forward and the inclination changes, but the device cannot detect that because the force is still acting "downward" along the y-axis.
Is it any possible to detect the inclination angle in this case?
Not sure if I've explained my problem clearly. What I need is the angle of a moving pendulum, detected by device sensors.
<< EDIT >>
What I need is the correctly measrued angle of the pendulum even when it is within a moving car (please take a look at my pdf lying in my Dropbox):
https://dl.dropboxusercontent.com/u/5655235/sensors.pdf
As you can see on the image, the measured angle is not correct if the box is accelerating.
Here is my code:
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
acc = event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mag = event.values;
if (acc != null && mag != null) {
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, acc, mag);
if (success) {
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
phiRad = orientation[1];
phi = Math.toDegrees(phiRad);
}
}
I hope that I understood your problem correctly!! So you want to detect the inclination of the device when it is in a moving car...
First of all, when you say that:
When the car is braking, the pendulum moves forward and the inclination changes, but the device cannot detect that because the force is still acting "downward" along the y-axis.
Are you sure that the device is kept vertically straight?
So, when the car is moving the pendulum will obtain an acceleration of the car in forward direction. Here, forward doesn't mean that it is directed towards x-axis of the device, but the x-axis of the car/surface. As per my understanding, you are trying to check the pendulum's movement towards x-axis, but you should check for the position of device. Hope it helps, if I understood your problem correctly.

How to change coordinate regularly using gl.glTranslatef() ?

Having the following display() -
float tranX , tranY , tranZ ;
public void display(GLAutoDrawable gLDrawable) {
final GL gl = gLDrawable.getGL();
gl.glLoadIdentity();
gl.glPushMatrix();
gl.glTranslatef(tranX ,tranY ,tranZ);
gl.glCallList(i);
gl.glPopMatrix();
tranX += 0.05;
tranY += 0.05;
tranZ += 0.05;
}
As you can see each display() calling the matrix of the object in gl.glCallList(i) saved and get coordinates change by gl.glTranslatef(tranX ,tranY ,tranZ) .
Suppose that at some stage I want to save this object at its current position (after gl.glTranslatef calling ) and start to translate it to another tranX , tranY , tranZ values .
How could I save this object position considering the above gl.glPushMatrix() and gl.glPopMatrix() ?
Push/pop matrices are there to accumulate complex matrix transformations that would otherwise be painful to do by hand. For storing and moving object positions, keeping variables as you have done is correct. To expand on that and, as you say start moving in another, add a directionX/y/Z. Eg, tranX += directionX etc. Then when you want to change direction, simply set directionX/Y/Z to a different value.
The speed will change depending on how fast your computer is though. You'll want to find the time since the last frame (or last call to display) and do something like this: transX += velocityX * deltaTime etc.
If you want to move an object from one point to another specific point, you want to look into key-framed interpolation. For example position = pointA * (1.0 - x) + pointB * x and make x move from 0 to 1 (x += speed * deltaTime). When x is above one, pointA becomes pointB and pointB is set to the next position in the list. Then subtract 1.0 from x and continue.
Assuming you're translating from the origin (and even if you're not) - it should be quite possible to save the position of the object relative to the origin in this case. You might use an object that stores the data in three fields (xPosition, yPosition, zPosition).
To translate the object later on, you would first translate to this position and then translate from there as needed.

Categories