核心要点:3D物体碰撞是靠射线检测,射线与碰撞器相撞获取对应的碰撞点信息。

class RayPicking03 {
private ray: Laya.Ray;
private point: Laya.Vector2 = new Laya.Vector2();
private _outHitInfo: Laya.RaycastHit;
private _position: Laya.Vector3;
private _upVector3: Laya.Vector3;
private _vector3: Laya.Vector3;
private _offsetVector3: Laya.Vector3;
private box: Laya.MeshSprite3D;
private _rotateV3:Laya.Vector3;
private camera: Laya.Camera;
private label: Laya.Label;
constructor() {
Laya3D.init(0, 0, true);//初始化3D
Laya.stage.scaleMode = "full";//屏幕缩放方式
Laya.stage.screenMode = "none";
Laya.Stat.show(); var scene: Laya.Scene = Laya.stage.addChild(new Laya.Scene()) as Laya.Scene;//添加场景 this.point = new Laya.Vector2();//位置信息
this.ray = new Laya.Ray(new Laya.Vector3(0, 0, 0), new Laya.Vector3(0, 0, 0));//初始化射线
this._outHitInfo = new Laya.RaycastHit();//初始化射线信息
this._position = new Laya.Vector3(0, 0.25, 0);//位置
this._upVector3 = new Laya.Vector3(0, 1, 0);
this._rotateV3 = new Laya.Vector3(1, 0, 1);
this._vector3 = new Laya.Vector3();
this._offsetVector3 = new Laya.Vector3(0, 0.15, 0) //初始化照相机
this.camera = scene.addChild(new Laya.Camera(0, 0.1, 100)) as Laya.Camera;
this.camera.transform.translate(new Laya.Vector3(0, 2, 5));
this.camera.transform.rotate(new Laya.Vector3(-15, 0, 0), true, false);
this.camera.clearColor = null; //方向光
var directionLight: Laya.DirectionLight = scene.addChild(new Laya.DirectionLight()) as Laya.DirectionLight;
directionLight.color = new Laya.Vector3(0.6, 0.6, 0.6);
directionLight.direction = new Laya.Vector3(1, -1, -1); var plane: Laya.MeshSprite3D = scene.addChild(new Laya.MeshSprite3D(new Laya.PlaneMesh(6, 6, 10, 10))) as Laya.MeshSprite3D;//创建平面物体
var planeMat: Laya.StandardMaterial = new Laya.StandardMaterial();//标准材质
planeMat.diffuseTexture = Laya.Texture2D.load("../bin/res/layabox.png");//添加材质
planeMat.albedo = new Laya.Vector4(0.9, 0.9, 0.9, 1);
plane.meshRender.material = planeMat; var meshCollider = plane.addComponent(Laya.MeshCollider) as Laya.MeshCollider;//网格碰撞器
meshCollider.mesh = plane.meshFilter.sharedMesh;//网格过滤器,获取共享网格 this.box = scene.addChild(new Laya.MeshSprite3D(new Laya.SphereMesh(0.2, 8, 8))) as Laya.MeshSprite3D;//创建立方体
var mat: Laya.StandardMaterial = new Laya.StandardMaterial();
mat.diffuseTexture = Laya.Texture2D.load("../bin/res/layabox.png");
this.box.meshRender.material = mat; Laya.timer.frameLoop(1, this, this.checkHit); this.loadUI();
}
private checkHit(): void {
this.box.transform.position = this._position;
//this.box.transform.rotate(this._rotateV3, true, false)
//从屏幕空间生成射线
this.point.elements[0] = Laya.MouseManager.instance.mouseX;//鼠标X坐标
this.point.elements[1] = Laya.MouseManager.instance.mouseY;//鼠标Y坐标
this.camera.viewportPointToRay(this.point, this.ray);//从摄像机到鼠标点击位置生成射线
Laya.Physics.rayCast(this.ray, this._outHitInfo, 30, 0);//生成射线
} private loadUI(): void { this.label = new Laya.Label();
this.label.text = "点击移动";
this.label.pos(Laya.Browser.clientWidth / 2.5, 100);
this.label.fontSize = 50;
this.label.color = "#40FF40";
Laya.stage.addChild(this.label); //鼠标事件
Laya.stage.on(Laya.Event.MOUSE_UP, this, function (): void {
if (this._outHitInfo.distance !== -1) {
Laya.Vector3.add(this._outHitInfo.position, this._offsetVector3, this._vector3);
Laya.Tween.to(this._position, { x: this._vector3.x, y: this._vector3.y, z: this._vector3.z }, 500/**,Ease.circIn*/);
}
});
}
}

