本文整理匯總了Java中org.jbox2d.common.Vec2.normalize方法的典型用法代碼示例。如果您正苦於以下問題:Java Vec2.normalize方法的具體用法?Java Vec2.normalize怎麽用?Java Vec2.normalize使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.jbox2d.common.Vec2
的用法示例。
在下文中一共展示了Vec2.normalize方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: PrismaticJoint
import org.jbox2d.common.Vec2; //導入方法依賴的package包/類
protected PrismaticJoint(IWorldPool argWorld, PrismaticJointDef def) {
super(argWorld, def);
m_localAnchorA = new Vec2(def.localAnchorA);
m_localAnchorB = new Vec2(def.localAnchorB);
m_localXAxisA = new Vec2(def.localAxisA);
m_localXAxisA.normalize();
m_localYAxisA = new Vec2();
Vec2.crossToOutUnsafe(1f, m_localXAxisA, m_localYAxisA);
m_referenceAngle = def.referenceAngle;
m_impulse = new Vec3();
m_motorMass = 0.0f;
m_motorImpulse = 0.0f;
m_lowerTranslation = def.lowerTranslation;
m_upperTranslation = def.upperTranslation;
m_maxMotorForce = def.maxMotorForce;
m_motorSpeed = def.motorSpeed;
m_enableLimit = def.enableLimit;
m_enableMotor = def.enableMotor;
m_limitState = LimitState.INACTIVE;
m_K = new Mat33();
m_axis = new Vec2();
m_perp = new Vec2();
}
示例2: updateSensor
import org.jbox2d.common.Vec2; //導入方法依賴的package包/類
public void updateSensor(){
Body bsensor = m_sensor.getBody();
CircleShape circle = (CircleShape) m_sensor.getShape();
Vec2 sensor_pos = bsensor.getWorldPoint(circle.m_p);
// iterate through all contacts and apply a force on shapes that overlap the sensor.
for(Contact contact = world.getContactList(); contact != null; contact = contact.m_next){
Fixture fixtureA = contact.getFixtureA();
Fixture fixtureB = contact.getFixtureB();
Body body = null;
if(fixtureA == m_sensor) body = fixtureB.getBody();
else if(fixtureB == m_sensor) body = fixtureA.getBody();
else continue;
Vec2 body_pos = body.getPosition();
Vec2 dist = sensor_pos.sub(body_pos);
if (dist.lengthSquared() > Settings.EPSILON * Settings.EPSILON) {
dist.normalize();
Vec2 force = dist.mulLocal(200f);
body.applyForce(force, body_pos);
}
}
}
示例3: updateSensor
import org.jbox2d.common.Vec2; //導入方法依賴的package包/類
public void updateSensor() {
Body bsensor = m_sensor.getBody();
CircleShape circle = (CircleShape) m_sensor.getShape();
Vec2 sensor_pos = bsensor.getWorldPoint(circle.m_p);
// Traverse the bodies. Apply a force on shapes that overlap the sensor.
for (Body body = world.getBodyList(); body != null; body = body.getNext()) {
if(body == bsensor){
continue;
}
boolean is_touching = false;
DwBody dwbody = DwWorld.getShape(body);
if(dwbody != null){
if(dwbody.m_userData instanceof Boolean){
is_touching = (Boolean) dwbody.m_userData;
}
}
if (is_touching) {
Vec2 body_pos = body.getPosition();
Vec2 dist = sensor_pos.sub(body_pos);
if (dist.lengthSquared() > Settings.EPSILON * Settings.EPSILON) {
dist.normalize();
Vec2 force = dist.mulLocal(200f);
body.applyForce(force, body_pos);
}
}
}
}
示例4: raycast
import org.jbox2d.common.Vec2; //導入方法依賴的package包/類
/**
* @param callback
* @param point1
* @param point2
*/
public void raycast(ParticleRaycastCallback callback, final Vec2 point1, final Vec2 point2) {
if (m_proxyCount == 0) {
return;
}
int firstProxy =
lowerBound(
m_proxyBuffer,
m_proxyCount,
computeTag(m_inverseDiameter * MathUtils.min(point1.x, point2.x) - 1, m_inverseDiameter
* MathUtils.min(point1.y, point2.y) - 1));
int lastProxy =
upperBound(
m_proxyBuffer,
m_proxyCount,
computeTag(m_inverseDiameter * MathUtils.max(point1.x, point2.x) + 1, m_inverseDiameter
* MathUtils.max(point1.y, point2.y) + 1));
float fraction = 1;
// solving the following equation:
// ((1-t)*point1+t*point2-position)^2=diameter^2
// where t is a potential fraction
final float vx = point2.x - point1.x;
final float vy = point2.y - point1.y;
float v2 = vx * vx + vy * vy;
if (v2 == 0) v2 = Float.MAX_VALUE;
for (int proxy = firstProxy; proxy < lastProxy; ++proxy) {
int i = m_proxyBuffer[proxy].index;
final Vec2 posI = m_positionBuffer.data[i];
final float px = point1.x - posI.x;
final float py = point1.y - posI.y;
float pv = px * vx + py * vy;
float p2 = px * px + py * py;
float determinant = pv * pv - v2 * (p2 - m_squaredDiameter);
if (determinant >= 0) {
float sqrtDeterminant = MathUtils.sqrt(determinant);
// find a solution between 0 and fraction
float t = (-pv - sqrtDeterminant) / v2;
if (t > fraction) {
continue;
}
if (t < 0) {
t = (-pv + sqrtDeterminant) / v2;
if (t < 0 || t > fraction) {
continue;
}
}
final Vec2 n = tempVec;
tempVec.x = px + t * vx;
tempVec.y = py + t * vy;
n.normalize();
final Vec2 point = tempVec2;
point.x = point1.x + t * vx;
point.y = point1.y + t * vy;
float f = callback.reportParticle(i, point, n, t);
fraction = MathUtils.min(fraction, f);
if (fraction <= 0) {
break;
}
}
}
}
示例5: solvePositionConstraints
import org.jbox2d.common.Vec2; //導入方法依賴的package包/類
@Override
public boolean solvePositionConstraints(final SolverData data) {
if (m_frequencyHz > 0.0f) {
return true;
}
final Rot qA = pool.popRot();
final Rot qB = pool.popRot();
final Vec2 rA = pool.popVec2();
final Vec2 rB = pool.popVec2();
final Vec2 u = pool.popVec2();
Vec2 cA = data.positions[m_indexA].c;
float aA = data.positions[m_indexA].a;
Vec2 cB = data.positions[m_indexB].c;
float aB = data.positions[m_indexB].a;
qA.set(aA);
qB.set(aB);
Rot.mulToOutUnsafe(qA, u.set(m_localAnchorA).subLocal(m_localCenterA), rA);
Rot.mulToOutUnsafe(qB, u.set(m_localAnchorB).subLocal(m_localCenterB), rB);
u.set(cB).addLocal(rB).subLocal(cA).subLocal(rA);
float length = u.normalize();
float C = length - m_length;
C = MathUtils.clamp(C, -Settings.maxLinearCorrection, Settings.maxLinearCorrection);
float impulse = -m_mass * C;
float Px = impulse * u.x;
float Py = impulse * u.y;
cA.x -= m_invMassA * Px;
cA.y -= m_invMassA * Py;
aA -= m_invIA * (rA.x * Py - rA.y * Px);
cB.x += m_invMassB * Px;
cB.y += m_invMassB * Py;
aB += m_invIB * (rB.x * Py - rB.y * Px);
// data.positions[m_indexA].c.set(cA);
data.positions[m_indexA].a = aA;
// data.positions[m_indexB].c.set(cB);
data.positions[m_indexB].a = aB;
pool.pushVec2(3);
pool.pushRot(2);
return MathUtils.abs(C) < Settings.linearSlop;
}
示例6: solvePositionConstraints
import org.jbox2d.common.Vec2; //導入方法依賴的package包/類
@Override
public boolean solvePositionConstraints(final SolverData data) {
Vec2 cA = data.positions[m_indexA].c;
float aA = data.positions[m_indexA].a;
Vec2 cB = data.positions[m_indexB].c;
float aB = data.positions[m_indexB].a;
final Rot qA = pool.popRot();
final Rot qB = pool.popRot();
final Vec2 u = pool.popVec2();
final Vec2 rA = pool.popVec2();
final Vec2 rB = pool.popVec2();
final Vec2 temp = pool.popVec2();
qA.set(aA);
qB.set(aB);
// Compute the effective masses.
Rot.mulToOutUnsafe(qA, temp.set(m_localAnchorA).subLocal(m_localCenterA), rA);
Rot.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subLocal(m_localCenterB), rB);
u.set(cB).addLocal(rB).subLocal(cA).subLocal(rA);
float length = u.normalize();
float C = length - m_maxLength;
C = MathUtils.clamp(C, 0.0f, Settings.maxLinearCorrection);
float impulse = -m_mass * C;
float Px = impulse * u.x;
float Py = impulse * u.y;
cA.x -= m_invMassA * Px;
cA.y -= m_invMassA * Py;
aA -= m_invIA * (rA.x * Py - rA.y * Px);
cB.x += m_invMassB * Px;
cB.y += m_invMassB * Py;
aB += m_invIB * (rB.x * Py - rB.y * Px);
pool.pushRot(2);
pool.pushVec2(4);
// data.positions[m_indexA].c = cA;
data.positions[m_indexA].a = aA;
// data.positions[m_indexB].c = cB;
data.positions[m_indexB].a = aB;
return length - m_maxLength < Settings.linearSlop;
}