需要Matter.js计算力 [英] Matter.js calculating force needed

查看:66
本文介绍了需要Matter.js计算力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对物体施加力.要使其以我的鼠标位置相对于该对象生成的角度移动.

Im trying to apply a force to an object. To get it to move in the angle that my mouseposition is generating relative to the object.

我有角度

 targetAngle = Matter.Vector.angle(myBody.pos, mouse.position);

现在,我需要施加力,以使身体沿该角度移动.我在下面的applyForce方法中输入什么值?

Now I need to apply a force, to get the body to move along that angle. What do I put in the values below for the applyForce method?

  // applyForce(body, position, force)

  Body.applyForce(myBody, {
    x : ??, y : ??
  },{
    x:??, y: ?? // how do I derive this force??
  });

如何在此处输入x和y值,以使主体沿鼠标和主体之间的角度移动.

What do I put in the x and y values here to get the body to move along the angle between the mouse and the body.

推荐答案

您可以依赖 Matter.Vector 模块并将其用于减去,归一化和乘以位置矢量:

You can rely on the Matter.Vector module and use it to substract, normalize and multiply positions vectors:

var force = 10;
var deltaVector = Matter.Vector.sub(mouse.position, myBody.position);
var normalizedDelta = Matter.Vector.normalise(deltaVector);
var forceVector = Matter.Vector.mult(normalizedDelta, force);
Body.applyForce(myBody, myBody.position, forceVector);

这篇关于需要Matter.js计算力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