Roulette

I am going to make a roulette game. You know… with the little ball and roulette wheel…
I have done this before in VB and in Javascript. Its kind of a “Hello world” for me I guess. The problem is… I have been sitting here for an hour, wondering where to start.
Here are some things im tossing up, any suggestions will help.

How should the wheel spin? Through script? If so, ill need to get into that. Through some kind of bump or push physics?
How should the ball meets number be detected? The wheel is one object, It could be through material types. But will that require script like… if(wheel stops) then detect material its touching? Or… can logic bricks be ok for that? Or… should seperate objects be drawn below the actual wheel, and use a ray to detect which object the ball is directly over.

Or… maybe just record an animation of the wheel… play it when play is pressed, then as the wheel starts to stop, zoom in on the appropriate random number that script has created…

Hmm… any suggestions?

Thank you very much. At least that gives me a direction to go.

Im working on spinning the wheel by using this code, and I have a couple questions.

import bge


def main():

    cont = bge.logic.getCurrentController()
    own = cont.owner

    #we can say the speed.. is the rotation z value
    
    
    speed = 30
    
    
    #for i in range(5): 
      #print(i)
    while(speed > 0):
       speed = speed - .01
       print(speed)
       own.applyRotation([0,0,speed],True)

main()

This executes when I press “S”
Well… the graphics do not follow this. The graphics only update when I initially press the “S” and also when I release the “S”. First, I only need it to update when the initial “S”… for spin… is pressed. Then update while the loop is running to show the spinning wheel.
Any suggestions?

I’ve actually been making a roulette game, myself. I’ve pretty much finished the wheel spinning portion, so I’ve got some insight into the problems you’re facing.

Monster’s solution is probably the simplest. If you wanted to add more realism you could consider giving the wheel a random starting speed, and using python to get it to decelerate smoothly. If you just want something that just works, though, this is probably overkill.

How should the ball meets number be detected? The wheel is one object, It could be through material types. But will that require script like… if(wheel stops) then detect material its touching? Or… can logic bricks be ok for that? Or… should seperate objects be drawn below the actual wheel, and use a ray to detect which object the ball is directly over.
The difficulty here is that on a real roulette wheel, the ball lands on the spinning portion of the wheel, but the numbers are actually on the outside of the wheel. There are several solutions to this problem. The easiest is to just break with reality and put the numbers on the wheel, then use the touch sensor to get the property.
Similarly, you could try making the wheel really small, so that the number touches the ball when the wheel stops. I’d worry about the physics on this one, though: the ball may not behave properly with a small wheel (you’d have to test this one), and there is also the possibility that the ball may not touch the slot on rare occasions, even if it works every time you try it. Physics can be a bit unpredictable that way.
The best solution, assuming you don’t want to delve into math, is probably to determine which slot the ball is nearest to using the game_object.getDistanceTo(other) method in python. Whichever slot you are closest to is the one the ball landed on.
My method was to use math to get the answer; Each slot on the wheel has a corresponding range of angles from the center of the wheel. As such, if you know where the ball is in relation to the center of the wheel, you can determine which slot you’re in. This method is the fastest I was able to come up with, and is pretty much guaranteed to work, but it can get a bit tricky in implementation. If you’re interested, I could go into more detail, but it may take me awhile to remember how I got it to work :wink:

There is one other concern with the ball, although relatively minor. Make sure to place an invisible collision object around your wheel to prevent ring-outs. A UV sphere with mesh collision is good for this, but I found that a plane with box collision works well enough, with much less demand on physics.

You’re going to need to put this code on an Always sensor, and find a way to set an initial speed. Personally, I would put the this into the script. If you don’t know how to read key presses, in your case, you would use bge.logic.keyboard.events[bge.events.SKEY] to get the state of the ‘S’ key.
Otherwise, you could use the Keyboard sensor to set the property.

Ok. wheel spins good, ball drops, etc. Time to detect where the ball is resting.
Here was a suggestion from @Monster :
“Determine when the ball comes to rest
On Changing of the slot number you can reset a timer property to zero. You can set a time limit (e.g. 1 second) the ball needs to keep the same slot number (but not -1).
After the time limit this number is the thrown number.”
… That is kind of what im going for.

@Galan, I have read over your suggestions too, ty.

