I am currently writing some simple snake game in java. The snake objects has an array full of bodyparts. these bodyparts all contain a rectangle.
the snake's update method is like that:
public void update() {
if(isMoving) {
if(System.currentTimeMillis() - moveTimer > moveSpeed) {
if(parts.get(0).getX()+dx*size >= 0+GameView.GRID_PADDING_LEFT && parts.get(0).getY()+dy*size >= 0+GameView.GRID_PADDING_TOP && parts.get(0).getX()+dx*size <= ((GameView.GRID_WIDTH-1)*size)+GameView.GRID_PADDING_LEFT && parts.get(0).getY()+dy*size <= ((GameView.GRID_HEIGHT-1)*size)+GameView.GRID_PADDING_TOP) {
for(int i = parts.size()-1; i > 0; i--) {
parts.get(i).setLocation((int)(parts.get(i-1).getX()), (int)(parts.get(i-1).getY()));
}
parts.get(0).setLocation(parts.get(0).getRect().left+dx*size, parts.get(0).getRect().top+dy*size);
moveTimer = System.currentTimeMillis();
}else {
die();
}
}
}
}
All in one, the snake moves it's head into dx direction times one cellspace (size) --> dx*size and dy*size.
Then the rest of the body moves, the part behind the head gets the coordinates of the head, the part behind that part gets the coodrinates from the part in front of him etc.
Now I am trying to achieve some collision with the snake itself. I've tried several methods, e.g.:
if(parts.size() > 2) {
for(int i = 3; i < parts.size(); i++) {
if(parts.get(0).getRect().intersect(parts.get(i).getRect())) {
die();
}
}
}
But as soon as the bodypart array reaches the limit of 2 parts, the die() method gets called. I don't know why, because the 2nd part should sit two bodyparts behind the head, which makes it impossible to intersect with the head-rectangle.
Where's my problem?
Oh, that was a really big fail i made here.
When adding a new part, the new part gets the coordinates of the head. Of course will intersect with the head when it gets it's coordinates on create!!
Nevertheless, thanks for the replies!
new addPart method, corrected the parts.get(0) --> parts.get(parts.size()-1)
public void addPart(int amount) {
for(int i = 0; i < amount; i++) {
parts.add(new BodyPart(parts.get(parts.size()-1).getX(), parts.get(parts.size()-1).getY(), parts.get(parts.size()-1).getHue()-5));
}
}
Related
I'm parsing a Json file which contains a lot of multipolygons to Realm. I've set it up the following way:
RealmMultiPolygon class:
public int dangerLevel;
public int timeOfDay;
public RealmList<RealmPolygon> realmPolygons
RealmPolygon class:
public RealmList<RealmPolygonCoordinate> coordinates;
RealmPolygonCoordinate
public double latitude;
public double longitude;
As far as items concern:
RealmMultiPolygon contains 8 items
RealmPolygon contains 22260 items
RealmPolygonCoordinate contains 352241 items
I'm trying to figure out if a RealmPolygon is in a direction where i'm looking at and near my current location. I'm taking 31 locationsamples with bearings like this:
private void calculateUserDirectionLocations() {
Log.d(Constants.DEBUG, "update the locationbar");
if(compassData < 0) {
return;
}
int step = 50;
int bearingstep = 1;
double bearing = Math.round(compassData / bearingstep) * bearingstep;
if(userLocation != null) {
if(Math.abs(bearing - oldBearing) > 1) {
locationSamples.clear();
Log.d(Constants.DEBUG, "START location samples");
for(int i = 0; i <= Constants.MAX_DISTANCE; i+=step) {
if (i % step == 0) {
locationSamples.add(locationWithBearing((double) i));
}
}
Log.d(Constants.DEBUG, "END location samples");
updateColors();
oldBearing = bearing;
}
}
}
Where compassData is gained from the RotationVectorSensor (value between 0 and 360 degrees) and the MAX_Distance is 1.5Km.
Now if I want to know if one of the 31 locationSample points is near a polygon I have to loop through all the polygonCoordinates which I currently have in my Realm database 31 times. which means:
31 * 352241 = 10919471
This is incredibly slow but I can't really seem to find a better solution for this. Anyone have an idea how to do this better / faster?
Update 1
At the moment i'm looping through the coordinates like this:
for(RealmMultiPolygon rmp : area.avalanche.multiPolygon) {
if(rmp.timeOfDay == 2) {
for (RealmPolygon polygon : rmp.realmPolygons) {
for(LatLng sample : locationSamples) {
tempPoint = new LatLng(polygon.coordinates.first().latitude, polygon.coordinates.first().longitude);
if(SphericalUtil.computeDistanceBetween(tempPoint, sample) <= 500) {
coords.add(tempPoint);
} else {
break;
}
}
}
}
}
I finally fixed this by coming up with a totally different solution. I'm now creating an image based on the width and height of the area the polygons are in as a background task. Then I calculate the pixel position based on the locationsample I take and use the getPixel method to get the pixels on that position. This is way faster than looping through each polygon.
I am trying to make an app view that, when presented with a subtraction problem, will show an animation of a growing rectangle (or fat line) that crawls up the number line to that integer.
I already have things set up so that I click "show me!" and bars are drawn along the number line showing the minuend, the subtrahend and the difference, but I'd like to be able to have a positive number's rectangle crawl up in a positive direction, negative from zero in the negative direction.
In looking through the documentation there seem to be several different ways to go about this. I am hoping somebody can suggest a way that's reasonably simple for this novice to implement. Here are the different approaches I've found:
This seems very much like this person's desire to have a bar graph where the bars "pop up," but it doesn't have an answer. Android Animated Bar Chart (invalidate())
I've perused http://developer.android.com/guide/topics/resources/drawable-resource.html -- but I don't have a "drawable" because it's being drawn in the View. I'm thinking of making the rest of the number line a background bitmap per Android View.getDrawingCache returns null, only null but I want three rectangles (for the minuend, subtrahend and difference).
I have thought of making a series of rectangle drawables and showing them frame-by-frame to show the growth.
I have looked at Animation at a specified rate using canvas / Ondraw but cannot discern just what code to wrap in that "if" statement, if in fact my problem is re-drawing...
I looked at using Paths -- and put the following code together. If direction matters, then it seems I should be able to slow things down and watch the path going in that direction, but it's instantaneous. I found I saw an example at http://www.curious-creature.org/2013/12/21/android-recipe-4-path-tracing/
if (minuendLength > 0) // start at 0 and go to the minuend length
{
path.addRect(interpX(0), yPosition(40), interpX(minuendLength), yPosition(43) , Path.Direction.CW);
// interpX lets me say what number on the number line it should align with;
//yPosition is percent of the way down the screen.
canvas.drawPath(path,minuendPaint);
// Seems same as drawRect -- instantaneous.
}
(The number line in the 'background' code is as follows, with different options for different sized integers entered:
if ( (minuendLength <10 && subtrahendLength <10 ) && (minuendLength >-10 && subtrahendLength >-10 ) )
{
this.setLineDimension(10); // default
super.onDraw(canvas);
canvas.drawLine(interpX(-this.getLineDimension()), yPosition(52 ),
interpX(this.getLineDimension()), yPosition(52), axisPaint);
int step = this.getLineDimension()/5; // so you're not writing *all* the numbers
// when they enter numbers and you make your own number line.
// paints the little hatch marks
for (int x = -this.getLineDimension(); x <= this.getLineDimension(); x+=step/2)
canvas.drawLine(interpX(x), yPosition(52), interpX(x), yPosition(53) , littleAxisPaint);
// draw the numbers on the hatch marks
textPaint.setTextAlign(Paint.Align.CENTER);
for (int x = -this.getLineDimension() + step; x < this.getLineDimension(); x += step)
{
canvas.drawText(Integer.toString(x), interpX(x), yPosition(56), textPaint);
}
}
I ended up doing this using Runnables.
animate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ( ((addend1 > 0) && (addend2 < 0)) )
{
tempNum1 =addend1 ;
tempNum2 = addend2;
h.post(shrink1bigger);
}
else if (addend1 < 0 && addend2 > 0)
{
tempNum1 =addend1 ;
tempNum2 = addend2;
h.post(shrink2bigger);
}
}
});
private Runnable shrink2bigger = new Runnable(){
#Override
public void run() {
tempNum1+=1;
tempNum2-=1;
shrinkDraw();
Log.i("WatchAnimActivity", "tempNum1 is " + tempNum1);
h.postDelayed(shrink2bigger, 500);
checkTemp(shrink2bigger);
}};
private void shrinkDraw()
{
numLine.setFirstNum(tempNum1);
numLine.setNextNum(tempNum2);
numLine.invalidate();
}
public void checkTemp(Runnable shrink)
{
Log.i("WatchAnimActivity", "tempNum1 in CHeckTemp is " + tempNum1);
if (tempNum1 ==0 || tempNum2==0)
h.removeCallbacks(shrink);
}
I want to moove two objects smoothely at Touching.
Here is my Code:
for(int i = 0; i <96; i++){
Asstest.rect_pipe_down.y--);
}
This should move the rect 96 pixels down (SMOOTH)
But it just close without smoothed...
What did I wrong?
If you Touch, the pipes should close, but not hard, smooth should they close.
But with following code they just close hard...
Here is the full touched code:
if(Gdx.input.isTouched()){
Assets.rect_pipe_down.y = 512 - 320/2;
Assets.rect_pipe_up.y = -320 + 320/2;
for (int i = 0; i < 96; i++){
smoothTime = TimeUtils.millis();
if(TimeUtils.millis() - smoothTime > 10) {
Assets.rect_pipe_down.y--;
Assets.rect_pipe_up.y++;
batch.begin();
batch.draw(Assets.region_pipe_down, Assets.rect_pipe_down.x, Assets.rect_pipe_down.y);
batch.draw(Assets.region_pipe_up, Assets.rect_pipe_up.x, Assets.rect_pipe_up.y);
batch.end();
}
}
closed = true;
}
You cannot do rendering multiple times in one render() call, one call is for drawing exactly one frame. In your current code, the later images simply overwrite the previous ones.
What you could do is have a variable which persists between frames which stores whether or not the pipes are currently closing, a constant for the speed and some condition to tell when they can stop - maybe when they are some given distance from each other, not sure what you would want here. Anyway, that's what I'll use in my example.
Then in the render() method, before drawing anything, you can do this:
if (closing) {
Assets.rect_pipe_down.y -= CLOSE_SPEED * delta;
Assets.rect_pipe_up.y += CLOSE_SPEED * delta;
if (Assets.rect_pipe_down.y - Assets.rect_pipe_up.y < TARGET_DIST) {
Assets.rect_pipe_down.y = Assets.rect_pipe_up.y + TARGET_DIST;
closing = false;
}
}
Here, closing is a variable you set to true when you want them to start closing, the others are constants. You could add some more variables/constants if you want to make sure they end up at a specific height independent on framerate.
I write some wallpaper. It represents the many sprites which fall down. When its fly out of screen I want to detach them from Scene. How can I do this optimally?
Now I use this code:
for (int i = 0; i < getScene().getChildCount(); i++) {
if (i < getScene().getChildCount())
removeSprite((Sprite) getScene().getChildByIndex(i));
}
private boolean removeSprite(final Sprite sprite) {
if ((sprite.getX()>CAMERA_WIDTH+sprite.getWidth()*2 || sprite.getX()<-sprite.getWidth()*2) && (sprite.getY()>CAMERA_HEIGHT+sprite.getHeight()*2 || sprite.getY()<0-sprite.getWidth()*2)) {
final PhysicsConnector spritePhysicsConnector = physicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(sprite);
physicsWorld.unregisterPhysicsConnector(spritePhysicsConnector);
physicsWorld.destroyBody(spritePhysicsConnector.getBody());
getScene().unregisterTouchArea(sprite);
getScene().detachChild(sprite);
}
}
But its not right way. What are the best solutions exist?
You can enable culling on Sprite object. That way it won't be rendered when it's out of screen.
The below code is my attempt to send mMyView to the front or the back of the set of children of mPivotParent so it will be rendered on top or behind the others. Hiding the view will not suffice in my case.
mPivotParent is a FrameLayout.
Looking at mPivotParent.mChildren shows that my code below "works" in that the ordering is being set correctly. Yet it has no impact on the z order. Not only this, but the framerate gets cumulatively slower and slower the more times the repositioning code gets called. There are 4 children total and mPivotParent.mChildrenCount remains at 4 throughout as expected.
I'm targeting API Level 7.
#Override
public boolean onTouchEvent(MotionEvent event) {
Display display = getWindowManager().getDefaultDisplay();
float x = event.getRawX();
float sWidth = (int)display.getWidth();
float xLerpFromCenter = ((x / sWidth) - .5f) * 2.f; // [-1, 1]
mRotateAnimation.mfLerp = xLerpFromCenter;
if(xLerpFromCenter < -0.2f && mPivotParent.getChildAt(0) != mMyView)
{
mPivotParent.removeView(mMyView);
mPivotParent.addView(mMyView, 0);
refreshEverything();
}
else if(xLerpFromCenter > 0.2f && mPivotParent.getChildAt(0) == mMyView)
{
mPivotParent.removeView(mMyView);
mPivotParent.addView(mMyView, mPivotParent.getChildCount() - 1);
refreshEverything();
}
return super.onTouchEvent(event);
}
private void refreshEverything()
{
for(int i = 0; i < mPivotParent.getChildCount(); ++i)
{
mPivotParent.getChildAt(i).invalidate();
mPivotParent.getChildAt(i).requestLayout();
}
mPivotParent.invalidate();
mPivotParent.requestLayout();
}
Partial Solution
Here's a somewhat inefficient hack but it works for my purpose, which is to take the top item and send it to the back, keeping all other items in their same order.
private void putFrontAtBack()
{
for(int i = 0; i < mPivotParent.getChildCount() - 1; ++i)
{
mPivotParent.getChildAt(0).bringToFront();
}
refreshEverything();
}
Note: This doesn't work in the general case of arbitrary re-ordering.
Try this.
private void reorder(int[] order)
{
if(order == null || order.length != mPivotParent.getChildCount()) return;
for(int i = order.length - 1; i >= 0; i--)
{
mPivotParent.getChildAt(order[i]).bringToFront();
}
refreshEverything();
}
This code provides arbitrary reordering. The integer array "order" is used to indicate the new order of each view, where order[n]=x means the new order of childAt(x) is n.