Share My Creation Bouncing balls in a 3D box with collision detection

Hi,

I wanted to do a small experiment, to use Android View rotation capabilities to do some of the work for me for a very simple 3D app, simulating perspective (there are some posts and snippets of view rotation here in the forum)
Android views can rotate on different axes and pivot points. So I thought that I could use them to emulate the walls of a box viewed from the front. I thought that for a square box, 45 degrees on each side would produce a nice perspective.. In my device, the good value (trial and error) has been 47º, I suppose that it is because I haven't taken into account camera position and a 'real' equation set. But it works ok.

(2nd edit) Added an example with 3 balls and a simple collision detector

The code is made just with a timer and a direction vector. One canvas for each 'side' draws the ball or its shadow, as if we were in front of each wall while it moves. Perspective does the rest. I think it can be modified with more than one ball and collision detection

A bit of math background will be needed to understand the code. Also, I have only used normal views, as speed was not the goal in this demo.

Hope you like it :)
 

Attachments

  • RotationView.zip
    4 KB · Views: 612
  • RotationView_2Balls.zip
    323.3 KB · Views: 628
Last edited:

Peter Simpson

Expert
Licensed User
Longtime User
That's excellent work @JordiCP, really nice coding :cool:
 

JordiCP

Expert
Licensed User
Longtime User
But... Unfortunately I cannot add a second like to your video :D
Thank LucaMs. You can unlike and then like the new one, since it works as a depth-one Lifo, and do it once again for the just-loaded 3-ball if you like it (just joking:p)

Perhaps Erel could move this thread to Share your Creations (just a thought), since my first intention was indeed to build a simple example but now it is growing and I am not sure if this is the correct place
 

JordiCP

Expert
Licensed User
Longtime User
You can build a game on this base.
Thanks! Not exactly based on this, but I am preparing a "tunnel" perspective game, also using some maths and a surfaceview.

To be honest, I would only consider it as a kind of GameView implementation, that 'works' because it takes advantage of many simplifications that can only be used for some very specific cases:
  • Timing --> now it is just using a timer and normal views. It doesn't even take into account delays for variable step values...
  • Collision detection --> with round objects (either in 2D or 3D), collision maths are really simple. Also, I don't take inertia into account so some rebounds are not 'real' depending on the angles (I am solving it)
  • Perspective --> Here camera is static and only basic transforms are used. The ball radius just increases or decreases with depth. So a canvas and a preloaded bitmap is enough
Free idea. The killer app!!
In some late-night summer deliriums, as it is difficult to sleep with such high temperatures, I have thought to do a face-controlled 3D-pong using OpenCV and detecting eye blinks, but this would only give control in one direction, so another detection would be needed to detect also left-right tongue detection and move along both axis. At the same time the app will record a video of the player's face while he is in the game doing all kind strange faces, and can share it in social media. Not sure if it would make a lot of money, but at least we would all laugh a lot :D
 

Star-Dust

Expert
Licensed User
Longtime User
You're thinking later on about a game you already have in mind. I see that you can do something with what you've created.
Many details to make more real movements and rebounds are for simulators. In a game they are not always necessary.
The 3D view looks like a 3D Tetris game that fun flaunted years ago for PCs.

Where I live, the heat is always high and not only in the summer. At least you are in a holiday area.
The best ideas come in the sleepless nights. Do not buy an air conditioner.
 

Star-Dust

Expert
Licensed User
Longtime User
I was thinking of making a 3d version of pong :p:p:p
260px-Pong.png

The first game I met, my dad bought a console that was just this game in the late 70's
 

wonder

Expert
Licensed User
Longtime User
To be honest, I would only consider it as a kind of GameView implementation
Please don't, you'll run into problems... LibGDX all the way!!!
GameView should be deprecated or updated to utilize OpenGL. <-- @Erel


From my first post ever, back in 2014: Problems with GameView
https://www.b4x.com/android/forum/threads/gameview-libgdx-help.45890/
I just tested on my Galaxy tab 3 and it runs real slow, i couldn't really tell you the reason for the speed differences as i have not used the gameview library, i'm sure Erel or someone else who has used this library would be able to shed some light, again this game will run real smooth in libGDX.

There is no doubt that libGDX is the best way to implement such a game.
 
Last edited:

JordiCP

Expert
Licensed User
Longtime User
Please don't, LibGDX all the way. GameView should be deprecated. <-- @Erel
Yes, that was precisely the meaning of my sentence: that it isn't (and won't be) even near to any serious game engine.

But this doesn't mean that it isn't an interesting experiment. I am preparing a different example to show what I mean.. :)
 

JordiCP

Expert
Licensed User
Longtime User
Great!! :) Give LibGDX a try, you can use one of the templates to get things started.
I will, it is in my to-do list

As many others, I like experimenting with code. Even when not using the most powerful tools, some tricks and optimizations can lead to surprising results, this is the purpose of many of the things I do when programming for leisure.

 

Star-Dust

Expert
Licensed User
Longtime User
Good job!!
It looks like the background of the Wolfenstein 3D Game
 

JordiCP

Expert
Licensed User
Longtime User
Yes, I suppose that somehow I got inspired by that ;)

That one is now zipped, but is evolving into this one. Still must figure how to make it 'playable'.
 
Top