玩法 / PhysicsService
PhysicsService Class
碰撞组
Table of contents
Methods
addCollisionGroup(name : string ): void |
---|
添加新碰撞组 |
deleteCollisionGroup(name : string ): void |
删除已有碰撞组 |
getAvailableCollisionGroupsCount(): number |
获取当前可剩余使用碰撞组数量 |
getCollisionBetweenGroups(group1 : string , group2 : string ): boolean |
获取两碰撞组之间的碰撞关系(是否可发生碰撞) |
getValidCollisionGroups(): string [] |
获取当前已添加的碰撞组名列表 |
isCollisionGroupValid(name : string ): boolean |
检测碰撞组是否有效(已添加过) |
renameCollisionGroup(previousName : string , newName : string ): void |
重命名碰撞组 |
setCollisionBetweenGroups(group1 : string , group2 : string , collidable : boolean ): void |
设置俩碰撞组之间的碰撞关系(是否可发生碰撞) |
Methods
addCollisionGroup
• Static
addCollisionGroup(name
): void
添加新碰撞组
Parameters
name string | 碰撞组名 range: 不限制 |
---|
ts
@Component
export default class CollisionGroup extends Script {
// 当脚本被实例后,会在第一帧更新前调用此函数
protected onStart(): void {
PhysicsService.addCollisionGroup("GroupA");
PhysicsService.addCollisionGroup("GroupB");
PhysicsService.setCollisionBetweenGroups("GroupA", "GroupB", false);
// 创建模拟物理的球体A并设置碰撞组为GroupA
InputUtil.onKeyDown(Keys.One, ()=>{
PhysicsService.addCollisionGroup("GroupC");
console.log(PhysicsService.getValidCollisionGroups());
})
}
}
@Component
export default class CollisionGroup extends Script {
// 当脚本被实例后,会在第一帧更新前调用此函数
protected onStart(): void {
PhysicsService.addCollisionGroup("GroupA");
PhysicsService.addCollisionGroup("GroupB");
PhysicsService.setCollisionBetweenGroups("GroupA", "GroupB", false);
// 创建模拟物理的球体A并设置碰撞组为GroupA
InputUtil.onKeyDown(Keys.One, ()=>{
PhysicsService.addCollisionGroup("GroupC");
console.log(PhysicsService.getValidCollisionGroups());
})
}
}
deleteCollisionGroup
• Static
deleteCollisionGroup(name
): void
删除已有碰撞组
Parameters
name string | 碰撞组名 range: 不限制 |
---|
ts
@Component
export default class CollisionGroup extends Script {
// 当脚本被实例后,会在第一帧更新前调用此函数
protected onStart(): void {
PhysicsService.addCollisionGroup("GroupA");
PhysicsService.addCollisionGroup("GroupB");
PhysicsService.setCollisionBetweenGroups("GroupA", "GroupB", false);
// 创建模拟物理的球体A并设置碰撞组为GroupA
InputUtil.onKeyDown(Keys.One, ()=>{
PhysicsService.deleteCollisionGroup("GroupA");
console.log(PhysicsService.getValidCollisionGroups());
})
}
protected onUpdate(dt: number): void {
}
// 脚本被销毁时最后一帧执行完调用此函数
protected onDestroy(): void {
}
}
@Component
export default class CollisionGroup extends Script {
// 当脚本被实例后,会在第一帧更新前调用此函数
protected onStart(): void {
PhysicsService.addCollisionGroup("GroupA");
PhysicsService.addCollisionGroup("GroupB");
PhysicsService.setCollisionBetweenGroups("GroupA", "GroupB", false);
// 创建模拟物理的球体A并设置碰撞组为GroupA
InputUtil.onKeyDown(Keys.One, ()=>{
PhysicsService.deleteCollisionGroup("GroupA");
console.log(PhysicsService.getValidCollisionGroups());
})
}
protected onUpdate(dt: number): void {
}
// 脚本被销毁时最后一帧执行完调用此函数
protected onDestroy(): void {
}
}
getAvailableCollisionGroupsCount
• Static
getAvailableCollisionGroupsCount(): number
获取当前可剩余使用碰撞组数量
Returns
number | 剩余可用碰撞组数量 |
---|
ts
@Component
export default class CollisionGroup extends Script {
// 当脚本被实例后,会在第一帧更新前调用此函数
protected onStart(): void {
PhysicsService.addCollisionGroup("GroupA");
PhysicsService.addCollisionGroup("GroupB");
PhysicsService.setCollisionBetweenGroups("GroupA", "GroupB", false);
// 创建模拟物理的球体A并设置碰撞组为GroupA
InputUtil.onKeyDown(Keys.One, ()=>{
console.log(PhysicsService.getAvailableCollisionGroupsCount());
})
}
}
@Component
export default class CollisionGroup extends Script {
// 当脚本被实例后,会在第一帧更新前调用此函数
protected onStart(): void {
PhysicsService.addCollisionGroup("GroupA");
PhysicsService.addCollisionGroup("GroupB");
PhysicsService.setCollisionBetweenGroups("GroupA", "GroupB", false);
// 创建模拟物理的球体A并设置碰撞组为GroupA
InputUtil.onKeyDown(Keys.One, ()=>{
console.log(PhysicsService.getAvailableCollisionGroupsCount());
})
}
}
getCollisionBetweenGroups
• Static
getCollisionBetweenGroups(group1
, group2
): boolean
获取两碰撞组之间的碰撞关系(是否可发生碰撞)
Parameters
group1 string | 碰撞组名 1 range: 不限制 |
---|---|
group2 string | 碰撞组名 2 range: 不限制 |
Returns
boolean | 碰撞关系(是否可发生碰撞) |
---|
ts
@Component
export default class CollisionGroup extends Script {
// 当脚本被实例后,会在第一帧更新前调用此函数
protected onStart(): void {
PhysicsService.addCollisionGroup("GroupA");
PhysicsService.addCollisionGroup("GroupB");
PhysicsService.setCollisionBetweenGroups("GroupA", "GroupB", false);
// 创建模拟物理的球体A并设置碰撞组为GroupA
InputUtil.onKeyDown(Keys.One, ()=>{
console.log(PhysicsService.getCollisionBetweenGroups("GroupA", "GroupB"));
})
}
}
@Component
export default class CollisionGroup extends Script {
// 当脚本被实例后,会在第一帧更新前调用此函数
protected onStart(): void {
PhysicsService.addCollisionGroup("GroupA");
PhysicsService.addCollisionGroup("GroupB");
PhysicsService.setCollisionBetweenGroups("GroupA", "GroupB", false);
// 创建模拟物理的球体A并设置碰撞组为GroupA
InputUtil.onKeyDown(Keys.One, ()=>{
console.log(PhysicsService.getCollisionBetweenGroups("GroupA", "GroupB"));
})
}
}
getValidCollisionGroups
• Static
getValidCollisionGroups(): string
[]
获取当前已添加的碰撞组名列表
Returns
string [] | 当前已添加的碰撞组名列表 |
---|
ts
@Component
export default class CollisionGroup extends Script {
// 当脚本被实例后,会在第一帧更新前调用此函数
protected onStart(): void {
PhysicsService.addCollisionGroup("GroupA");
PhysicsService.addCollisionGroup("GroupB");
PhysicsService.setCollisionBetweenGroups("GroupA", "GroupB", false);
// 创建模拟物理的球体A并设置碰撞组为GroupA
InputUtil.onKeyDown(Keys.One, ()=>{
console.log(PhysicsService.getValidCollisionGroups());
})
}
}
@Component
export default class CollisionGroup extends Script {
// 当脚本被实例后,会在第一帧更新前调用此函数
protected onStart(): void {
PhysicsService.addCollisionGroup("GroupA");
PhysicsService.addCollisionGroup("GroupB");
PhysicsService.setCollisionBetweenGroups("GroupA", "GroupB", false);
// 创建模拟物理的球体A并设置碰撞组为GroupA
InputUtil.onKeyDown(Keys.One, ()=>{
console.log(PhysicsService.getValidCollisionGroups());
})
}
}
isCollisionGroupValid
• Static
isCollisionGroupValid(name
): boolean
检测碰撞组是否有效(已添加过)
Parameters
name string | 碰撞组名 range: 不限制 |
---|
Returns
boolean | 碰撞组是否有效 |
---|
ts
@Component
export default class CollisionGroup extends Script {
// 当脚本被实例后,会在第一帧更新前调用此函数
protected onStart(): void {
PhysicsService.addCollisionGroup("GroupA");
PhysicsService.addCollisionGroup("GroupB");
PhysicsService.setCollisionBetweenGroups("GroupA", "GroupB", false);
// 创建模拟物理的球体A并设置碰撞组为GroupA
InputUtil.onKeyDown(Keys.One, ()=>{
console.log(PhysicsService.isCollisionGroupValid("GroupC"));
})
}
}
@Component
export default class CollisionGroup extends Script {
// 当脚本被实例后,会在第一帧更新前调用此函数
protected onStart(): void {
PhysicsService.addCollisionGroup("GroupA");
PhysicsService.addCollisionGroup("GroupB");
PhysicsService.setCollisionBetweenGroups("GroupA", "GroupB", false);
// 创建模拟物理的球体A并设置碰撞组为GroupA
InputUtil.onKeyDown(Keys.One, ()=>{
console.log(PhysicsService.isCollisionGroupValid("GroupC"));
})
}
}
renameCollisionGroup
• Static
renameCollisionGroup(previousName
, newName
): void
重命名碰撞组
Parameters
previousName string | 碰撞组名 range: 不限制 |
---|---|
newName string | 新碰撞组名 range: 不限制 |
ts
@Component
export default class CollisionGroup extends Script {
// 当脚本被实例后,会在第一帧更新前调用此函数
protected onStart(): void {
PhysicsService.addCollisionGroup("GroupA");
PhysicsService.addCollisionGroup("GroupB");
PhysicsService.setCollisionBetweenGroups("GroupA", "GroupB", false);
// 创建模拟物理的球体A并设置碰撞组为GroupA
InputUtil.onKeyDown(Keys.One, ()=>{
PhysicsService.renameCollisionGroup("GroupA","GroupC");
console.log(PhysicsService.getValidCollisionGroups());
})
}
protected onUpdate(dt: number): void {
}
// 脚本被销毁时最后一帧执行完调用此函数
protected onDestroy(): void {
}
}
@Component
export default class CollisionGroup extends Script {
// 当脚本被实例后,会在第一帧更新前调用此函数
protected onStart(): void {
PhysicsService.addCollisionGroup("GroupA");
PhysicsService.addCollisionGroup("GroupB");
PhysicsService.setCollisionBetweenGroups("GroupA", "GroupB", false);
// 创建模拟物理的球体A并设置碰撞组为GroupA
InputUtil.onKeyDown(Keys.One, ()=>{
PhysicsService.renameCollisionGroup("GroupA","GroupC");
console.log(PhysicsService.getValidCollisionGroups());
})
}
protected onUpdate(dt: number): void {
}
// 脚本被销毁时最后一帧执行完调用此函数
protected onDestroy(): void {
}
}
setCollisionBetweenGroups
• Static
setCollisionBetweenGroups(group1
, group2
, collidable
): void
设置俩碰撞组之间的碰撞关系(是否可发生碰撞)
Parameters
group1 string | 碰撞组名1 range: 不限制 |
---|---|
group2 string | 碰撞组名2 range: 不限制 |
collidable boolean | 是否可碰撞 |
ts
@Component
export default class CollisionGroup extends Script {
// 当脚本被实例后,会在第一帧更新前调用此函数
protected onStart(): void {
PhysicsService.addCollisionGroup("GroupA");
PhysicsService.addCollisionGroup("GroupB");
PhysicsService.setCollisionBetweenGroups("GroupA", "GroupB", false);
// 创建模拟物理的球体A并设置碰撞组为GroupA
InputUtil.onKeyDown(Keys.One, ()=>{
this.serverCreateBall(new Vector(300, 0, 0), "GroupA", true);
})
// 创建正常碰撞的球体B并设置碰撞组为GroupB
InputUtil.onKeyDown(Keys.Two, ()=>{
this.serverCreateBall(new Vector(600, 0, 0), "GroupB", false);
})
}
@mw.RemoteFunction(mw.Server)
serverCreateBall(pos:Vector, Group:string, bPhysicsSimulate:boolean)
{
GameObject.asyncSpawn("197388", {replicates:true}).then((obj)=>{
let ball = obj as mw.Model;
ball.worldTransform.position = pos;
if (bPhysicsSimulate)
{
ball.physicsEnabled = true;
ball.massEnabled = true;
ball.mass = 50;
}
ball.collisionGroup = Group;
})
}
}
@Component
export default class CollisionGroup extends Script {
// 当脚本被实例后,会在第一帧更新前调用此函数
protected onStart(): void {
PhysicsService.addCollisionGroup("GroupA");
PhysicsService.addCollisionGroup("GroupB");
PhysicsService.setCollisionBetweenGroups("GroupA", "GroupB", false);
// 创建模拟物理的球体A并设置碰撞组为GroupA
InputUtil.onKeyDown(Keys.One, ()=>{
this.serverCreateBall(new Vector(300, 0, 0), "GroupA", true);
})
// 创建正常碰撞的球体B并设置碰撞组为GroupB
InputUtil.onKeyDown(Keys.Two, ()=>{
this.serverCreateBall(new Vector(600, 0, 0), "GroupB", false);
})
}
@mw.RemoteFunction(mw.Server)
serverCreateBall(pos:Vector, Group:string, bPhysicsSimulate:boolean)
{
GameObject.asyncSpawn("197388", {replicates:true}).then((obj)=>{
let ball = obj as mw.Model;
ball.worldTransform.position = pos;
if (bPhysicsSimulate)
{
ball.physicsEnabled = true;
ball.massEnabled = true;
ball.mass = 50;
}
ball.collisionGroup = Group;
})
}
}