核心要点: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. CentOS7下 Python2.7.5升级为Python2.7.13

    参考:https://www.jianshu.com/p/fad3942fc0ed 第一步:查看Centos版本及Python版本 • CentOS版本 [root@ tools_package]# ...

  2. 重新设置Linux文件共享密码..

    今天同事把我主机踹倒了,鼠键都没反应,于是我在Linux运行的情况下强制重启了下电脑. 启动完了VMware后Linux重启,正常使用.后来我想起来有快照功能 之前也没用到过,于是就点了一下刚开始安装 ...

  3. 使用.net core读取Json文件配置

    1.使用vs2017创建一个应用台程序 2.使用程序包管理器控制台执行命令 Install-Package Microsoft.AspNetCore -Version 2.0.1 3.创建一个json ...

  4. python3异常处理 try

    一. 简介 在编程过程中为了增加友好性,在程序出现Bug时一般不会直接将错误信息展示给用户,而是提供一个友好的输出提示. 二. 使用 1.异常基础 常用结构: try: pass except Exc ...

  5. POJ 2301

    #include<iostream> #include<stdio.h> using namespace std; int main() { //freopen("a ...

  6. document文档对象

    document 文挡对象 - JavaScript脚本语言描述———————————————————————注:页面上元素name属性和JavaScript引用的名称必须一致包括大小写否则会提示你一 ...

  7. 获取设备信息——获取客户端ip地址和mac地址

    1.获取本地IP(有可能是 内网IP,192.168.xxx.xxx) /** * 获取本地IP * * @return */ public static String getLocalIpAddre ...

  8. 【转】Entity framework 6 编写的通用数据类

    原文:http://blog.csdn.net/laokaizzz/article/details/25730813 准备参考

  9. BugFree3.0.4Linux环境安装指南

    bugfree安装的前提是配置LAMP(apache+mysql+php),我安装的linux系统是centos6.0 一.安装Apache服务器 1.安装apache yum install htt ...

  10. sql 函数字符串处理

    --Description: 字符处理 --使用: 放到查询分析器里执行就可以了 --示例: select * from dbo.splitstr('12 44 45 50 56 87',' ') o ...