用Python和Pygame写游戏-从入门到精通(18)

本文转自:http://eyehere.net/2011/python-pygame-novice-professional-18/

3D是非常酷的技术,同时也就意味着更多的工作,上次的简单介绍之后,这次还要讲更多2D到3D的新概念。

基于时间的三维移动

我们使用Vector3类来进行3D上的移动,与2D非常类似,看下面一个例子:

3D 目标向量

直升机A在(-6, 2, 2)的位置上,目标是直升机B(7, 5, 10),A想摧毁B,所以发射了一枚火箭AB,现在我们得把火箭的运动轨迹过程给画出来,否则一点发射敌机就炸了,多没意思啊~~ 通过计算出两者之间的向量为(13, 3, 8),然后单位化这个向量,这样就可以在运动中用到了,下面的代码做了这些事情。

from gameobjects.vector3 import *
A = (–6, 2, 2)
B = (7, 5, 10)
plasma_speed = 100. # meters per second
AB = Vector3.from_points(A, B)
print "Vector to droid is", AB
distance_to_target = AB.get_magnitude()
print "Distance to droid is", distance_to_target, "meters"
plasma_heading = AB.get_normalized()
print "Heading is", plasma_heading
#######输出结果#########
Vector to droid is (13, 3, 8)
Distance to droid is 15.5563491861 meters
Heading is (0.835672, 0.192847, 0.514259)

然后不停的重绘火箭的位置,用这个语句:
rocket_location += heading * time_passed_seconds * speed

不过我们还不能直接在pygame中绘制3D物体,得先学习一下下面讲的,“如何把3D转换为2D”。

3D透视

如果您初中美术认真学了的话,应该会知道这里要讲什么,还记得当初我们是如何在纸上画立方体的?

忘了?OK,从头开始说起吧,存储、计算3D坐标是非常容易的,但是要把它展现到屏幕上就不那么简单了,因为pygame中所有的绘图函数都只接受2D坐标,因此,我们必须把这些3D的坐标投影到2D的图面上。

平行投影

最简单的投影方法是——把第三个坐标z坐标给丢弃,用这样的一个简单的函数就可以做到:

def parallel_project(vector3):
    return (vector3.x, vector3.y)


尽管这样的转换简单又快速,我们却不能用它。为什么?效果太糟糕了,我们甚至无法在最终的画面上感受到一点立体的影子,这个世界看起来还是平的,没有那个物体看起来比其他物体更远或更近。就好像我右边这幅图一样。

立体投影

在3D游戏中使用的更为广泛且合理的技术是立体投影,因为它的结果更为真实。立体投影把远处的物体缩小了,也就是使用透视法(foreshortening),如左图所示,然后下面是我们的转换函数,看起来也很简单:









def perspective_project(vector3, d):
    x, y, z = vector3
    return (x * d/z, –y * d/z)

与上一个转换函数不同的是,这个转换函数还接受一个d参数(后面讨论),然后所有的x、y坐标都会接受这个d的洗礼,同时z也会插一脚,把原本的坐标进行缩放。

d的意思是视距(viewing distance),也就是摄像头到3D世界物体在屏幕上的像素体现之间的距离。比如说,一个在(10, 5, 100)的物体移动到了(11, 5, 100),视距是100的时候,它在屏幕上就刚好移动了1个像素,但如果它的z不是100,或者视距不是100,那么可能移动距离就不再是1个像素的距离。有些抽象,不过玩过3D游戏的话(这里指国外的3D大作),都有一种滚轮调节远近的功能,这就是视距(当然调的时候视野也会变化,这个下面说)。

在我们玩游戏的时候,视距就为我们的眼睛到屏幕的直线距离(以像素为单位)。

视野

那么我们怎么选取一个好的d呢?我们当然可以不断调整实验来得到一个,不过我们还可以通过视野(field of view)来计算一个出来。视野也就是在一个时刻能看到的角度。看一下左图的视野和视距的关系,可以看到两者是有制约关系,当视野角度(fov)增大的时候,d就会减小;而d增加的话,视野角度就会减小,能看到的东西也就变少了。

视野是决定在3D画面上展现多少东西的绝好武器,然后我们还需要一个d来决定透视深度,使用一点点三角只是,我们就可以从fov计算出d,写一下下面的代码学习学习:
在Internet上,你总是能找到99%以上的需要的别人写好的代码。不过偶尔还是要自己写一下的,不用担心自己的数学是不及格的,这个很简单~ 很多时候实际动手试一下,你就能明白更多。

from math import tan
def calculate_viewing_distance(fov, screen_width):
    d = (screen_width/2.0) / tan(fov/2.0)
    return d


fov角度可能取45~60°比较合适,这样看起来很自然。当然每个人每个游戏都有特别的地方,比如FPS的话,fov可能比较大;而策略性游戏的fov角度会比较小,这样可以看到更多的东西。很多时候还需要不停的变化fov,最明显的CS中的狙击枪(从没玩过,不过听过),开出来和关掉是完全不同的效果,改的就是视野角度。