Layabox 3D游戏开发学习笔记---射线检测,鼠标控制物体运动的更多相关文章

  1. Unity 3D游戏开发学习路线(方法篇)

    Unity 3D本来是由德国的一些苹果粉丝开发的一款游戏引擎,一直只能用于Mac平台,所以一直不被业外人士所知晓.但是后来也推出了2.5版,同时发布了PC版本,并将其发布方向拓展到手持移动设备.Uni ...

  2. 【Visual C++】游戏编程学习笔记之八:鼠标输入消息(小demo)

     本系列文章由@二货梦想家张程 所写,转载请注明出处. 作者:ZeeCoder  微博链接:http://weibo.com/zc463717263 我的邮箱:michealfloyd@126.c ...

  3. [游戏开发-学习笔记]菜鸟慢慢飞(四)-Camera

    游戏开发中,主相机应该是最重要的GameObject之一,毕竟游戏呈现给玩家,就是通过它. 相机的使用,在不同的游戏中,有很大的不同.这里总结一下自己学到的一些相关知识. 固定位置-游戏过程中相机的T ...

  4. 【整理】HTML5游戏开发学习笔记(5)- 猜谜游戏

    距上次学习笔记已有一个多月过去了,期间由于新项目赶进度,以致该学习计划给打断,十分惭愧.书本中的第六章的例子相对比较简单.所以很快就完成. 1.预备知识html5中video标签的熟悉 2.实现思路对 ...

  5. 【整理】HTML5游戏开发学习笔记(1)- 骰子游戏

    <HTML5游戏开发>,该书出版于2011年,似乎有些老,可对于我这样没有开发过游戏的人来说,却比较有吸引力,选择自己感兴趣的方向来学习html5,css3,相信会事半功倍.不过值得注意的 ...

  6. cocos2d-x 3.x游戏开发学习笔记(1)--mac下配置cocos2d-x 3.x开发环境

    打开用户文件夹下.bash_profile文件,配置环境 vim ~/.bash_profile //按键i,进行插入编辑(假设输错d进行删除一行) 环境配置过程例如以下: 1.首先配置下androi ...

  7. [Android游戏开发学习笔记]View和SurfaceView

    本文为阅读http://blog.csdn.net/xiaominghimi/article/details/6089594的笔记. 在Android游戏中充当主要角色的,除了控制类就是显示类.而在A ...

  8. Photon + Unity3D 线上游戏开发 学习笔记(一)

    大家好. 我也是学习Photon + unity3D 的新手 有什么说错的地方大家见谅哈. 我的开发环境是 unity3D 4.1.3  ,   Visual Studio 是2010 版本号的  p ...

  9. [游戏开发-学习笔记]菜鸟慢慢飞(九)- NGUI- UIPanel(官方说明翻译)

    我自己笔记是做在OneNote上,直接复制粘贴过来变成图片了,效果好像还可以. 机器翻译,我自己看了一下,改了一部分.

随机推荐

  1. linux查看防火墙状态及开启关闭命令

    存在以下两种方式: 一.service方式 查看防火墙状态: [root@centos6 ~]# service iptables status iptables:未运行防火墙. 开启防火墙: [ro ...

  2. pymysql模块使用

    一.写函数的原因 写这个函数的原因就是为了能够不每次在用Python用数据库的时候还要在写一遍  做个通用函数做保留,也给大家做个小小的分享,函数不是最好的,希望有更好的代码的朋友能提出 互相学习 二 ...

  3. POJ 2304

    #include<iostream>// cheng da cai zi 11. 18 解锁问题 using namespace std; #define f 360 int main() ...

  4. postman创建mocker Server

    为了不影响前端开发的进度,一般后端都是先定数据结构,然后写个假接口让前端调用,这样前端就不必等着后端接口开发完成以后再开始了. 届时,前后端以及UI和测试就可以并行,待双方都把各自的逻辑写好了,便可以 ...

  5. Netty核心概念(8)之Netty线程模型

    1.前言 第7节初步学习了一下Java原本的线程池是如何工作的,以及Future的为什么能够达到其效果,这些知识对于理解本章有很大的帮助,不了解的可以先看上一节. Netty为什么会高效?回答就是良好 ...

  6. 利用System.IO.Compression操作压缩文件

    引用: using System.IO.Compression; using (FileStream zipToOpen = new FileStream(@"D:\json.zip&quo ...

  7. hostonly、桥接和NAT的联网方式

    不多说,直接上干货! 通信设置: 1)  hostonly,换句话就是,Windows和Linux在不插网线情况下,也是可以进行通信. 这样设置的好处,有时候,万一比如在农村,没网络.那么,这是最佳方 ...

  8. 18个HTML5和JavaScript游戏引擎库

    1) Best HTML5 and javascript game engine Library-  Impactjs     2) Best HTML5 and javascript game en ...

  9. android学习-仿Wifi模块实现

    最近研究android内核-系统关键服务的启动解析,然而我也不知道研究wifi的作用,就当兴趣去做吧(其实是作业-_-) 系统原生WiFI功能大概有:启动WiFI服务,扫描WiFi信息(这个好像已经被 ...

  10. 10-hdfs-hdfs搭建

    hdfs的优缺点比较: 架构图解分析: nameNode的主要任务: SNameNode的功能: (不是NN的备份, 主要用来合并fsimage) 合并流程: dataNode的主要功能: HDFS上 ...