Android Game Development: Framerate & Collisions - android

I've created a simple collision detection script, which works this way:
When the distance between the hero and an object is x pixels, the hero can "walk" x pixels, when he would not collide with an object (hero + 3px = no collision) he moves by 5 pixels.
But I also have to consider the framerate and therefor multiply his speed with the elapsed time /20
My problem is, when the framerate at some time is very low or high, he just moves by an additional pixel (1px) ..the chance is very small, but it still can happen.
so what can I do to prevent this?

Add a position correction to the end of post-collision-check or add a velocity correction to the end of pre-collision-check.
Post-collision: object is translated back to the collision point.
Pre-collision: object speed is altered temporarily so in the next frame it will be on the point of collision.
Example:
Your object moves 75 pixels and tunnells through the wall. What to
do? You need a position history of 1-iteration-back. Looking to
history, you see it was actually behind the wall, then the current
location-->it is now passed the wall xx pixels. Then you would set
its new position next to wall before painting.
You cannot know when it will lag your android: better algortihm needed to make it independent of fps. How? you may just pause whole world a bit until fps is steady again or just store the next few iteration before painting then calculate things before painting.

Related

Is there any method/function to control moving speed in justTouched() in Gdx?

I want help in set condition to control the speed of missile when screen touched(A missile shooter Jet).
I tried with Timer schedule but didn't worked for me.
=========================================================================================
enter image description here
Welcome. :) When asking questions you should include your code directly into the body of your post instead of linking to an image.
By calling batch.draw() and updating the velocity of the missile in a while loop the missile will be moved off screen before it actually gets drawn. The while loop must complete before it moves down to the rest of the drawing code.
You will need to remove the while loop, which means you won't be able to use justTouched() as that will only be active for one render cycle. Instead you'll need to create another variable (like "missileFired" or something) that is set when the button is pressed, and then you check this before updating the position of the missile and drawing it. Once the missile is off screen, then reset missileFired to false.
What you are calling "velocity", isn't really velocity. It is a position offset from the original position of the rocket. You can directly update the y position of the missile instead.
missileY += 1;
One final point is that your missile will move at different speeds when playing the game at different frame rates. The missile will move twice as fast when played at 60fps as it will at 30fps. To get around this, you need to use the delta time (the time between frames) and multiply this by the increment value that you use to update your missile's position. So that would look like this.
missileY += 20 * Gdx.graphics.getDeltaTime();
In this example, the missile will move 20 pixels every second. However, if you use missileY += 1 instead, then the missile will be updated 1 pixel every frame (which would be 30 pixels per second at 30fps or 60 pixels per second at 60fps). By using delta time the movement is now based on seconds instead of frames, which means your missile will move at exactly the same speed regardless of the frame rate of the device. Hopefully this all makes sense. If not, look up programming tutorials on "delta time", "game loops", and "frame rate independent game play" for more details on this topic.

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?

AndEngine Box2D Extension - Scaling

I'm new to AndEngine and Box2D. So bear with me please.
I created a new project, set up a 480x800 camera, added a 32x32 stripe, created a physics world at Earth gravity and dropped the stripe. Lo and behold, it DID drop. But it didn't seem "natural" to me; it was too slow.
Then I realized that the gravity is in meters (m/s2) whereas the environment is in pixels. Where does the conversion between meters and pixels tak place? Somewhere there should be an assumption behind the scenes. Do I have any control over it?
How does Box2D know whether it's dropping the stripe from 100 meters above the ground (and viewing it from a distance which would appear a very slow drop) or 1 meter above the ground (and viewing it from up close which would appear very fast)?
To test that the conversion is the real problem, I multiplied the gravity by 10 and it improved the "naturalness". But I think there should be a more sophisticated way to convert pixels to meters.
Thanks in advance. I really appreciate your comments.
It's as #iforce2d said it the comment. In AndEngine the default value is 32, therefore 32 pixels is considered one meter. When converting pixels to meter, divide the pixels by this value. When converting from meters to pixels, multiply with this value. You can find this value in org.andengine.extension.physics.box2d.util.constants.PhysicsConstants class.
The ratio is then used in PhysicsFactory.create... methods if you don't specify your own. These methods create the physics body for you, measuring your sprite size in pixels and passing meters to Box2D. It's also used in the PhysicsConnector class constructor. Use your own value if 32 doesn't suit you, but then you will have to be consistent and use it every time.

Updating positions and positions outside screen

