标签:
作者:i_dovelemon
来源:CSDN
日期:2015 / 7 / 11
主题:Interpolate,Bezier Curve
从今天開始。将会陆陆续续的向大家介绍什么是插值技术?以及在计算机视频游戏开发中常常使用的插值技术有哪些。
这种技术能够在诸如动画系统等游戏内容中得到使用。就拿动画系统举例。如今的动画系统大都流行一种基于关键帧的动画。
美术人员在一些动画制作软件中。如flash。制作好动画的关键帧。然后就将该动画导出成一个文件。
这个文件里仅仅保存了每个动画的关键运动部分。
那么。当我们在游戏中使用的时候,就须要通过程序来补充每两个关键帧之间的帧状态数据。这种通过两个首尾值来构建中间部分值的技术就称之为插值技术。本文就会向大家展示当中一种常常使用的插值技术的数学技术。它的实际应用将在兴许章节向大家展示。
它是一些曲线几何的总称。在本文中,将会向大家展示三种Bezier Curve。各自是Linear Bezier Curve, Quadratic Bezier Curve, Cubic Bezier Curv。
这三种Bezier Curve都是经经常使用到的曲线。而且各自是一次曲线。二次曲线和三次曲线。其它更高维度的曲线将不再讲述。关于Bezier Curve的具体解说。大家能够看下这篇Wiki:Bezier Curve.
<span style="font-family:Microsoft YaHei;"><html> <script> var _2dContext = 0; /*Begin Solve Bezier Curve method*/ function LinearBezierCurve(p0,p1,t) { return p0 + (p1 - p0) * t; } function QuadBezierCurve(p0,p1,p2,t) { var current = 0; var InvT = 1 - t; var InvT_2 = InvT * InvT; var T2 = t * t; current = InvT_2 * p0; current += 2 * InvT * t * p1; current += T2* p2; return current; } function CubicBezierCurve(p0,p1,p2,p3,t) { var current = 0; var InvT = 1 - t; var InvT_2 = InvT * InvT; var InvT_3 = InvT_2 * InvT; var T2 = t * t; var T3 = T2 * t; current += InvT_3 * p0; current += 3 * InvT_2 *t * p1; current += 3 * InvT * T2 * p2; current += T3 * p3; return current; } /*End Solve Bezier Curve*/ //Draw a point in the cavans function DrawPoint(context,x,y,color) { context.beginPath(); context.moveTo(x,y); context.lineTo(x + 1,y); context.closePath(); context.strokeStyle = color; context.stroke(); } /*Begin Draw Bezier Curve*/ function DrawLinearBezierCurve(context,p0_x,p0_y,p1_x,p1_y,step,color) { var t = 0; for(;t<=1;t+=step) { //Calculate the new position var current_x = LinearBezierCurve(p0_x,p1_x,t); var current_y = LinearBezierCurve(p0_y,p1_y,t); //Draw the point DrawPoint(context,current_x,current_y,color); } } function DrawQuadBezierCurve(context, p0_x,p0_y,p1_x,p1_y,p2_x,p2_y,step,color) { var t = 0; for(;t<=1;t+=step) { //Calculate the new position var current_x = QuadBezierCurve(p0_x,p1_x,p2_x,t); var current_y = QuadBezierCurve(p0_y,p1_y,p2_y,t); //Draw the point DrawPoint(context,current_x,current_y,color); } } function DrawCubicBezierCurve(context,p0_x,p0_y,p1_x,p1_y,p2_x,p2_y,p3_x,p3_y,step,color) { var t = 0; for(;t<=1;t+=step) { //Calculate the new position var current_x = CubicBezierCurve(p0_x,p1_x,p2_x,p3_x,t); var current_y = CubicBezierCurve(p0_y,p1_y,p2_y,p3_y,t); //Draw the point DrawPoint(context,current_x,current_y,color); } } /*End Draw Bezier Curve*/ function gameInit() { //Get the canvas context _2dContext = document.getElementById("canvas").getContext("2d"); //enable game loop setInterval(gameLoop,100.0); } function gameLoop() { //Clear the canvas _2dContext.clearRect(0,0,1024,768); //Draw the linear bezier curve DrawLinearBezierCurve(_2dContext,100,100,1000,100,0.001,"rgb(255,0,0)"); //Draw the Quadratic bezier curve var qp1_x = Math.random() * 1024; var qp1_y = Math.random() * 768; DrawQuadBezierCurve(_2dContext,100,400,qp1_x,qp1_y,1000,400,0.001,"rgb(0,255,0)"); //Draw the Cubic bezier curve var cp1_x = Math.random() * 1024; var cp1_y = Math.random() * 768; var cp2_x = Math.random() * 1024; var cp2_y = Math.random() * 768; DrawCubicBezierCurve(_2dContext,100,600,cp1_x,cp1_y,cp2_x,cp2_y,1000,600,0.001,"rgb(0,0,255)"); } </script> <body> <body onLoad="gameInit();"> <canvas id="canvas" width="1024" height="768"> 您的浏览器不支持Canvas特性!!。请使用Chrome,Firefox!! </canvas> </body> </html></span>
插值技术之Bezier插值(1) -- Bezier Curve
标签:
原文地址:http://www.cnblogs.com/bhlsheji/p/5377362.html