- 浏览: 1060543 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (695)
- 心情日记 (14)
- AS开发工具 (12)
- 文章转载 (99)
- AIR (5)
- 问题总结 (46)
- SWF格式 (7)
- 测试总结 (10)
- 外文资料 (9)
- 算法技术 (33)
- AS3常用开源库 (43)
- 源码范例 (102)
- FLEX (72)
- FLASH 优化 (33)
- 游戏开发 (49)
- 开发技术 (11)
- 工作应用 (34)
- AS3收集 (140)
- WebBase (0)
- 开发构想 (4)
- 设计模式 (2)
- 框架和框架范例 (19)
- RED5 (3)
- java开发 (3)
- JAVA (1)
- FLASH-3D (23)
- 3D (6)
- 书籍 (10)
- 业界信息资料 (3)
- C# (1)
- JavaScript (12)
- HTML5 (6)
- Flixel (1)
- D5Power RPG网页游戏引擎 (0)
- ColorMatrixFilter - 获得相应颜色的色调 函数 (0)
- Starling (0)
最新评论
-
老顽童203:
字体
水果忍者鼠标跟随特效制作[转载] -
hairball00:
[转] 放出超多的Flash组件源代码 -
he74552775:
flash AS3 RegExp简单功能用法(转) -
hanshuai1232000:
第四点,有利也有弊,等你做了大型的aprg,你就知道了
[转]位图数据内存优化 -
yangfantao:
太感谢
[转] 放出超多的Flash组件源代码
第二个是书中的源代码给出了一个Collision类,其中的block方法可以实现两个物体之间的碰撞检测。
有趣的是,Collsion.block方法有两个参数,第一个是被阻碍物,第二个是障碍物,在这个演示中,山上有两块石板。
对于左边的石板,石板是被阻碍物,小猪是障碍物,产生的效果就是小猪可以推着石板走。
对于右边的石板,小猪是被阻碍物,石板是障碍物,产生的效果就是小猪撞到墙上就不能动了,很有意思。
http://bbs.9ria.com/viewthread.php?tid=88769&extra=page%3D1%26amp%3Borderby%3Ddateline%26amp%3Bfilter%3D2592000
package |
{ |
import flash.display.MovieClip; |
import flash.geom.Point; |
|
public class Collision |
//0.2 (March 20, 2009) |
{ |
public function Collision() |
{ |
} |
|
//Block objects |
static public function block(objectA:MovieClip, objectB:MovieClip):void |
{ |
var objectA_Halfwidth:Number = objectA.width / 2; |
var objectA_Halfheight:Number = objectA.height / 2; |
var objectB_Halfwidth:Number = objectB.width / 2; |
var objectB_Halfheight:Number = objectB.height / 2; |
var dx:Number = objectB.x - objectA.x; |
var ox:Number = objectB_Halfwidth
+ objectA_Halfwidth - Math.abs(dx); |
if (ox > 0) |
{ |
var dy:Number = objectA.y - objectB.y; |
var oy:Number = objectB_Halfheight
+ objectA_Halfheight - Math.abs(dy); |
if (oy > 0) |
{ |
if (ox < oy) |
{ |
if (dx < 0) |
{ |
//Collision on right |
oy = 0; |
} |
else |
{ |
//Collision on left |
oy = 0; |
ox *= -1; |
} |
} |
else |
{ |
if (dy < 0) |
{ |
//Collision on Top |
ox = 0; |
oy *= -1; |
} |
else |
{ |
//Collision on Bottom |
ox = 0; |
} |
} |
|
//Use the
calculated x and y overlaps to |
//Move objectA
out of the collision |
|
objectA.x += ox; |
objectA.y += oy; |
} |
} |
} |
|
//General purpose method for
testing Axis-based collisions. Returns true or False |
static public function test(objectA:Object,objectB:Object):Boolean |
{ |
var objectA_Halfwidth=objectA.width/2; |
var objectA_Halfheight=objectA.height/2; |
var objectB_Halfwidth=objectB.width/2; |
var objectB_Halfheight=objectB.height/2; |
var dx=objectB.x-objectA.x; |
var ox=objectB_Halfwidth+objectA_Halfwidth-Math.abs(dx); |
if (0<ox) |
{ |
var dy=objectA.y-objectB.y; |
var oy=objectB_Halfheight+objectA_Halfheight-Math.abs(dy); |
if (0<oy) |
{ |
return true; |
} |
} |
else |
{ |
return false; |
} |
return false; |
} |
|
//Collisions between the
player and platform |
static public function playerAndPlatform(player:MovieClip,
platform:MovieClip, bounce:Number,
friction:Number):void |
{ |
//This method requires the
following getter and |
//setter properties in the
player object: |
//objectIsOnGround:Boolean,
vx:Number, vy:Number, |
//bounceX:Number,
bounceY:Number |
|
//Decalre variables needed
for the player's |
//position and
dimensions |
var player_Halfwidth:Number; |
var player_Halfheight:Number; |
var player_X:Number; |
var player_Y:Number |
|
//Decalre variables needed
for the physics calculations |
var bounceX:Number; |
var bounceY:Number; |
var frictionX:Number; |
var frictionY:Number; |
|
//Find out whether the player
object has a collisionArea |
//subobject
defined |
if(player.collisionArea
!= null) |
{ |
//If it does, find out
its width and height |
player_Halfwidth = player.collisionArea.width / 2; |
player_Halfheight = player.collisionArea.height / 2; |
|
//Convert the
collisionArea's local x,y coordinates to global coordinates |
var player_Position:Point = new Point(player.collisionArea.x, player.collisionArea.y); |
player_X
= player.localToGlobal(player_Position).x; |
player_Y = player.localToGlobal(player_Position).y; |
} |
else |
{ |
//If there's no
collisionArea subobject |
//Use the player's main
height, width, x and y |
player_Halfwidth = player.width / 2; |
player_Halfheight = player.height / 2; |
player_X = player.x; |
player_Y = player.y; |
} |
//Find the platform's
dimensions |
var platform_Halfwidth:Number =
platform.width / 2; |
var platform_Halfheight:Number =
platform.height / 2; |
|
//Find the distance between
the player and platfrom on the x axis |
var dx:Number =
platform.x -
player_X; |
|
//Find the amount of overlap
on the x axis |
var ox:Number = platform_Halfwidth
+ player_Halfwidth - Math.abs(dx); |
|
//Check for a collision on
the x axis |
if (ox > 0) |
{ |
//If the objects
overlap on the x axis, a collision might be occuring |
//Define the variables
you need to check for a collision on the y axis |
var dy:Number = player.y - platform.y; |
var oy:Number = platform_Halfheight
+ player_Halfheight - Math.abs(dy); |
|
//Check for a y axis
collision. We know a collision must be |
//occuring if there's a
collision on both the x and y axis |
if (oy > 0) |
{ |
//Yes, a
collision is occuring! |
//Now you need
to find out on which side |
//of the
platform it's occuring on. |
if (ox < oy) |
{ |
if (dx < 0) |
{ |
//Collision on right |
oy = 0; |
dx = 1; |
dy = 0 |
} |
else |
{ |
//Collision on left |
oy = 0; |
ox *= -1; |
dx = -1; |
dy = 0 |
} |
} |
else |
{ |
if (dy < 0) |
{ |
//Collision on Top |
ox = 0; |
oy *= -1; |
dx = 0; |
dy = -1; |
//set the player's isOnGround property to |
//true to enable jumping |
player.isOnGround
= true; |
} |
else |
{ |
//Collision on Bottom |
ox = 0; |
dx = 0; |
dy = 1; |
} |
} |
|
//Find the
direction of the collision ("dot product") |
var directionOfCollision:Number = player.vx * dx + player.vy * dy; |
|
//Calculate the
new direction for the bounce ("projection") |
var newDirection_X:Number =
directionOfCollision * dx; |
var newDirection_Y:Number =
directionOfCollision * dy; |
|
//Find the
"tangent velocity": |
//the speed in
the direction that the object is moving. |
//It's used for
calculating additional platform friction. |
var tangent_Vx:Number = player.vx - newDirection_X; |
var tangent_Vy:Number = player.vy - newDirection_Y; |
|
//Apply
collision forces if the object is moving into a collision |
if (directionOfCollision
< 0) |
{ |
//Calculate the friction |
frictionX = tangent_Vx
* friction; |
frictionY = tangent_Vy
* friction; |
|
//Calculate the amount of bounce |
bounceX = newDirection_X
* bounce; |
bounceY = newDirection_Y
* bounce; |
} |
else |
{ |
//Prevent forces from being applied if the object is |
//moving out of a collision |
bounceX = 0; |
bounceY = 0; |
frictionX = 0; |
frictionY = 0; |
} |
//Apply
platform friction |
player.vx += ox - frictionX; |
player.vy += oy - frictionY; |
|
//Move the
player out of the collision |
player.x += ox; |
player.y += oy; |
|
//Bounce the
player off the platform |
player.bounceX = bounceX; |
player.bounceY = bounceY; |
} |
} |
} |
} |
} |
发表评论
-
greenSock新老库
2012-08-07 13:25 0老库-做项目的时候比新库完全不抖啊 -
Starling开发的游戏源码
2012-07-26 17:13 0Starling开发的游戏源码 -
一个基于STARLING的TOWER塔防游戏
2012-07-24 15:27 0一个基于STARLING的TOWER塔防游戏 -
HttpStatusConfig --一个HTTP 协议返回的 解析说明类
2012-04-18 16:40 0package guwanyuan.qicool.game ... -
[转] [Flash/Flex] 加载SWF性能VS影片剪辑性能
2012-03-15 22:29 0http://bbs.9ria.com/viewthread. ... -
水果忍者鼠标跟随特效制作[转载]
2012-03-01 16:06 2393实现这效果其实比较简单,主要是思路~! package ... -
greensock-as3
2012-02-23 21:26 0greensock-as3 -
禁止输入文本可以粘贴
2012-02-10 13:15 2254//禁止输入文本粘贴动作 private static ... -
江湖情缘游戏里的跳的类
2012-02-08 23:55 0package com.app.role.montions ... -
ColorMatrixFilter - 获得相应颜色的色调 函数
2012-02-08 23:03 0/**获得相应颜色的色调的滤镜**/ public sta ... -
[教程] 路点导航(Waypoint Navigation)
2011-12-31 00:51 0前言:这个不难,所以知道的童鞋不要喷,不知道的童鞋也不要怕,要 ... -
ARPG游戏引擎设计思路
2011-12-31 00:48 00.整体结构 下载 (32.92 KB ... -
Embed绑定XML与txt文本文件
2011-12-28 15:54 4599使用Embed标签可以将图片绑定到swf中并显示,那么我 ... -
弹弹堂测试
2011-12-08 04:21 0弹弹堂测试弹弹堂测试 -
一些DEMO
2011-11-28 17:42 0一些DEMO一些DEMO -
[转]三次贝尔曲线
2011-11-10 01:09 1869http://bbs.9ria.com/viewt ... -
[心得] 完美解决as3在ie中初始化时stageWidth和stageHeight为0的问题
2011-11-03 00:46 2882先看下面的一段脚本,这是比较经典的初始化脚本: pa ... -
五子棋算法详解
2011-10-31 04:14 0五子棋算法详解五子棋算法详解五子棋算法详解 -
[转]服务器端ActionScript语言参考
2011-10-31 00:29 0服务器端ActionScript语言参考 -
[转]FLASH与JS序列简单应用
2011-10-28 01:03 2024FLASH与JS序列简单应用 (一) 用swfob ...
相关推荐
fcl flexible collision library 碰撞检测库源码
实时碰撞检测算法技术 real time collision detection
This C library handles 2-dimensional bounding box collision detection. It is optimized to scale well from a few objects to many thousands of objects.
本程序主要介绍碰撞检测编程的具体方法,通过计算空间点与平面的距离
Efficient Collision Detection Using Bounding Volume Hierarchies of k-DOPs 碰撞检测,很好的文档,对新手是很好的帮助。
本资源用于碰撞检测: ...2.Real-Time Collision Detection,一个包含实时碰撞检测算法原理+代码的文档 3.CVPR2022:将bullet用于深度学习中的数据生成上,点云模型处理 适用于想要深入研究碰撞检测的研究员和老师
碰撞检测的源代码,是游戏引擎必备的功能。
利用VC++实现碰撞检测的算法。其中假设粒子都是椭圆的。
这个例子描述了MD4碰撞,很有研究价值,大家可以仔细研究下...这个例子描述了MD4碰撞,很有研究价值,大家可以仔细研究下
实时碰撞检测算法技术 Real-Time Collision Detection
FANUC机器人SRVO-050碰撞检测报警原因分析及处理对策
2D Polygon Collision Detection, 多边形碰撞检测,两个多边形是否可以靠近,
GJK (Gilbert-Johnson-Keerthi) 碰撞检测算法在 MATLAB 中的实现。 GJK.m 函数获取形状顶点数据并返回两个形状是否穿透。 仅适用于凸形物体! MAIN_example.m 对两个多面体进行动画处理,并在两者相互碰撞时停止。
Fast Collision Detection for Interactive Games 互动游戏的快速碰撞检测.doc
使用八叉树实现的三维空间碰撞检测 collision detection
该程序模拟了N个粒子和矩形边界之间的碰撞。 基本模拟器分为4部分: 通过UNIFORM GRID算法的碰撞检测功能确定碰撞时间的功能时间步长和解决冲突的功能将模拟渲染为电影文件奖励功能为了获得更多荣誉,已实现以下...
碰撞检测的文章 写的不错。
我只传了个 collision.hpp,和collision.cpp 但是里边有分离轴理论,进行长方体碰撞检测源码 经检测,非常精准.希望各位喜欢
第一人称视角,可以跳跃行走碰撞检测,模拟了白天黑夜等等!(Visual C++ with OpenGL to do a 3D scene. Which contains the terrain generation, scene roaming, collision detection, simulation of the river ...