Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / Rolling Virtual Dice

Author
Message
Klingbeil
12
Years of Service
User Offline
Joined: 25th Mar 2012
Location:
Posted: 26th Nov 2015 00:05 Edited at: 26th Nov 2015 00:34
I want to roll some virtual dice and I've done a search of the forums and none of the results involved using a 3D model and physics. I've made a nifty little D6 model with pips if anyone wants to use it.

Can anyone point me at a guide/tutorial/example for rolling some virtual D6 and reading the dice?

Attachments

Login to view attachments
Rich Dersheimer
AGK Developer
14
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 27th Nov 2015 01:16
I'm sure there are some simple ways to find out which face is up with GetObjectAngle, but I'm very lazy and like to cheat.

So... I simply fixed three cubes to a die. By checking each cube's "Y" position relative to the die's "Y" position, you can quickly see which side is up.

In the image below,
if the red cube is higher than the die, then the roll is 1. If the red cube is lower, then the roll is 6.
if the blue cube is higher, then the roll is 4, if it is lower then the roll is 3.
If the green cube is higher, then roll is 2, if it is lower then the roll is 5.

The cubes can be tiny planes, and they can be invisible, I just made them long and visible for the example.
They can actually be inside the die, so they don't interfere with the collisions of the die.
You could even make 6 of them and just loop through to find which one is higher than the die.



Attachments

Login to view attachments
Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 27th Nov 2015 11:45 Edited at: 27th Nov 2015 11:46
If your dice model can have bones, and you can get the 3D position of those bones, then you could work it out that way. A while ago some of us collaborated on a dice game for the newsletter, kinda like Yahtzee with several dice. I added a bone in the centre of each face, and was able to wait until the dice stopped moving, then work out which bone is pointing up, just based on the Y position. I haven't had a chance to mess with much of the new 3D commands, but it might be possible in AppGameKit just like I did it in DBPro.

An alternate way to achieve the same thing, would be to have a hidden cube with no physics assigned to it, and position and rotate this to suit a dice, and then move it towards each 'face' and keep track of the Y position, the face with the highest Y coordinate is the one pointing up. This is much like Rich's suggestion, but using a single cube and stepping through the faces one by one to find to topmost. When you position the hidden cube, you can move it forward, backwards, left, right, up and down - checking the Y position each time... then you just need to know how the dice model is orientated to work out which face value this relates to. Like, if the 1 face is at the top of the die, position the hidden cube, and move it up - then that relates to the 1 face, and if this is the highest Y coordinate then 1 is pointing up no matter how the dice ends up after it's physics are finished.

I'd be happy to make an example of this, I managed to get the DBPro game working perfectly accurately with this method - the only fault is if a die lies at a funny angle - it will work out the very topmost face, but the player might not necessarily agree with it
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 27th Nov 2015 17:03
Quote: "A while ago some of us collaborated on a dice game for the newsletter"


I looked for this when I read the thread, but couldn't find it
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Rich Dersheimer
AGK Developer
14
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 27th Nov 2015 21:01
Excellent ideas, Van!

So I did this:



Makes a "medialess" die and rolls it. The die is a cube, with six planes fixed to it, each with it's own image applied. The code checks to see which face is highest.

Not the most elegant solution - I haven't played around much with friction and bounciness in the physics commands, so the die jitters sometimes.

Attachments

Login to view attachments
Klingbeil
12
Years of Service
User Offline
Joined: 25th Mar 2012
Location:
Posted: 27th Nov 2015 22:47
thanks guys, these are all great leads on how to approach this.

"GetObjectAngle" seems like it might be the best bet to start with, at least from my understanding, i could be wrong.
especially, when you start adding multifaceted dice outside of the standard cube (such as a D20). although, you'd have to figure out what the angle would be for each number when face up and that seems somewhat time consuming since the dice numbers are not simply laid out in a linear fashion.

i don't know how bones work. i made the D6 in Rhino3D and ran it through NetFabb, but that seems to have mucked up the corners.

i've attached the original D6

Attachments

Login to view attachments
Rich Dersheimer
AGK Developer
14
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 28th Nov 2015 05:46
One thing to consider... you could just generate a random number, then have the die bounce around a bit and then "magically" come to rest with the generated number on top. This would obviate the need to do any checking of the object angle.
AJCodes
9
Years of Service
User Offline
Joined: 6th Jan 2015
Location:
Posted: 30th Nov 2015 03:52
I'd go with Rich's solution but with a small twist. Make sure the center of the die is the 0,0,0 position, then attach objects to each face on the inside of the die. Set each small object's color to the face value so once you have the object you can read the color to get the value. Be really lazy and use collision detection with the start being centerX, centerY + radius, centerZ and the end being center, center, center. 2nd object it hits is the face that's up.
Born. Currently living.
David Gervais
Retired Moderator
18
Years of Service
User Offline
Joined: 28th Sep 2005
Location: Montreal, Canada
Posted: 4th Dec 2015 00:18 Edited at: 4th Dec 2015 00:23
Are you talking about Score 100! from newsletter number 100? Me thinks j00 are

http://www.thegamecreators.com/pages/newsletters/newsletter_issue_100.html

Cheers!
SpecTre
Developer
21
Years of Service
User Offline
Joined: 24th Feb 2003
Location: UK
Posted: 4th Dec 2015 17:04
On a side note, just read that newsletter as not seen it before. It's quite a good read and would be good to see the newsletter getting back to that quality. Superb.
The Amiga and Amos were great!
My website LEAP - Download Paint Pot here!

Login to post a reply

Server time is: 2024-04-19 15:35:23
Your offset time is: 2024-04-19 15:35:23