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.
Related
is it possible to track mobile phone movement in space? I need and info like:
vertical position of the phone and it's movement in 3d space of our world.
So lets imagine that we are holding phone at 1.5 meters from the ground and moving it by circle trajectory. So I need to get coords of the phone in real word. Is this possible somehow?
It would be possible to do it like GPS, then you would have to move the z axis onto the x axis. Basically making the 3D element part of the 2D element, then reconstructing the 3d sphere using the newly obtained information to get a position in space. The problem with this method is that the earth is round, and you can never get a view that applies to all the phones.
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.
I have a a drawing program where a user can trace with their finger, and in a manner similar to the FingerPaint program, a series of Path's are drawn to represent the lines.
Now, I am doing some collision detection to allow the user to enter an 'erase' mode and delete selected lines, and am trying to determine how to track the individual pixels of the Path. Essentially, I am tracking the RectF that encompasses the Path, and if the RectF is intersected when in erase mode, I'd like to then do pixel-by-pixel intersection tests. So, I need to create some structure for storing the pixels, likely a two dimensional array where each element will be a 1 or 0, based on whether or not the underlying pixel is occupied by the drawn Path.
It is this last part that I am struggling with. While the user is drawing the line, I am feeding the passed X/Y values in as control points for a quadratic bezier curve via Path.quadTo(). The problem is that while Path uses these points to represent a continuous line, I am only being fed discontinous X/Y points from the touch device. Essentially, I need a way to duplicate what the Path object itself is doing, and take the passed X/Y points and interpolate that into a continous curve, but as a set of X/Y coordinates rather than a Path object...
Any pointers to get started on this?
Thanks
EDIT/MORE:
Ok, so as I mentioned, each Path is created (roughly) using the method found in FingerPaint, which means that it is a series of segments, where each segment is a quadratic bezier curve. Given that I know P0, P1 and P2 when I add these curved segments to the larger Path, I can determine the X/Y coordinates along the curve with:
So, my only problem now is determining a 'continous' set of adjacent X/Y coordinates, such that there are no gaps in this set that a user's finger might pass through without hitting one. This would mean determining each X/Y point at 1 pixel intervals. As the above formula would yield points at an infinite number of intervals, given the values of T ranging from 0 through 1, any idea how to programmatically determine the right values of T that will yield points at 1 pixel intervals?
I would do all collision detection using not curves or pixels, but just lines, it's much easier to find intersecting lines, i.e. the intersection of two sequential x/y coordinates of the user's swipe, and the lines of existing lines
Whats the best way to create a dart board game on Android. Is it possible to determine where a dart hit and in which area it is using a canvas or do I need to use a diffenrent approach?
Edit
Sorry I dont think I have explained my problem very well.
In svg in html5 I can create a dart board using shapes and assign an id to each shape. When a dart lands on a shape the code knows which shape it has landed on, gets its id and calculates the score. e.g id="20" id="60" id="40" for all 20 areas on a dart board.
How would I do this on the Android?
You've got the right idea. A simple approach would be to display a custom DartBoardView in an activity. The view would use an overridden onDraw to draw a dart board image and any darts the user has "thrown." The view would then use View.onTouchEvent to handle user touch events and translate those into new darts.
New answer based on your update:
You're going to have to do the hit detection manually. A touch event will give you an x-y coordinate with the upper left of the view as origin. Calculating points would go as follows:
Offset the given coords so they line up with the center of your board (i.e. touching the middle of your board would give (100, 150), translate that to (0,0)).
Get the angle of the touch to find which numbered section of the board was hit. Something like double touchAngle = Math.atan2(y,x). Might need to convert from radians to degrees here.
Map the angle to a base point value (if angle > 9 && angle < 27, base points = 7)
Multiply the base point value by 2 or 3 if the distance of the touch (distance = Math.sqrt(x^2 + y^2)) from center is a particular length away to account for the multiplier rings.
I'm building augmented reality app for android and I'm using jMonkey as my 3D engine.
I want to do simple thing. Move object from left side of screen to right (X axis) by changing the azimuth of view (I got it from compass).
I can calculate where the object is (rendered object has gps location) so I can say am I looking directly or maybe it is on the left or right. Now my problem is smooth move and calculate the change for local translation. My questions are:
1. how can I calculate position in local translation for the object based on azimuth thay I have
2. how make the change of local translation smooth. Now when I change the value from (for example) -4 to -1 the Spatial jumps. I would like to move it smoothly. I've tried to use Cinematic but either it is not for that or I'm not using it properly
About the calculating I've tried something like this
(objectAzimuth - azimuthWhereIlook) / offset
where offset is the scrope of X axis for example if my scope is <-20, 20> the offset is 40
the diffrence
(objectAzimuth - azimuthWhereIlook)
is a proper way of checking where the object is according to two azimuth (I have strong math for that and I've this is working. Based on that I know where the object is (directly, on left, on right).
So I have the point where the object should be on screen but I don't know how to cast it on X axis