Page 1 of 4 123 ... LastLast
Results 1 to 20 of 62

Thread: Custom Cursor Script (NEW! Updated for 2.5+)

  1. #1
    Member blendenzo's Avatar
    Join Date
    Apr 2006
    Location
    Michigan, USA
    Posts
    2,702

    Custom Cursor Script (NEW! Updated for 2.5+)

    (This tutorial has been updated to work with Blender 2.5 and up. If you need the 2.34 thru 2.49 version of the script, you can find it here:
    http://blenderartists.org/forum/show...=1#post2025675)

    As I mentioned in the previous version, this method was first shared on BlenderArtists by NOR.J, but it was lost/forgotten over time. I have to give credit to Social for his part in inspiring my version of the custom cursor system, as well as his help in creating the initialization system.

    Blend File: Coming Soon...
    Version Info: Blend made with Blender 2.60. Tutorial compatible with v2.50 and up.
    Requirements: Basic knowledge of Blender GE interface and ability to "plug in" a pre-written Python script
    Helpful Knowledge: UV Mapping

    Step 1 - Setup
    In most cases, a custom cursor should be placed in a separate scene from the main game assets. If you have a user interface or a HUD you may find it convenient to add the cursor there. For the sake of simplicity, this tutorial will demonstrate how to add the cursor to an empty scene.

    Begin by adding a new empty scene and naming it something descriptive like "CursorOverlay". Now add some logic bricks to the main scene to intialize the overlay scene. I like to put this type of logic on the camera, but you may add it where you find it most convenient.



    Switch to the "CursorOverlay" scene and add a camera. Clear any initial rotation with [Alt + R] and move the camera so that it is centered about 15 Blender Units above the global origin. What you do next depends on whether you want to use a 2D cursor or a 3D cursor.

    For 2D cursors:
    Set the camera to Orthographic in the Object Data buttons and adjust the Orthographic Scale setting as you please (I like 10.000 or 15.000 personally).



    For 3D cursors:
    Leave the camera in Perspective mode, but set the camera's "Clipping: Start" value to something larger. 15 should work. (Later on, we will use a script to position the cursor on this clipping plane, so it's useful to move it away to a workable distance.)



    For Both:
    In camera view [Numpad 0], add a new plane. This will cause the new plane to be facing the camera. If you are using Perspective mode, move the plane slightly farther away from the camera than the "Clipping: Start" value you specified (this doesn't require a lot of precision) and resize it to look good.

    You'll probably want to add a nice custom cursor texture at this point. For the sake of this tutorial, I will assume that you know how to do that. Crosshairs should be centered on the plane. If you are using a pointer type cursor, make sure the point is at the top left corner of the plane.

    Step 2 - Centering the Cursor (Pointer type cursors only. Crosshairs skip to step 3.)
    The user will probably expect mouse clicks to be calculated from the tip of the cursor, so it is important to move the cursor's object center to this point.

    With the cursor object selected, enter Edit Mode [Tab] and move the plane so that the object center is located correctly (the object center is represented by an orange dot, as seen in the image below).



    Step 3 - The Script (Social's Modified Version, 2.5+)
    Copy this script and paste it into the Blender Text Editor. Name the script "CustomCursor.py"
    Code:
    from bge import render,logic
    
    cont = logic.getCurrentController()
    own = cont.owner
    mPosi = cont.sensors['MousePosi']
    
    ### Start mouse position at center of game screen 
    if own['init']:     
       render.setMousePosition(render.getWindowWidth()//2, render.getWindowHeight()//2)
       own['init'] = 0
    
    ### Move cursor to mouse position
    own.localPosition = mPosi.raySource
    Step 4 - Logic Bricks
    With the cursor selected, go to the Logic panel. Add two sensors (an Always and a Mouse) and one controller. Turn on "TRUE Level triggering (pulse mode)" on the Always sensor. Set the Mouse sensor to "Mouse Over Any" and name it "MousePosi". Set the controller to "Python" and set the script to "CustomCursor.py".



    You will also need to add a property named "init" and set it to a value of 1. (This is the property used by the script to center the mouse on the game screen at startup.)



    Conclusion
    It's that simple. Press "P" and watch it work. You'll notice that the cursor follows the movement of the mouse with good accuracy. If necessary, resize your cursor a bit to make it look good in game and your done.

    Enjoy,
    -blendenzo
    Last edited by blendenzo; 13-Jan-12 at 00:02.
    "From somewhere, a transmission..."
    Game Engine tutorials: TutorialsForBlender3D - blendenzo.com - blending-online
    My Projects: Escape the Room - Brought2Life - UpRight - Solomon's Treasure - Sokoban - Marble Labyrinth



  2. #2
    Member Chaser's Avatar
    Join Date
    Nov 2004
    Location
    BC, Canada
    Posts
    987
    Very nicley done Blendenzo! Works perfect here. It's no nice and simple, and so effective!

    Keep it up!

    -Chaser



  3. #3
    Member tarkata14's Avatar
    Join Date
    Oct 2006
    Location
    In a parellel Universe...o_O O_o
    Posts
    744
    Awesome! it works very nicely. and it can be applied to any object,Cool!



  4. #4
    Simple code, and it works perfectly. Thanks for that Blendenzo.



  5. #5
    Could i add that there should be a track-to actuator or halo mode on the mouse incase the camera moves?
    Last edited by Littlebob; 13-Feb-07 at 14:43.
    Check out my Blog.



  6. #6
    Member blendenzo's Avatar
    Join Date
    Apr 2006
    Location
    Michigan, USA
    Posts
    2,702
    LittleBob: I'm writing a tut right now, and I had that same thought, only I would say just make the camera the parent of the cursor.
    "From somewhere, a transmission..."
    Game Engine tutorials: TutorialsForBlender3D - blendenzo.com - blending-online
    My Projects: Escape the Room - Brought2Life - UpRight - Solomon's Treasure - Sokoban - Marble Labyrinth



  7. #7
    Member PlantPerson's Avatar
    Join Date
    May 2002
    Location
    Milky Way Galaxy
    Posts
    5,777
    blendenzo, if potatoes rocked, I would say you rocked like a potato. Excellent work, much better than the lame joke I tried to pull a few words back. I will be using this one, you can bet on it...
    "If we shadows have offended, Think but this, and all is mended, That you have but slumber'd here. While these visions did appear." - William Shakespeare
    Portfolio | Blog | Webcomic



  8. #8
    Member blender_rox's Avatar
    Join Date
    Dec 2004
    Location
    The USA (Florida).
    Posts
    742
    Thank you so much! I can really use this for my RPG I'm making! Thank you!



  9. #9
    ARG! Why is it that everything I download from blendenzo is an incomplete file? Always has been for me and can never figure it out! It does this to other files from a lot of people. Oh well anyways, I did the tutorial and works great

    ~Marshal~



  10. #10
    Member blendenzo's Avatar
    Join Date
    Apr 2006
    Location
    Michigan, USA
    Posts
    2,702
    Marshal,

    That's odd, but I don't think it's related to my server. I test most of my downloads to be sure they're complete before I post links to them. Anyway, I'm glad that you all find the tut /script/demo useful.
    "From somewhere, a transmission..."
    Game Engine tutorials: TutorialsForBlender3D - blendenzo.com - blending-online
    My Projects: Escape the Room - Brought2Life - UpRight - Solomon's Treasure - Sokoban - Marble Labyrinth



  11. #11
    Yeah, it's very use full, but I found one problem. It might just be the way I did it but, whenever I press P the cursor always starts off in the upper left hand corner. It's a small problem, but do you know how to fix this?



  12. #12
    Member
    Join Date
    Oct 2002
    Location
    Cambridge, UK
    Posts
    238
    Just a few things to add:

    - If your camera is moving, don't parent the cursor object to it. getRaySource() returns the position in world space, parenting only adds an extra transform. A plane set to halo is the best option.

    - There's a slight delay when using a moving camera, the cursor will appear to "jump" a little but this isn't noticable when the cursor object is further from the camera.

    - The position is on the near clip plane, so rather than move the cursor really really close and small, you can put it on an overlay scene linked to a dummy object, and adjust the cameras "start" value. Obviously don't move it too far or you'll notice clipping errors with your scene.

    @Meat_Beater - Until you move the mouse, I assume the mouse sensor defaults to the windows origin, which is the upper left corner for OpenGL.

    Piran



  13. #13
    Member Stu_Flowers's Avatar
    Join Date
    Dec 2006
    Location
    Oregon
    Posts
    526
    YES! This is great! I was using a different "customCursors" script, but this beats that tar out of that one!! This will fit nicely into my RTS...I will be sure to mention your name! (Not that it's a ground-breaking production or anything)

    ~~Stu_Flowers
    Old MGP (Monkey Game Project) Website: --HERE--



  14. #14
    Member blendenzo's Avatar
    Join Date
    Apr 2006
    Location
    Michigan, USA
    Posts
    2,702
    Piraniac: Thanks for the tips. I'll update my tutorial to reflect those points. I didn't know that halo was faster than parenting. Does halo not use transforms?
    "From somewhere, a transmission..."
    Game Engine tutorials: TutorialsForBlender3D - blendenzo.com - blending-online
    My Projects: Escape the Room - Brought2Life - UpRight - Solomon's Treasure - Sokoban - Marble Labyrinth



  15. #15
    Member Ammusionist's Avatar
    Join Date
    Dec 2003
    Location
    City of Churches, Oz
    Posts
    1,756
    I assume there's nothing stopping you using a model instead of a plane as a cursor?
    (e.g. Modeling a pointer, hand, gun, tool, persimmon etc.) as long as the hot point is the object center right?
    "It's a primitive culture; I'm just trying to blend-in!" - Inebriated Counsellor Troi
    "You're blended alright." - Commander Riker
    Star Trek - First Contact



  16. #16
    Member blendenzo's Avatar
    Join Date
    Apr 2006
    Location
    Michigan, USA
    Posts
    2,702
    Originally Posted by Ammusionist View Post
    I assume there's nothing stopping you using a model instead of a plane as a cursor?
    (e.g. Modeling a pointer, hand, gun, tool, persimmon etc.) as long as the hot point is the object center right?
    You would be correct in making that assumption. I would go with the persimmon, personally.
    "From somewhere, a transmission..."
    Game Engine tutorials: TutorialsForBlender3D - blendenzo.com - blending-online
    My Projects: Escape the Room - Brought2Life - UpRight - Solomon's Treasure - Sokoban - Marble Labyrinth



  17. #17
    Member PlantPerson's Avatar
    Join Date
    May 2002
    Location
    Milky Way Galaxy
    Posts
    5,777
    Anyone who's having problems downloading:
    I'm mirroring it for you at my site. Click
    "If we shadows have offended, Think but this, and all is mended, That you have but slumber'd here. While these visions did appear." - William Shakespeare
    Portfolio | Blog | Webcomic



  18. #18
    Member Social's Avatar
    Join Date
    Nov 2005
    Location
    New Jersey, USA
    Posts
    3,596
    Originally Posted by Meat_Beater View Post
    Yeah, it's very use full, but I found one problem. It might just be the way I did it but, whenever I press P the cursor always starts off in the upper left hand corner. It's a small problem, but do you know how to fix this?
    Add an integer prop to your cursor plane named "init" (no quotes), and set it to 1.

    Then modify your code like this:

    Code:
    import Rasterizer as R
    cont = GameLogic.getCurrentController()
    own = cont.getOwner()
    mPosi = cont.getSensor("MousePosi")
    
    if own.init == 1:
        R.setMousePosition(R.getWindowWidth()/2, R.getWindowHeight()/2)
        own.init = 0
    
    cursorPosi = mPosi.getRaySource()
    own.setPosition(cursorPosi)
    Yea, I don't like the cursor coming out of nowhere like that either. Hope this is what you are looking for.

    Blendenzo>

    Do I even have to say anything anymore? It's awesome of course, like everything else you do. Actually, now that I think about it.....you never published anything mediocre really, it was always awesome stuff one way or the other.

    Kudos to you buddy, kudos.
    Last edited by Social; 13-Feb-07 at 21:00.
    It is in the nature of powerless people to vicariously enjoy the exercise of arbitrary power by others.

    - Dmitry Orlov



  19. #19
    Thanks Social, It obviously worked! Blendenzo yours still worked 100 % of the time

    Thanks guys

    ~Marshal~



  20. #20
    Member
    Join Date
    Oct 2002
    Location
    Cambridge, UK
    Posts
    238
    Does halo not use transforms?
    Everythings got a transform somewhere, the problem with parenting is the cursor inherits it's parents transformation as well as being set directly by your script - resulting in wierdness. Really all you want from the parent is an orientation, hence you can use halo to aim at the camera while your script deals with the position.

    It's a cool demo though, thanks for sharing.

    Piran



Page 1 of 4 123 ... LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •