Author Topic: Coin Toss Graphics Program (_MAPTRIANGLE(3D) Demonstration)  (Read 5078 times)

0 Members and 1 Guest are viewing this topic.

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Coin Toss Graphics Program (_MAPTRIANGLE(3D) Demonstration)
« on: August 14, 2021, 06:03:34 am »
I wanted to create a program to demonstrate the wide applicability of the QB64 _MAPTRIANGLE procedure for creating impressive graphics applications.  _MAPTRIANGLE, most especially _MAPTRIANGLE(3D), is another amazing and supremely useful QB64 coding technique, and can be easily implemented by the novice coder.  I include myself very much within this group (not so much a QB64 novice as a low-skill member - most of the techniques of QB64 and virtually all of the discussions at this website are way above my understanding).

Whilst QB64 allows the creation of 'real' 3D graphics applications with the use of _GL methods (wonderfully demonstrated here by Ashish in particular), I do not have the faintest understanding of the technique - a survey of the _GL commands in wiki is just incomprehensible.  But I have used _MAPTRIANGLE(3D) in the past, and so could any of our members.

The idea of creating a Coin-tossing simulation seemed a good basis to demonstrate _MAPTRIANGLE(3D) abilities.  A search of the website suggests that this has not been attempted in the past, so this looked to be a good project with the goal of simulating a 50-50 Heads-Tails toss of a spinning coin.

To simulate a coin-tossing, the basic starting points were:

  • A thin cylindrical 'solid' body with two different faces.

    The body describes a 3D path where it moves under a constant downward gravitational acceleration.

    In travelling such a path, the body spins upon an axis through a fixed diagonal.

    Upon hitting a solid flat surface the coin settles to lay flat - which way it falls determined by the angle at which it hit that surface.

    A degree of randomness in the flight so that there is a 50-50 chance of falling on each face.

To give a good representation of a coin shape in 3D, the graphics image was constructed in the following manner.  Websites scanned for attractive images of the two faces of a coin in flat right-angle view.  The milled edge was created in code to give cyclic lighter-darker regions (which looks pretty realistic).

The flat faces are mapped into the _MAPTRIANGLE(3D) space by cutting the faces into radial triangular segments.  The coding to do this was reasonably simply and was achieved in pretty short time.  This is the beauty of _MAPTRIANGLE(3D).

The motion of the coin in the 3D space was easy to code.  The starting assumptions were:

  • The coin travels in a straight line (the x- axis of the _MAPTRIANGLE(3D) space) with constant speed.

    After being launched from the starting position, the coin spins at a constant rotation.

    The initial upward (_MAPTRIANGLE(3D) space y-axis) varies at each toss.  This sets the (random) time of flight.

The _MAPTRIANGLE(3D) view is rotated in the y-axis (vertical) to give a perspective view of the coin travel.


Getting the equations correct for the coin rotation and the horizontal and vertical motions was a doddle, of course.  The complicated part was in working out the appropriate equations for the landing of the coin and its subsequent fall to the landing surface.  The coin is going to land on one of four edge corners (front and rear faces meeting the edge 'up' and 'down').  In fact this is the simplest of trigonometry, working out triangle geometry, but it took me ten times longer doing this to get it correct for all conditions than it did to code the graphics - again this demonstrates the user-friendliness of _MAPTRIANGLE(3D).


The first video shows the running program with consecutive tails and heads.  The graphics are pretty convincing, with realistic coin rotation, movement, landing and settling.  When the coin has landed it falls to the nearest horizontal angle.  The coin has no inertial mass even though it has gravitational mass.  The coin rotation rate (and overall speed) is a compromise between showing the full movement and the time taken for a tossing event.  With a faster display speed the spinning would hardly be noticeable, but even so a single event takes much longer than a real coin would.  Even so, the rotation is rather difficult to appreciate and even appears unnatural at times (somewhat distorted by the perspective of the 3D space).  The second video shows the process happening at a slowed-down rate and it is clear that the coin truly spins as in reality.




To run the program, download and extract the zip file.  Place the folder "Flippin' Coin" in your QB64 folder and load the Coin Toss bas file.

https://www.dropbox.com/s/4navhbwrta780e4/Flippin%27%20Coin.zip?dl=1

Press the Spacebar to initiate a toss.  The coin will land either heads up or tails up (this you would expect!).  Depending upon which of the four edge corners the coin lands on, it will either settle backwards as in the video (this is by far the usual case) or forwards.  You may have as many goes as you wish.  Esc to Quit.


Data gathering was performed using a calculation-only program (no graphics) over 2000 cycles and it was found that the heads-tails ratio was 50-50 to within 1% - pretty good!  Because of the simple assumptions and idealised mathematical model there may be a slight bias for one particular face (if there was, it was tails).


The display could be enhanced with more appealing additional graphics, and I had intended to add an animated launch spigot, but as-is the program does its job of showing _MAPTRIANGLE(3D)'s capabilities.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Coin Toss Graphics Program (_MAPTRIANGLE(3D) Demonstration)
« Reply #1 on: August 14, 2021, 09:53:47 am »
That's pretty cool. Now onto 4D where the gold coin flips out of the screen onto my desk. Nicely done and very informative.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Coin Toss Graphics Program (_MAPTRIANGLE(3D) Demonstration)
« Reply #2 on: August 14, 2021, 11:40:32 am »
Weird. The slow-mo one looks more like a real flipping coin. So either the regular speed is too slow, or too fast to look as realistic. It gave me the impression of a butterfly effect. I mean in an actual coin toss, the human eye would only make see a blur. That type of special effect might be quite a challenge.

I'm curious with the leverage of using OpenGL and  _maptriangle just how much code was required to put this together. Back in the day, this would have required a lot of coding, and probably not enough speed of execution to make it nearly as realistic.

