文章目录
  1. 1. 为刚体设置属性
    1. 1.1. 设置刚体的属性
    2. 1.2. 返回总目录

本节主要介绍为刚体设置属性。

为刚体设置属性


设置刚体的属性

创建一个刚体之后,还可以对它进行许多操作,比如设置质量,访问其位置和速度,施加力,以及转换点和向量等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// 设置刚体的属性
var position = new b2Vec2(200, 100); //定义一个点坐标(200,100)
var rotation = 0.3 * Math.PI; //定义一个弧度值
spclBodyDef.position.Set(basicX, basicY); //设置物体的初始位置置,括号内是矢量坐标
slectBody.SetCenterPosition(position, rotation); //设置选中的刚体的中心位置及其旋转弧度

var CenterPosition = new b2Vec2();
CenterPosition = slectBody.GetCenterPosition();
alert(CenterPosition.x + "," + CenterPosition.y);

var RotationMatrix = new b2Mat22();
RotationMatrix = slectBody.GetRotationMatrix(); //获取一个2X2的刚体旋转矩阵
alert(RotationMatrix);

var LinearVelocity = new b2Vec2(500, -500); //定义一个线速度
slectBody.WakeUp(); //唤醒选中物体,刷新数据
slectBody.SetLinearVelocity(LinearVelocity); //设定选中物体的线速度

var LinearVelocity = new b2Vec2();
LinearVelocity = slectBody.GetLinearVelocity(); //获取选中刚体的线速度向量
alert(LinearVelocity.x + "," + LinearVelocity.y);

var AngularVelocity = 100; //定义一个角速度
slectBody.WakeUp();
slectBody.SetAngularVelocity(AngularVelocity); //设定选中物体的角速度

var AngularVelocity;
AngularVelocity = slectBody.GetAngularVelocity(); //获取选中刚体的角速度向量
alert(AngularVelocity);

var force = new b2Vec2(10000, 1000000000); //定义一个力的大小及方向
var point = new b2Vec2(slectBody.GetCenterPosition().x, slectBody.GetCenterPosition().y);
slectBody.WakeUp();
//在指定点给物体施加一个已知大小与方向的力
//施加一个力,需要很大才有效果,不如直接给定线速度,一般给力据F=ma给定
slectBody.ApplyForce(force, point);
alert(slectBody.m_linearVelocity);

var torque = 100000000000; //定义一个力矩
slectBody.WakeUp();
slectBody.ApplyTorque(torque); //设定选定物体的力矩,数值较大,可以通过设定角速度大小实现相同功能

var impulse = new b2Vec2(0, 100000000); //定义一个物体的冲量
var point = new b2Vec2(slectBody.GetCenterPosition().x, slectBody.GetCenterPosition().y);
slectBody.WakeUp();
//在指定点给物体施加一个已知大小与方向的冲量,一般在初始的时候给定,以便决定初始运动
slectBody.ApplyImpulse(impulse, point);

返回总目录

码生艰难,写文不易,给我家猪囤点猫粮了喵~

B站: 被删

查看Github有更多内容噢:https://github.com/godbasin
更欢迎来被删的前端游乐场边撸猫边学前端噢

如果你想要关注日常生活中的我,欢迎关注“牧羊的猪”公众号噢

作者:被删

出处:https://godbasin.github.io

本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

文章目录
  1. 1. 为刚体设置属性
    1. 1.1. 设置刚体的属性
    2. 1.2. 返回总目录