添加物理瞄准线

This commit is contained in:
leo
2019-12-25 10:35:32 +08:00
parent 0fafa426cc
commit a1b45d5c33
35 changed files with 22986 additions and 0 deletions

67
RayCastDemo/.gitignore vendored Normal file
View File

@@ -0,0 +1,67 @@
#/////////////////////////////////////////////////////////////////////////////
# Fireball Projects
#/////////////////////////////////////////////////////////////////////////////
library/
temp/
local/
build/
#/////////////////////////////////////////////////////////////////////////////
# Logs and databases
#/////////////////////////////////////////////////////////////////////////////
*.log
*.sql
*.sqlite
#/////////////////////////////////////////////////////////////////////////////
# files for debugger
#/////////////////////////////////////////////////////////////////////////////
*.sln
*.csproj
*.pidb
*.unityproj
*.suo
#/////////////////////////////////////////////////////////////////////////////
# OS generated files
#/////////////////////////////////////////////////////////////////////////////
.DS_Store
ehthumbs.db
Thumbs.db
#/////////////////////////////////////////////////////////////////////////////
# exvim files
#/////////////////////////////////////////////////////////////////////////////
*UnityVS.meta
*.err
*.err.meta
*.exvim
*.exvim.meta
*.vimentry
*.vimentry.meta
*.vimproject
*.vimproject.meta
.vimfiles.*/
.exvim.*/
quick_gen_project_*_autogen.bat
quick_gen_project_*_autogen.bat.meta
quick_gen_project_*_autogen.sh
quick_gen_project_*_autogen.sh.meta
.exvim.app
#/////////////////////////////////////////////////////////////////////////////
# webstorm files
#/////////////////////////////////////////////////////////////////////////////
.idea/
#//////////////////////////
# VS Code
#//////////////////////////
.vscode/

2
RayCastDemo/README.md Normal file
View File

@@ -0,0 +1,2 @@
# 物理系统下瞄准线
环境Creator v2.0.0

View File

