Search Results

Monday, February 28, 2011

Using Python in the Blender Game Engine - Part 5

Hey. So it's been a little while since the last post about this subject, but it's still going strong. In this tutorial, we work a little more using Python with the BGE game Maze Crawl.

So if you haven't been following, Maze Crawl is the little project that I've been making a tutorial about here on Game Up!, and it's gotten pretty good. We have movement and collision. In this part, we'll be adding a couple of things - a better looking Player and a camera system. Now, then, let's take a look at the new Maze Crawl...

Well, it's starting to look much more like a game now, isn't it? I've changed the character from a cube to a more interesting looking character. Not bad.

Now, then. There's not too much going on for the camera in terms of logic bricks - there's just an Always sensor plugged into a Python controller running the camera script (Game.Camera). The script's pretty simple:

from bge import logic

def Camera():

    cont = logic.getCurrentController()
    obj = cont.owner
    sce = logic.getCurrentScene()
   
    def Init():
       
        if not 'init' in obj:
           
            obj['init'] = 1
            obj['target'] = sce.objects['Player']
            obj['depth'] = obj.position.y - obj['target'].position.y
           
    def Update():
   
        tarpos = obj['target'].worldPosition
        pos = obj.worldPosition
       
        soft = .1
        obj.position.x += (tarpos.x - pos.x) * soft
        obj.position.y += (tarpos.y + obj['depth'] - pos.y) * soft
   
    Init()
    Update()

I should've commented the script moreso, but we'll go through it, and there's nothing here that's too hard to understand. First, we have the camera set its target object in the initialization function. At the beginning, it's set to watch the Player. Also, we get the depth of the camera (how far away from the player the camera is) by subtracting the Player's Y-position from the camera's Y-Position.

After that, we move the camera by adding to the camera's position the difference between the camera's position and the Player's position, taking into account the camera's depth for the Y-axis. Also, we don't do this exactly, but instead move the camera by a fraction of this movement amount - this way, the camera won't snap to the Player's position but will rather 'float' to focus on the Player. The soft variable controls this snappiness.

So, that's the only code running on the camera - the main code difference is in the Player's code. Now, I ran into a bit of a problem making this part of the tutorial series. I had some trouble because part of a game developement tutorial is about making a fun, functional game, but a major part of this particular series is about doing so using Python. What I mean is that I had some trouble figuring out either to go with the Bullet physics engine to produce efficient movement for our character, or to continue doing so purely with Python (handling collision detection, movement, acceleration, friction, etc.). I decided to go about doing this with both methods - utilizing the Bullet physics engine as well as controlling the physics ourselves.

I made a change to the logic brick setup of the Player to work well with Bullet - that change can be seen to the left. The Motion actuator is now set to Servo Motion (available because the Player is a Dynamic physics object). I also set up a Stop actuator, which is a Motion actuator who's specific (and only) function is to stop the Player's movement. I'll post up the source of the new Player function in the next part of this, as the source is a bit long to post in one go. As always, have fun!

9 comments:

  1. A good and short tutorial.
    I am sure that the tutorials, as a form of education are a huge step in the promotion and education.
    I'm glad there are people interested in scripting for Blender 3D.

    ReplyDelete
  2. Thanks, I'm glad that there are people reading this. I'll keep making them - if there's any questions or comments, feel free to leave a message here or contact me via e-mail.

    ReplyDelete
  3. This is really helpful stuff! A lot of the things you cover have been hard fro me to locate in online docs and forums. :)

    ReplyDelete
  4. Hey, thanks. I'll keep trying to make good tutorials - the next part in the series should be better than this one.

    ReplyDelete
  5. Yeah, keep up the good work. The API overhaul for Blender 2.5 has thrown off a lot of people (myself included). Tutorials like these, especially in the early days of Blender 2.5, are going to help bring a lot of people up to speed.

    ReplyDelete
  6. I'm having a problem here. My camera isn't tracking the player. I compared my script to yours using Notepad++ and the files match. So I must have made a mistake with the Camera logic bricks.

    I added an Always sensor to the Camera object, connected it to a Python controller set to Module mode, and entered Game.Camera as the module name and function. I saved the script in a file called Game.py in the same directory as the blend file. Where did I go wrong?

    ReplyDelete
  7. Me again. I figured out what I missed -- I forgot to click the """ button on the Camera Actuator (the one that says Activate TRUE level triggering (pulse mode) when you hover over it).

    Everything's great now.

    ReplyDelete
  8. hey nice work..
    i dont think you hv attached your source file here.

    ReplyDelete
  9. @Anonymous - Yes, I didn't attach the source file because it's in the next part in this series (Part 5.5). You can get it here:

    http://solarlune-gameup.blogspot.com/2011/02/python-in-blender-game-engine-part-55.html

    ReplyDelete