I think I have a good solution, but don’t know how to implement it.
What I have so far: Ball drops into the spinning wheel, and G-force forces it toward the outside of the numbers… so far so good.
There are detection cubes on the outside of the numbers for the ball to rest against. Which works great so far.
In physics… it may rest against the cube for 30 game frames, then slight bump off, then rest again… which is fine.
Also, when bouncing around, it will be bumping into these detection boxes randomly.

What I want to try to do is something like this.


   If(TouchDetectionCube) then
      AllOtherDetectionCounters = 0; <i>//All other detection cubes counter will be set to 0</i>. <i>Need to do that because the ball still might be bouncing.</i>
       ThisCubeCounter++ <i>//The cube that it is touching will be updated once per game cycle</i>. 
  end if

 If(ThisCubeCounter = 100) Then
        This is our winning number
end if

Here is what I have so far for detection. It is attached to a detection cube.


import bge


def main():
    #IF it can loop around soo many times, without
    #triggering another number, then this must be the number
    

    cont = bge.logic.getCurrentController()
    #own = cont.owner

    sens = cont.sensors['Touch']
    #actu = cont.actuators['myActuator']

    if sens.positive:
      print("positive") #this runs every game cycle
    #else:
       #print("not positive") #doesn mean its not there, just means its not touching at the moment

main()


My questions are… Can I create global arrays somewhere? If so, that would be great.
Do I have to create 38 game properties maybe, and use them as counters?
Do I have to create 38 different scripts and attach them to each detection cube?

The hardest part is figuring out what tools I have available, and how exactly blender runs them. Like… How can I initiate some variables to use throughout all the scripts, etc.

Ok guys, if you can help, thank you. Sorry to bug you. No worries though, not long from now, ill be helping answer the questions. =)

I think I kind of have it… Im creating a script to run once, using the
import GameLogic as GL
so… this might work… somehow.

I found a way… but hit a bump… yawn… posting then going to bed. solved Going to bed… now.

This gets called from any detection cube that the ball hits or rests on.
I cant figure out why some of it is not executing though…


import bge
import GameLogic as GL

def main():
    #IF it can loop around soo many times, without
    #triggering another number, then this must be the number
    

    cont = bge.logic.getCurrentController()
    own = cont.owner #<b>&lt;--------- CHANGED THIS TO cont.owner.name now it is a string.. not an object so it works now....</b>

    sens = cont.sensors['Touch']
    #actu = cont.actuators['myActuator']

    if sens.positive:
        updateTouch(own)
   #else:
       #print("not positive") #doesn mean its not there, just means its not touching at the moment

def updateTouch(obj):
        
    if obj == GL.CurrentDet: # then dont worry about clearing the rest of them.
      print("obj is current",obj)
      
      if obj == "det1": #The name of the detection object is in fact "det1" 
        print("updating obj1")
        Gl.det1 = GL.det1 + 1 #THIS NEVER GETS EXECUTED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      if obj == "det2":
        print("updating obj2")
        GL.det2 = GL.det2 + 1 #THIS NEVER GETS EXECUTED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   
    else: #clear the rest to 0, because were starting new
      print("ClearingAll")
      GL.det1 = 0
      GL.det2 = 0
      GL.det3 = 0
      GL.det4 = 0
      GL.det5 = 0
      GL.det6 = 0
      GL.det7 = 0
      GL.det8 = 0
      GL.det9 = 0
      GL.det10 = 0
      GL.det11 = 0
      GL.det12 = 0
      GL.det13 = 0
      GL.det14 = 0
      GL.det15 = 0
      GL.det16 = 0
      GL.det17 = 0
      GL.det18 = 0
      GL.det19 = 0
      GL.det20 = 0
      GL.det21 = 0
      GL.det22 = 0
      GL.det23 = 0
      GL.det24 = 0
      GL.det25 = 0
      GL.det26 = 0
      GL.det27 = 0
      GL.det28 = 0
      GL.det29 = 0
      GL.det30 = 0
      GL.det31 = 0
      GL.det32 = 0
      GL.det33 = 0
      GL.det34 = 0
      GL.det35 = 0
      GL.det36 = 0
      GL.det37 = 0
      GL.det38 = 0
      GL.CurrentDet = obj #resets the current object to count


main()



BTW, am I in the wrong forum? Should I break the game up into a few different forums… one for coding, and one for generaly physics/modeling stuff?

You are right in the game forum.

But you can split the thread into threads dealing with single issues.