创建向量和碰撞

3

我有一个球和一个杆(用于台球游戏)。

首先将球放置在桌子上的一个位置。点击球后,杆出现了,这样我们可以确定杆的角度,通过单击球来确定鼠标相对于球心的角度,并将杆放置在那个角度接触球。

现在杆也在桌子上。现在我只沿着那个角度拖动杆,如果以另一个角度拖动,则返回false。

在拖动结束时,我计算了杆移动的距离,然后杆返回到接触球的初始位置。然后,我尝试根据杆的角度和杆移动的距离移动球。

球在这里移动,但不是根据任何这些移动的。这已经成为我的问题,我已经更新了fiddle here

strikerGroup.on('dragend', function () {
    var strikerLastPos = strikerGroup.getAbsolutePosition();
    strikerGroup.setPosition(initStrikerGrpX, initStrikerGrpY);
    striker.speedX = striker.speedY = 2;
    var strikerGrpDistMoved = Math.sqrt(((strikerLastPos.x - strikerGroup.getAbsolutePosition().x) * (strikerLastPos.x - strikerGroup.getAbsolutePosition().x)) + ((strikerLastPos.y - strikerGroup.getAbsolutePosition().y) * (strikerLastPos.y - strikerGroup.getAbsolutePosition().y)));
    var newX = striker.getX() + (Math.cos(theta) * strikerGrpDistMoved);
    var newY = striker.getY() - (Math.sin(theta) * strikerGrpDistMoved);
    var strikerMove = new Kinetic.Tween({
        node: striker,
        duration: 5,
        x: newX,
        y: newY,
        easing: Kinetic.Easings.EaseInOut
    });
    console.log(striker.getX());
    strikerMove.play();
    layer.batchDraw();
    // strikerGroup striked the striker!!!!
});
1个回答

2

你可以这样计算台球杆与球之间的角度:

var dx = ballX - stickX;
var dy = ballY - stickY;
var angle = Math.atan2(dy,dx);

然后你可以像这样沿着那个角度移动球:

var newBallX = ballX + desiredRollDistance * Math.cos(angle);
var newBallY = ballY + desiredRollDistance * Math.sin(angle);

您希望的滚动距离将基于手柄远离球的拉动距离。

手柄拉得越远 == 球飞行的距离越远。

您可以按以下方法计算手柄到球的距离:

var dx = ballX - stickX;
var dy = ballY - stickY;
var lengthFromStickToBall = Math.sqrt(dx*dx+dy*dy);

这是一个演示:http://jsfiddle.net/m1erickson/B6K9z/

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接