PDA

Просмотр полной версии : Физика шара


DoctorQueb
15.03.2013, 16:24
Привет!

Помогите создать имитацию того, что шар действительно "катится" по земле, а не просто движется.
Большая часть кода уже написана, осталось доделать только это.

Нужно что-то химичить с _rotation, но как правильно не могу въехать.

xVel = 5;
yVel = 5;
gravity = 1.2;
fr = 0.7;
radiusH = ball._height/2;
radiusW = ball._width/2;
angle = 10;

shakeI = 50;
oldShakeI = shakeI;
shaking = false;

ball.onEnterFrame = function()
{
yVel += gravity;
this._x += xVel;
this._y += yVel;
if (this._y>=Stage.height-radiusH)
{
this._y = Stage.height-radiusH;
yVel *= -1;
if (yVel<=-10)
{
shaking = true;
createParticles(int(Math.random()*50)+1,this);
createSmoke(30,70,this);
}
else if (yVel<=-20)
{
createParticles(int(Math.random()*5)+1,this);
createSmoke(5,70,this);

}
}
if (this._y<=0+radiusH)
{
this._y = 0+radiusH;
yVel *= -1;
}
if (this._x>=Stage.width-radiusW)
{
this._x = Stage.width-radiusW;
xVel *= -1;
}
if (this._x<=0+radiusW)
{
this._x = 0+radiusW;
xVel *= -1;
}
};
}

Заранее спасибо за помощь!

Tails
15.03.2013, 17:56
Примерно так:

var speed:Number = 0.2;

onEnterFrame = function() {
moveBall(ball1);
moveBall(ball2);
moveBall(ball3);
}

function moveBall(clip){
clip._x += speed;
var p:Number = (1 / (clip._width * Math.PI)) * speed;
clip._rotation += 360 * p;
}

circle-move.swf

Hauts
15.03.2013, 18:04
Можно _rotation привязать к _x * некий коэффициент. В простых случаях хорошо подходит.

DoctorQueb
15.03.2013, 18:54
Примерно так:

var speed:Number = 0.2;

onEnterFrame = function() {
moveBall(ball1);
moveBall(ball2);
moveBall(ball3);
}

function moveBall(clip){
clip._x += speed;
var p:Number = (1 / (clip._width * Math.PI)) * speed;
clip._rotation += 360 * p;
}

circle-move.swf
Спасибо большое) А что за формула такая? (1 / (clip._width * Math.PI)) * speed; ?

Tails
15.03.2013, 19:01
DoctorQueb,
Нахождение процента изменения ротации от длины прокатившейся дистанций.

http://upload.wikimedia.org/wikipedia/commons/thumb/2/2a/Pi-unrolled-720.gif/300px-Pi-unrolled-720.gif

DoctorQueb
15.03.2013, 19:08
Отлично! Теперь то, что надо!