今天又是补充了一大堆知识,等不及了吧~我们下一章就能看到一个用pygame画就的3D世界了!



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PythonPygame游戏-从入门到精通 中文pdf版本 部分目录如下: 系统学习部分 用PythonPygame游戏-从入门到精通(1) Pygame的历史,安装Pygame,第一个Pygame脚本 用PythonPygame游戏-从入门到精通(2) 理解游戏中的事件 用PythonPygame游戏-从入门到精通(3) Pygmae的屏幕显示 用PythonPygame游戏-从入门到精通(4) 使用字体模块,Pygame 的错误处理 用PythonPygame游戏-从入门到精通(5) 像素和颜色 用PythonPygame游戏-从入门到精通(6) 使用图像,理解Surface 用PythonPygame游戏-从入门到精通(7) 绘制图形 用PythonPygame游戏-从入门到精通(8) 产生动画和控制帧率 用PythonPygame游戏-从入门到精通(9) 向量基础 用PythonPygame游戏-从入门到精通(10) 用户输入 用PythonPygame游戏-从入门到精通(11) 使用鼠标控制精灵。一个在鼠标旁不断游动的小鱼的例程。 用PythonPygame游戏-从入门到精通(12) 手柄操作,暂无 用PythonPygame游戏-从入门到精通(13) AI初探 用PythonPygame游戏-从入门到精通(14) 状态机 用PythonPygame游戏-从入门到精通(15) 开始AI编程 用PythonPygame游戏-从入门到精通(16) AI编程总结。一个蚂蚁采集食物,攻击蜘蛛的系统模拟例程。 用PythonPygame游戏-从入门到精通(17) 3D基础 用PythonPygame游戏-从入门到精通18) 3D中的概念 用PythonPygame游戏-从入门到精通(19) 第一个 3D 程序(伪)。一个空间中的3D立方体的例程。 用PythonPygame游戏-从入门到精通(20) 声音原理 用PythonPygame游戏-从入门到精通(21) 使用声音,播放音效。一个重力模拟金属球碰撞的例程。 用PythonPygame游戏-从入门到精通(22) 播放长时间的背景音乐。一个建议播放器的例程。 额外提高部分 用PythonPygame游戏-从入门到精通(py2exe编) 使用py2exe将pygame脚本转换为exe可执行文件 用PythonPygame游戏-从入门到精通(Sprite篇) 介绍Pygame中不是必须但又很重要的Sprite模块,游戏中的角色实现,大多都要靠它。 实践部分 用PythonPygame游戏-从入门到精通(实战一:涂鸦画板1) 一个类似于Windows画图板的小玩意儿,精简了很多功能但是有更帅的笔刷。这一次主要是将笔刷的实现。 用PythonPygame游戏-从入门到精通(实战一:涂鸦画板2) 加上了按钮,我们的涂鸦画板可以用了! 用PythonPygame游戏-从入门到精通(实战二:恶搞俄罗斯方块1) 俄罗斯方块,却有不是普通的俄罗斯方块。 用PythonPygame游戏-从入门到精通(实战二:恶搞俄罗斯方块2) 代码构架 用PythonPygame游戏-从入门到精通(实战二:恶搞俄罗斯方块3) 实现说明 用PythonPygame游戏-从入门到精通(实战二:恶搞俄罗斯方块4) 完成,提供下载 用PythonPygame游戏-从入门到精通(实战三:植物大战僵尸1)
Gain a fundamental understanding of Python’s syntax and features with the second edition of Beginning Python, an up–to–date introduction and practical reference. Covering a wide array of Python–related programming topics, including addressing language internals, database integration, network programming, and web services, you’ll be guided by sound development principles. Ten accompanying projects will ensure you can get your hands dirty in no time. Updated to reflect the latest in Python programming paradigms and several of the most crucial features found in the forthcoming Python 3.0 (otherwise known as Python 3000), advanced topics, such as extending Python and packaging/distributing Python applications, are also covered. What you’ll learn * Become a proficient Python programmer by following along with a friendly, practical guide to the language’s key features. * Write code faster by learning how to take advantage of advanced features such as magic methods, exceptions, and abstraction. * Gain insight into modern Python programming paradigms including testing, documentation, packaging, and distribution. * Learn by following along with ten interesting projects, including a P2P file–sharing application, chat client, video game, remote text editor, and more. Complete, downloadable code is provided for each project! Who is this book for? Programmers, novice and otherwise, seeking a comprehensive introduction to the Python programming language. About the Apress Beginning Series The Beginning series from Apress is the right choice to get the information you need to land that crucial entry–level job. These books will teach you a standard and important technology from the ground up because they are explicitly designed to take you from “novice to professional.” You’ll start your journey by seeing what you need to know—but without needless theory and filler. You’ll build your skill set by learning how to put together real–world projects step by step. So whether your goal is your next career challenge or a new learning opportunity, the Beginning series from Apress will take you there—it is your trusted guide through unfamiliar territory!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值