I love the coin. It makes me miss the days when American coins looked like something to admire. Now the crap we produce here looks like the play money I had as a kid. Lucky for me, I invested that play money, and in a couple of more years, if I play my Chance Cards right, I'll be mortgage free on Boardwalk and Park Place!

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

FellippeHeitor

  • Guest
Re: Coin Toss Graphics Program (_MAPTRIANGLE(3D) Demonstration)
« Reply #3 on: August 14, 2021, 06:04:24 pm »
It looks amazing, Richard! Really well done.

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: Coin Toss Graphics Program (_MAPTRIANGLE(3D) Demonstration)
« Reply #4 on: August 15, 2021, 04:29:34 am »
Erratum:  For the technically pernickety,  there is an error in my descriptions above.  The following sentence

The coin travels in a straight line (the x- axis of the _MAPTRIANGLE(3D) space) with constant speed.

should read:

The coin travels in a straight line (the z- axis of the _MAPTRIANGLE(3D) space) with constant speed.  The coin spins on the x-axis.

If that Odin guy hadn't stopped us being able to correct posts, I could have snuck the amendment in without anyone noticing the technical inaccuracy.  That Odin!  Must think he's some sort of god.


@Dimster @Pete @FellippeHeitor Thanks for you positive comments.  It was a pleasure to be able to post again a small contribution to the site.

Richard

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Coin Toss Graphics Program (_MAPTRIANGLE(3D) Demonstration)
« Reply #5 on: August 15, 2021, 04:57:53 am »
Very nice work, @Qwerkey!

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: Coin Toss Graphics Program (_MAPTRIANGLE(3D) Demonstration)
« Reply #6 on: August 15, 2021, 10:25:34 am »
@Pete raises entirely the point I was emphasising about the considerable user-friendliness of _MAPTRIANGLE(3D) and the simplicity of coding for him/her.  The only code required for the coin in the program is:

Code: QB64: [Select]
  1. 'Coin Obverse & Reverse            
  2. _MAPTRIANGLE (Radius%, Radius%)-(MapFrom!(N%, 0), MapFrom!(N%, 1))-(MapFrom!(N%, 2), MapFrom!(N%, 3)), Obverse& TO(SpinMeRound!(0, 0, 0), SpinMeRound!(0, 0, 1), SpinMeRound!(0, 0, 2) + Offset%)-(SpinMeRound!(0, 1, 0), SpinMeRound!(0, 1, 1), ZTemp!)-(SpinMeRound!(0, 2, 0), SpinMeRound!(0, 2, 1), ZTemp2!)
  3.  _MAPTRIANGLE (Radius%, Radius%)-(MapFrom!(N%, 4), MapFrom!(N%, 5))-(MapFrom!(N%, 6), MapFrom!(N%, 7)), Reverse& TO(SpinMeRound!(1, 0, 0), SpinMeRound!(1, 0, 1), SpinMeRound!(1, 0, 2) + Offset%)-(SpinMeRound!(1, 1, 0), SpinMeRound!(1, 1, 1), ZTemp1!)-(SpinMeRound!(1, 2, 0), SpinMeRound!(1, 2, 1), ZTemp3!)
  4. 'Coin Edge
  5. _MAPTRIANGLE (0, 0)-(0, EdgeLess1%)-(EdgingLess1%, EdgeLess1%), EdgePart& TO(SpinMeRound!(0, 1, 0), SpinMeRound!(0, 1, 1), ZTemp!)-(SpinMeRound!(1, 1, 0), SpinMeRound!(1, 1, 1), ZTemp1!)-(SpinMeRound!(1, 2, 0), SpinMeRound!(1, 2, 1), ZTemp3!)
  6. _MAPTRIANGLE (0, 0)-(EdgingLess1%, 0)-(EdgingLess1%, EdgeLess1%), EdgePart& TO(SpinMeRound!(0, 1, 0), SpinMeRound!(0, 1, 1), ZTemp!)-(SpinMeRound!(0, 2, 0), SpinMeRound!(0, 2, 1), ZTemp2!)-(SpinMeRound!(1, 2, 0), SpinMeRound!(1, 2, 1), ZTemp3!)

Just four lines, although each line is quite long.  The _MAPTRIANGLE(3D) processing handles everything needed for the display output.  The user does not have to worry about which parts are occluded by those in front - everything is already handled.

The QB64 folks who coded _MAPTRIANGLE(3D) have thought of everything beforehand - the work of geniuses.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Coin Toss Graphics Program (_MAPTRIANGLE(3D) Demonstration)
« Reply #7 on: August 15, 2021, 12:35:15 pm »
Just four lines, even long lines, is remarkable for such an effect. I recall the QB era and how involved making a similar less realistic effect this would be. The blessing was challenges like that would really get the brain cooking. I loved that feeling, but I'd usually quit the project part way through, which I guess is why other members my programs half-baked. Kidding aside, as much as I like to figure out the component parts, at some point you have to get some actual results with many such "parts" working together. _MAPTRIANGLE certainly seems to provide that, with one exception, it lacks the sophistication to work in SCREEN 0.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Coin Toss Graphics Program (_MAPTRIANGLE(3D) Demonstration)
« Reply #8 on: August 15, 2021, 02:33:03 pm »
From what I remember, maptriangle (3d) works with hardware images, so it can work with your SCREEN 0 programs as well.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Coin Toss Graphics Program (_MAPTRIANGLE(3D) Demonstration)
« Reply #9 on: August 18, 2021, 06:33:22 pm »
I can say Cool!
A good example to learn _Maptriangle(3D) and its powerful graphic.
Thanks to share.
Programming isn't difficult, only it's  consuming time and coffee