1

Is it possible to generate a series of objects outside of the screen, and then making those objects move inwards? I am creating a live-wall paper with circles that start outside of the screen, and move inwards and bounce off the walls. I have created an illustration to better describe what i mean: enter image description here

The 2 Issues im facing are:

  • Generating Objects outside of screen
  • Making them move inward and then bounce off the edges

How can i achieve this?

1 Answers1

0

One solution to this problem would be to create a class which would contain following attributes:

  • X and Y coordinated (could be Point)
  • speedX
  • speedY

Then you could create objects with coordinates:

  • (X < 0) or (X > screenWidth)

and/or

  • (Y < 0) or (Y > screenHeight)

and give them appropriate speed (so they move towards the screen boundaries).

In each step you would:

  • update each object's coordinates, moving it in appropriate direction corresponding to its current speed
  • redraw all objects on your canvas

The offset of object's coordinates depends on time step between each two redraws. It's up to you how you want to evaluate it.

Until an object reaches screen boundaries it will be drawn outside the screen and not visible.

To draw the objects on canvas you could extend View class (or SurfaceView - difference between these two is discussed here) and override onDraw() method. You can follow this tutorial or find another one by yourself (there are lots of it).

If an object reaches the screen boundary from its inside (i.e. when its X is in range [0, screenWidth] and its Y is in range [0, screenHeight]) you can negate its speed (in X or Y direction, depending on which boundary has been reached) so it would go in the other direction (like in an elastic collision with a wall).

You can adjust speedX and speedY minimum and maximum values to see which give the most satisfying results.

Community
  • 1
  • 1
Poger
  • 1,897
  • 18
  • 16