PDA

View Full Version : Create something like Tilt2


insanepolak
07-22-2008, 09:49 PM
I'm trying to make something like Tilt2 (http://www.alexheaton.co.uk/games/tilt2/Tilt2.swf).

I've got it to tilt moving left and right with the mouse.
But I'm having trouble getting it to tilt and at the same time move with the mouse.

Am I thinking the right way?
Any help would be great

rrh
07-23-2008, 05:14 PM
Two things you'll want to learn about:
Math.atan2(y,x)
Inverse Kinematics

insanepolak
07-23-2008, 07:02 PM
Alright so I did some research and got something working.
move = function () {
this._x = _xmouse;
this._y = _ymouse;
var distance_x = stick._x-this._x;
var distance_y = stick._y-this._y;
var alpha = Math.atan2(distance_y, distance_x);
stick._x = this._x+Math.cos(alpha)*totalDistance;
stick._y = this._y+Math.sin(alpha)*totalDistance;
stick._rotation = (Math.PI+alpha)*180/Math.PI;
trace(stick._rotation);

};
press = function () {
this.onMouseMove = move;
};
release = function () {
delete this.onMouseMove;
};

var totalDistance = 170;

ball.onPress = press;
ball.onRelease = release;
ball.onReleaseOutside = release;
ball.move = move;


Now I'm trying to get the stick to rotate around the ball until it points down, but I'm having trouble making a loop to rotate properly around the ball.

I tried to make the rotation point the origin of the stick at 0,0 but then the totalDistance is 0 which screws up the move function rotation.

insanepolak
07-23-2008, 07:18 PM
now I've made the ball the mouse, still trying to get the "balance" aspect working..

onEnterFrame = function() {
ball._x = _xmouse;
ball._y = _ymouse;
ball.onMouseMove = move
};
move = function () {
var totalDistance = 170;
this._x = _xmouse;
this._y = _ymouse;
var distance_x = stick._x-this._x;
var distance_y = stick._y-this._y;
var alpha = Math.atan2(distance_y, distance_x);
stick._x = this._x+Math.cos(alpha)*totalDistance;
stick._y = this._y+Math.sin(alpha)*totalDistance;
stick._rotation = (Math.PI+alpha)*180/Math.PI;
};
var totalDistance = 170;

ball.move = move;

newblack
07-24-2008, 03:04 PM
i don't really like the end goal (tilt2)... i think that what makes it fun is the physics and they're at best crap. you could use a physics engine like box2d or try to write the equations of motion yourself. depending on how geeky you are the latter could be either horrible or fun.