I want to show the movement of a car in a road . I have a textfile containing the positions and I built the movement by updating the position of a car every second .lets say the plain is (200,200) . now what should I do for positions that are outside this screen ? how could I follow my car there ?
should I set up a camera or something?
by the way my app is 2D.
From my experience, there is no actual concept of setting up a camera in 2D programming, but I could be wrong. You'll have to do this yourself, create a camera class etc.....
What I think will end up happening is that the car will stay centered on the screen and everything under it will be moving instead. Depends on what you're trying to achieve.
So if your car is moving northeast at 20 km/h, don't actually move the car, make everything under the car move southwest at 20km/h (or how many pixels per frame this comes out to)
This is if you want to follow the car. If you want to center the "camera" on the car whenever it goes out of bounds you'll probably have to move the landscape and the car towards the center of the screen.
EDIT: I'm assuming that the car will be the main focus?? So it should always be at the center of the screen.
All objects in the game should have a velocity and a position. The position tells you where the object currently is and the velocity tells you how many x's and how many y's it should be moving per frame. So every frame you would say position = position + velocity.
The non-car objects can move off the screen as they wish without having the camera follow them, so let them go. Keep the car centered and adjust all the other objects' velocities based on the car's.
Ex:
Car's velocity (3, 0) ---> means it's moving right in the straight line at 3 pixels per frame
Object 1 velocity (4, 0) ---> means it's also moving right in a straight line but 4 pixels per frame
The velocity of object 1 will have to adjust itself according the the car's velocity. So say:
object1.position = object1.position + (object1.velocity - car.velocity)
Object 1's new velocity is (1, 0), so it's moving faster than the car by one.
If the car gains speed to let's say (5, 0) then object one will appear to be moving backwards by 1.

Is it possible to measure distance to object with camera?

Is it possible to measure distance to object with phone camera?
I mean, in my application I start the camera, facing the camera to the object (lets say house) and then press the button and it calculates the distance and shows me in screen.
If it's possible where I can find some tutorial or information about it?
I accept the question has been answered adequately (with the obvious caveats of requiring level ground and possible accuracy problems) but for those who don't believe it can be done or that it needs a video camera, let me explain the low-level math needed to do it....
The picture above shows me standing outside my house. The horizontal (d) is the distance I want to measure and the vertical (h) is the height above the ground at which I'm holding the camera. In this case 'h' is a known value when I'm holding the android camera at eye-level (approx 67 inches or 1.7 metres). When I tilt the camera to aim it directly at the point my house meets the ground, all the software needs to do is work out the angle (a) relative to vertical and it can calculate 'd' using...
d = h * tan a
Well you should read how ithinkdiff.com "measures" the distance:
Uses the angle of the iPhone to estimate the distance to a point on the ground.
Hold the iPhone in front of you, align the point in the camera and get a direct
reading of the distance. The distance can then be used in the speed tool.
So basically it takes the height of where you hold the phone (eye-level), then you must point the camera to the point where object touches the ground. Then the phone measures the inclination and with simple trigonometry it calculates distance.
This is of course not very accurate. It gets less accurate the further the object is. Also it assumes that the ground is level.
Nope. The camera can only give you image data and an image alone doesn't give you enough information to give you depth information. If you had multiple images that you had location information for or even video you could then process it to triangulate the distance, but a single image alone would not be enough to give you a distance.
You can use the technique used by our eye to get perspective of depth and distance.
1) Get 2 images of the same object from two different camera positions.
2) The distance or pixels between object in 2 images is inversely proportional to distance between camera and object.
The implementation is available at https://github.com/agnelvishal/Distance-between-camera-and-object
Here is the research paper http://dsc.ijs.si/files/papers/S101%20Mrovlje.pdf
You have the angle in the phone's accelerometer. If you calculate the tangent of this angle and multiply it by the height of the camera lens, you get the distance.
I think this App uses the approach MisterSquonk mentioned (its free). Watch the "Trigonometry" technique.
I think by using FastCV you can calculate the distance between Camera and the object. In this You dont need to know the angle or the Position of camera that you are holding above ground Level. take a look at this question here
One way to achieve this is using the DPI's in your device. You can take a picture and calculate the height. But you'll need another object as a reference and then you will be able to know the problem with this method could be the perspective between the objects
I think it could be possible doing that using the phone camera. I know that the modern phones use lenses to focus on a object. If it is possible to know their focal length and their position(displacement) to focus on the chosen object it's also possible to determinate the distance.
No. Only with two cameras in stereo mode, like the xbox 360 kinect. It takes at least 3 points to triangulate distance.

Categories

Resources