2014-03-19 116 views
3

我有一个球和一个棍子(用于台球游戏)。创建矢量和碰撞

首先将球放置在桌子的某个位置。点击球时,球杆出现,这样我们就可以通过点击球来确定球杆的放置角度(点击时我们确定鼠标相对于球的中心的角度,并将球杆放在那个角度上球)。

所以现在棒也在桌子上。现在,我只沿着该角度拖动棍子,如果以不同于初始角度的角度拖动它,则返回false。

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

球在这里移动,但没有关于这些中的任何一个。这已经成为我的问题,我已经更新了小提琴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!!!! 
}); 

回答

3

您计算台球棒的角度把球像这样:

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/

+0

谢谢。 它worked..Great帮助。 – Adidev