尝试开发一个Agar克隆,并且我已经获得了很多,但我无法弄清楚如何随着质量的增加而降低玩家的速度。我尝试了几种不同的方式,但没有任何作用。当质量上升时,我怎么能让速度下降?这是我的jsFiddle。这是我设定球员速度的地方:
var playerOneMass = 36;
var player1X = (canvas.width / 2) + 50;
var player = new Player({
x: player1X,
y: canvas.height / 2,
radius: playerOneMass,
speed: {
x: 5,
y: 5
},
name: "player 1",
dir: null
});
var playerTwoMass = 36;
var player2X = (canvas.width / 2) - 50;
var player2 = new Player({
x: player2X,
y: canvas.height / 2,
radius: playerTwoMass,
speed: {
x: 5,
y: 5
},
name: "player 2",
dir: null
});
答案 0 :(得分:4)
让我们带一些数学来帮助我们一点点。当你希望随着另一个变大的东西变小时,我发现的最佳选择是使用inversely proportional关系。这样可以为您提供更小更小的外观。
new_speed = scalar * start_speed / current_mass
在提出标量时,我发现最好先进行反复试验,直到它看起来如此。
以下是利用Two.js实施的等式的示例。
var two = new Two({width:320, height:180}).appendTo(document.getElementById("mytwo")),
rect = two.makeRectangle(100, 100, 10, 10),
circ = two.makeCircle(5, 100, 5),
mass = 10,
rspeed = Math.PI / 10,
mspeed = 14,
scalar = 10;
// Make it look pretty!
rect.fill = "rgb(100,255,100)";
circ.fill = "rgb(100,100,255)";
// Looping...
two.bind('update', function(fc) {
// Prevents from growing indefinetely
if(mass > 150) return;
mass += 1.5;
rect.scale += .1;
circ.scale += .1;
rect.rotation += scalar * rspeed / mass;
circ.translation.addSelf(new Two.Vector(
scalar * mspeed / mass, 0));
}).play();

<script src="https://rawgit.com/jonobr1/two.js/master/build/two.min.js"></script>
<div id="mytwo"><div></div></div>
&#13;