创建向量和冲突 [英] Create vectors and collisions

查看:171
本文介绍了创建向量和冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个球和一根棍子(用于台球比赛)。

I have a ball and a stick (for a billiard game).

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

First the ball is placed in a position of a table. On clicking the ball the stick appears, in such a way that we can determine the angle by which stick is placed by clicking the ball (on clicking we determine the angle of mouse with respect to centre of ball and place the stick at that angle touching the ball).

所以现在棍子也在桌子上。现在我只是沿着那个角度拖动棍子,如果拖动另一个角度而不是初始角度它返回false。

So now the stick is also in the table. Now I am dragging the stick along that angle only, if dragged in another angle than the initial angle it returns false.

在拖动结束时我正在计算移动的距离棒和棒回到接触球的初始位置。然后我试图相对于棍子的角度和棍子移动的距离移动球。

On drag end I am calculating the distance moved by the stick and the stick returns to the initial position touching the ball. Then I am trying to move the ball with respect to the angle of the stick and the distance moved by the stick.

球在这里移动但不是关于任何一个这些。这已经成为我的问题我已经更新了小提琴 here

The ball moves here but not with respect to any of these. That has become my issue I have updated the 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!!!!
});


推荐答案

你计算台球杆的角度像这样的球:

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);

你想要的掷骰距离将取决于杖从球上拉回的距离。

Your desired roll distance would be based on how far the stick was drawn back away from the ball.

棒被拉回的次数越多= =球越往前走。

The further the stick was drawn back == the further the ball would travel.

你可以计算从棒到球的距离,如下所示:

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

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

这篇关于创建向量和冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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