@@ -0,0 +1,6 @@
{
"ver": "1.0.1",
"uuid": "29f52784-2fca-467b-92e7-8fd9ef8c57b7",
"isGroup": false,
"subMetas": {}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,7 @@
{
"ver": "0.9.0",
"uuid": "2d2f792f-a40c-49bb-a189-ed176a246e49",
"asyncLoadAssets": false,
"autoReleaseAssets": false,
"subMetas": {}
}

View File

@@ -0,0 +1,6 @@
{
"ver": "1.0.1",
"uuid": "4734c20c-0db8-4eb2-92ea-e692f4d70934",
"isGroup": false,
"subMetas": {}
}

View File

@@ -0,0 +1,16 @@
const {ccclass, property} = cc._decorator;
@ccclass
export default class Helloworld extends cc.Component {
@property(cc.Label)
label: cc.Label = null;
@property
text: string = 'hello';
start () {
// init logic
this.label.string = this.text;
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "e1b90feb-a217-4493-849d-9a611900d683",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@@ -0,0 +1,21 @@
const { ccclass, property } = cc._decorator;
@ccclass
export default class PhysicsMgr extends cc.Component {
@property
enabeld = true;
// LIFE-CYCLE CALLBACKS:
onLoad() {
cc.director.getPhysicsManager().enabled = this.enabeld;
}
start() {
}
// update (dt) {}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "53124fd2-8450-483a-a843-e9be55bdbd02",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@@ -0,0 +1,101 @@
const AIM_LINE_MAX_LENGTH = 1440;
const { ccclass, property } = cc._decorator;
@ccclass
export default class RayCastMgr extends cc.Component {
@property(cc.Node)
startNode: cc.Node = null;
@property(cc.Graphics)
graphic_line: cc.Graphics = null;
startLocation: cc.Vec2 = null;
curLength = 0;
onLoad() {
this.startLocation = this.startNode.convertToWorldSpaceAR(cc.v2(0, 0));
this.graphic_line.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
this.graphic_line.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
this.graphic_line.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
}
start() {
}
onTouchStart(touch: cc.Event.EventTouch) {
const location = touch.getLocation();
this.draw(cc.v2(location.x, location.y));
}
onTouchMove(touch: cc.Event.EventTouch) {
const location = touch.getLocation();
this.draw(cc.v2(location.x, location.y));
}
onTouchEnd(touch: cc.Event.EventTouch) {
}
draw(end) {
this.graphic_line.clear();
this.curLength = 0;
// 计算射线
this.drawRayCast(this.startLocation, end.subSelf(this.startLocation).normalizeSelf());
this.graphic_line.stroke();
}
drawRayCast(start: cc.Vec2, dirUnit: cc.Vec2) {
let leftLength = AIM_LINE_MAX_LENGTH - this.curLength;
if (leftLength <= 0) return;
// 计算线的终点位置
let end = start.add(dirUnit.mul(leftLength));
// 检测给定的线段穿过哪些碰撞体,可以获取到碰撞体在线段穿过碰撞体的那个点的法线向量和其他一些有用的信息。
let resultList = cc.director.getPhysicsManager().rayCast(start, end, cc.RayCastType.Closest);
if (resultList.length < 1) {
// 画剩余线段
this.drawAimLine(start, end);
} else {
const result = resultList[0];
// 指定射线与穿过的碰撞体在哪一点相交。
const point = result.point;
// 画入射线段
this.drawAimLine(start, point);
const lineLength = point.sub(start).mag();
this.curLength += lineLength;
// 指定碰撞体在相交点的表面的法线单位向量。
const vector_n = result.normal;
// 入射单位向量
const vector_i = dirUnit;
const vector_r = vector_i.sub(vector_n.mul(2 * vector_i.dot(vector_n)));
// 接着计算下一段
this.drawRayCast(point, vector_r);
}
}
/**
*
* @param start 世界坐标系
* @param end 世界坐标系
*/
drawAimLine(start: cc.Vec2, end: cc.Vec2) {
// 转换坐标
let startPoint = this.graphic_line.node.convertToNodeSpaceAR(start);
this.graphic_line.moveTo(startPoint.x, startPoint.y);
// 画小圆圆
const delta = 20, dirVector = end.sub(start);
let total = Math.round(dirVector.mag() / delta);
dirVector.normalizeSelf().mulSelf(delta);
for (let i = 0; i < total; i++) {
startPoint.addSelf(dirVector);
this.graphic_line.circle(startPoint.x, startPoint.y, 2);
}
}
// update (dt) {}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "75cd2a7e-f307-4648-96b7-ba9888c22f69",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@@ -0,0 +1,6 @@
{
"ver": "1.0.1",
"uuid": "7b81d4e8-ec84-4716-968d-500ac1d78a54",
"isGroup": false,
"subMetas": {}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

View File

@@ -0,0 +1,31 @@
{
"ver": "2.2.0",
"uuid": "6aa0aa6a-ebee-4155-a088-a687a6aadec4",
"type": "sprite",
"wrapMode": "clamp",
"filterMode": "bilinear",
"premultiplyAlpha": false,
"subMetas": {
"HelloWorld": {
"ver": "1.0.3",
"uuid": "31bc895a-c003-4566-a9f3-2e54ae1c17dc",
"rawTextureUuid": "6aa0aa6a-ebee-4155-a088-a687a6aadec4",
"trimType": "auto",
"trimThreshold": 1,
"rotated": false,
"offsetX": 0,
"offsetY": 0,
"trimX": 0,
"trimY": 0,
"width": 195,
"height": 270,
"rawWidth": 195,
"rawHeight": 270,
"borderTop": 0,
"borderBottom": 0,
"borderLeft": 0,
"borderRight": 0,
"subMetas": {}
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 548 B

View File

@@ -0,0 +1,31 @@
{
"ver": "2.2.0",
"uuid": "1072a1c2-537e-427a-9a7a-febfb0cc2792",
"type": "sprite",
"wrapMode": "clamp",
"filterMode": "bilinear",
"premultiplyAlpha": false,
"subMetas": {
"item_10": {
"ver": "1.0.3",
"uuid": "7076766c-671d-4fad-80d8-ab5c822e3609",
"rawTextureUuid": "1072a1c2-537e-427a-9a7a-febfb0cc2792",
"trimType": "auto",
"trimThreshold": 1,
"rotated": false,
"offsetX": 0,
"offsetY": 0,
"trimX": 0,
"trimY": 0,
"width": 134,
"height": 139,
"rawWidth": 134,
"rawHeight": 139,
"borderTop": 0,
"borderBottom": 0,
"borderLeft": 0,
"borderRight": 0,
"subMetas": {}
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

View File

@@ -0,0 +1,31 @@
{
"ver": "2.2.0",
"uuid": "b18e13db-6bd6-48ae-910e-c7ab8984ae65",
"type": "sprite",
"wrapMode": "clamp",
"filterMode": "bilinear",
"premultiplyAlpha": false,
"subMetas": {
"item_11": {
"ver": "1.0.3",
"uuid": "c6116e42-3eaa-44a6-9e91-f20c7eeb3aa2",
"rawTextureUuid": "b18e13db-6bd6-48ae-910e-c7ab8984ae65",
"trimType": "auto",
"trimThreshold": 1,
"rotated": false,
"offsetX": 0,
"offsetY": 0,
"trimX": 0,
"trimY": 0,
"width": 135,
"height": 158,
"rawWidth": 135,
"rawHeight": 158,
"borderTop": 0,
"borderBottom": 0,
"borderLeft": 0,
"borderRight": 0,
"subMetas": {}
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

View File

@@ -0,0 +1,31 @@
{
"ver": "2.2.0",
"uuid": "130f22b5-4f11-414d-ba08-99885835c0a7",
"type": "sprite",
"wrapMode": "clamp",
"filterMode": "bilinear",
"premultiplyAlpha": false,
"subMetas": {
"item_8": {
"ver": "1.0.3",
"uuid": "34661e88-7f90-42d8-8a7d-70727ed04a06",
"rawTextureUuid": "130f22b5-4f11-414d-ba08-99885835c0a7",
"trimType": "auto",
"trimThreshold": 1,
"rotated": false,
"offsetX": 0,
"offsetY": 0,
"trimX": 0,
"trimY": 0,
"width": 135,
"height": 140,
"rawWidth": 135,
"rawHeight": 140,
"borderTop": 0,
"borderBottom": 0,
"borderLeft": 0,
"borderRight": 0,
"subMetas": {}
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

View File

@@ -0,0 +1,31 @@
{
"ver": "2.2.0",
"uuid": "5591a452-f4cf-449d-ace2-fa200df80559",
"type": "sprite",
"wrapMode": "clamp",
"filterMode": "bilinear",
"premultiplyAlpha": false,
"subMetas": {
"item_9": {
"ver": "1.0.3",
"uuid": "71932bfb-3278-4b00-9b82-afe6f927723f",
"rawTextureUuid": "5591a452-f4cf-449d-ace2-fa200df80559",
"trimType": "auto",
"trimThreshold": 1,
"rotated": false,
"offsetX": 0,
"offsetY": 0,
"trimX": 0,
"trimY": 0,
"width": 72,
"height": 81,
"rawWidth": 72,
"rawHeight": 81,
"borderTop": 0,
"borderBottom": 0,
"borderLeft": 0,
"borderRight": 0,
"subMetas": {}
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View File

@@ -0,0 +1,31 @@
{
"ver": "2.2.0",
"uuid": "a8027877-d8d6-4645-97a0-52d4a0123dba",
"type": "sprite",
"wrapMode": "clamp",
"filterMode": "bilinear",
"premultiplyAlpha": false,
"subMetas": {
"singleColor": {
"ver": "1.0.3",
"uuid": "410fb916-8721-4663-bab8-34397391ace7",
"rawTextureUuid": "a8027877-d8d6-4645-97a0-52d4a0123dba",
"trimType": "auto",
"trimThreshold": 1,
"rotated": false,
"offsetX": 0,
"offsetY": 0,
"trimX": 0,
"trimY": 0,
"width": 2,
"height": 2,
"rawWidth": 2,
"rawHeight": 2,
"borderTop": 0,
"borderBottom": 0,
"borderLeft": 0,
"borderRight": 0,
"subMetas": {}
}
}
}

20635
RayCastDemo/creator.d.ts vendored Normal file

File diff suppressed because it is too large Load Diff

15
RayCastDemo/jsconfig.json Normal file
View File

@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"experimentalDecorators": true
},
"exclude": [
"node_modules",
".vscode",
"library",
"local",
"settings",
"temp"
]
}

6
RayCastDemo/project.json Normal file
View File

@@ -0,0 +1,6 @@
{
"engine": "cocos2d-html5",
"packages": "packages",
"version": "2.2.0",
"id": "b9249570-68a9-49c5-8195-108f1ae0b31f"
}

View File

@@ -0,0 +1,13 @@
{
"excludeScenes": [],
"orientation": {
"landscapeLeft": true,
"landscapeRight": true,
"portrait": false,
"upsideDown": false
},
"packageName": "org.cocos2d.helloworld",
"startScene": "2d2f792f-a40c-49bb-a189-ed176a246e49",
"title": "hello_world",
"webOrientation": "auto"
}

View File

@@ -0,0 +1,7 @@
{
"excludeScenes": [],
"packageName": "org.cocos2d.helloworld",
"platform": "web-mobile",
"startScene": "2d2f792f-a40c-49bb-a189-ed176a246e49",
"title": "HelloWorld"
}

View File

@@ -0,0 +1,38 @@
{
"collision-matrix": [
[
true
]
],
"excluded-modules": [
"Geom Utils",
"3D",
"3D Primitive"
],
"group-list": [
"default"
],
"start-scene": "current",
"design-resolution-width": 960,
"design-resolution-height": 640,
"fit-width": false,
"fit-height": true,
"use-project-simulator-setting": false,
"simulator-orientation": false,
"use-customize-simulator": false,
"simulator-resolution": {
"width": 960,
"height": 640
},
"last-module-event-record-time": 0,
"facebook": {
"enable": false,
"appID": "",
"live": {
"enable": false
},
"audience": {
"enable": false
}
}
}

View File

@@ -0,0 +1,102 @@
{
"services": [
{
"service_id": "235",
"service_name": "Cocos Analytics",
"service_icon": "https://account.cocos.com/client/3f8f31ccf66995e183044f167c092395.png",
"service_desc": "提供最核心最基本的数据、标准化界面功能简洁易用、数据准确性最好",
"service_title": "精准了解游戏的新增、活跃、留存、付费等数据",
"service_guide_url": "https://n-analytics.cocos.com/docs/",
"service_sample_url": "https://github.com/cocos-creator/tutorial-dark-slash/tree/analytics",
"service_dev_url": "http://analytics.cocos.com/realtime/jump_to/<app_id>",
"service_type": "3",
"service_type_zh": "公司和个人游戏",
"support_platform": [
"Android",
"iOS",
"HTML5"
],
"package_download_url": "https://download.cocos.com/CocosServices/plugins/service-analytics/1.2.3_2.1.1.zip",
"package_version_desc": "<p><strong>更新日期:</strong> 2019/11/18<br />\n<strong>更新说明:</strong><br />\n1、更新统计SDK<br />\n2、修改微信小游戏和百度小游戏集成失败的问题<br />\n",
"service_component_name": "service-analytics",
"package_versions": [
"1.2.3_2.1.1",
"1.2.0_2.1.0",
"1.1.7_2.0.3",
"1.1.6_2.0.1_2.0.2",
"1.1.5_2.0.1",
"1.1.4_2.0.1",
"1.1.3_2.0.1",
"1.1.2_2.0.0",
"1.0.0_1.0.5"
],
"build_platform": [
],
"require_verify": 0,
"service_price": "",
"service_protocol": "游戏首次开启该服务时Cocos会后台通知服务方为游戏开通服务并初始化参数服务方根据需要可能会获取您的Cocos账户信息包括账户基本资料、游戏基本资料、账户余额等点击确认开通按钮即视为您同意该服务访问您的账户信息详见<a href='http://auth.cocos.com/CocosServiceAgreement.html'>《Cocos用户服务协议》</a>和<a href='http://auth.cocos.com/PrivacyPolicy.html'>《Cocos隐私政策》</a>"
},
{
"service_id": "241",
"service_name": "Matchvs",
"service_icon": "https://account.cocos.com/client/14406719a07eb3d714d36e5edc6e06fa.png",
"service_desc": "通过SDK接入快速实现联网功能、帧同步、国内外多节点、服务器独立部署、gameServer自定义游戏服务端逻辑。",
"service_title": "专业成熟的移动游戏联网与服务端解决方案",
"service_guide_url": "http://doc.matchvs.com/QuickStart/QuickStart-CocosCreator",
"service_sample_url": "http://www.matchvs.com/serviceCourse",
"service_dev_url": "http://www.matchvs.com/cocosLogin",
"service_type": "3",
"service_type_zh": "公司和个人游戏",
"support_platform": [
"Android",
"iOS",
"HTML5"
],
"package_download_url": "https://download.cocos.com/CocosServices/plugins/service-matchvs/1.0.10_3.7.9.10.zip",
"package_version_desc": "<p><strong>更新日期:</strong> 2019/9/12\n<strong>更新内容:</strong>\n1.多语言支持\n2.SDK日常更新</p>",
"service_component_name": "service-matchvs",
"package_versions": [
"1.0.9_3.7.9.9",
"1.0.7_3.7.9.6",
"1.0.6_3.7.9.2",
"1.0.5_3.7.7.3",
"1.0.3_3.7.6.4",
"1.0.10_3.7.9.10"
],
"build_platform": [
],
"require_verify": 0,
"service_price": "该服务按使用量计费,<a href='https://www.matchvs.com/price'><font color='#dddddd'>计费规则</font></a>,所产生的费用将由第三方从您的 <a href='https://account.cocos.com/#/finance/finance_list'><font color='#dddddd'>Cocos 账户余额</font></a> 中扣除。",
"service_protocol": "游戏首次开启该服务时Cocos会后台通知服务方为游戏开通服务并初始化参数服务方根据需要可能会获取您的Cocos账户信息包括账户基本资料、游戏基本资料、账户余额等点击确认开通按钮即视为您同意该服务访问您的账户信息详见<a href='http://auth.cocos.com/CocosServiceAgreement.html'>《Cocos用户服务协议》</a>和<a href='http://auth.cocos.com/PrivacyPolicy.html'>《Cocos隐私政策》</a>"
},
{
"service_id": "242",
"service_name": "Agora Voice",
"service_icon": "https://account.cocos.com/uploads/client_icon/2019-07-16/273952d155b4cdb72d2b1bc61de91ade.png",
"service_desc": "稳定、低耗、76ms超低延时、全球200+数据中心覆盖变声器、超高音质、听声辩位等丰富玩法极速接入全平台支持Android、iOS、Web。",
"service_title": "游戏内置实时语音",
"service_guide_url": "https://docs.agora.io/cn/Interactive Gaming/game_c?platform=Cocos Creator",
"service_sample_url": "https://github.com/AgoraIO/Voice-Call-for-Mobile-Gaming/tree/master/Basic-Voice-Call-for-Gaming/Hello-CocosCreator-Voice-Agora",
"service_dev_url": "https://sso.agora.io/api/oauth/cocos/login",
"service_type": "3",
"service_type_zh": "公司和个人游戏",
"support_platform": [
"Android",
"iOS",
"HTML5"
],
"package_download_url": "https://download.cocos.com/CocosServices/plugins/service-agora/1.0.2_2.2.3.20_2.5.2.zip",
"package_version_desc": "<b>更新日期:<b>2019/06/27<br>\n<br><b>更新内容:</b><br>\n1、修复部分BUG<br>\n2、代码优化",
"service_component_name": "service-agora",
"package_versions": [
"1.0.2_2.2.3.20_2.5.2",
"1.0.1_2.2.3.20_2.5.2"
],
"build_platform": [
],
"require_verify": 1,
"service_price": "该服务按使用量计费,<a href='https://www.agora.io/cn/price/'><font color='#dddddd'>计费规则</font></a>,所产生的费用将由第三方从您的 <a href='https://account.cocos.com/#/finance/finance_list'><font color='#dddddd'>Cocos 账户余额</font></a> 中扣除。",
"service_protocol": "游戏首次开启该服务时Cocos会后台通知服务方为游戏开通服务并初始化参数服务方根据需要可能会获取您的Cocos账户信息包括账户基本资料、游戏基本资料、账户余额等点击确认开通按钮即视为您同意该服务访问您的账户信息详见<a href='http://auth.cocos.com/CocosServiceAgreement.html'>《Cocos用户服务协议》</a>和<a href='http://auth.cocos.com/PrivacyPolicy.html'>《Cocos隐私政策》</a>"
}
]
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

View File

@@ -0,0 +1,5 @@
{
"name": "TEMPLATES.helloworld-ts.name",
"desc": "TEMPLATES.helloworld-ts.desc",
"banner": "template-banner.png"
}

18
RayCastDemo/tsconfig.json Normal file
View File

@@ -0,0 +1,18 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [ "dom", "es5", "es2015.promise" ],
"target": "es5",
"allowJs": true,
"experimentalDecorators": true,
"skipLibCheck": true
},
"exclude": [
"node_modules",
"library",
"local",
"temp",
"build",
"settings"
]
}