12-20-2007, 11:29 AM
|
#1
|
|
Bracer
Join Date: Jul 2007
Posts: 111
|
Angular & Speed Based Motion Code Here.
I've created this function to emulate a directional based motion establisher in Actionscript from Game Maker instead of your usual Hspeed & Vspeed.
////////////////////////////////////////////////////////////////////////////
/*
Bracer, this function allows you to emulate Game Maker's
version of the "Move Free" Function.
*/
function MoveFree (ObjectToMove, Speed, Direction)
{
//Direction = Angle
//Speed = Hypotenuse
Direction = - Direction; //Actually, this inverse is NOT suppose to be,
//however since Game Maker's rotation for some reason is counter clockwise based,
//I might as well match it for consistency.
//We first retrieve the next ._x position of your object.
//Cosine(Angle) = Adjacent/Hypotenuse
//Cosine(Direction) = _x position / Speed
ObjectToMove._x += Math.cos(Math.PI/180 * Direction) * Speed;
//Now time to handle the ._y position of your object.
//Sine(Angle) = Opposite / Hypotenuse
//Sine(Direction) = ._y / Speed
ObjectToMove._y += Math.sin(Math.PI/180 * Direction) * Speed;
//The extra stupid "Math.PI/180 *" is due to the stupid fact that
//flash's trigonometry functions are based on radian, WTF right ?
}
////////////////////////////////////////////////////////////////////////////
Ball_mc.onEnterFrame = function()
{
MoveFree(this,5,45);
}
Hope you find this useful.
__________________
Available Freelancer
|
|
|
01-07-2008, 04:59 PM
|
#2
|
|
Registered User
Join Date: Jan 2008
Posts: 6
|
Very useful thanks
I'm a GMLer trying out actionscript too, and AS has NO DAMN FUNCTIONS! how can anyone build games with a language with no distance calculating function? No speed variable?
I had a look online to find GML functions for actionscript and found that thread of yours that got stopped on the GML forum. That was pretty harsh of them. So i kept looking and found this one
So anyway i'm trying to set up a library of GML functions so that you can call them in actionscript, and this is pretty helpful. I'm making the ones i use most first, point_direction, point_distance, lengthdir_x, lengthdir_y, motion_add etc.
So if we both keep making AS GML functions, we can share them and end up with a complete GML library! I'll send you my ones when i'm done making them.
|
|
|
01-07-2008, 05:46 PM
|
#3
|
|
Super Moderator
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,514
|
Me I'm more interested about collision detection. Moving stuff around is pretty straight forward to me but for some reason, I find it harder to get a good collision detection in 2D Flash than in 3D gaming with C++ and OpenGL. Obviously my MCs have from time to time a tendency to pass throught other MCs, get caught by other MCs, or bounce back with the wrong angle. By tweaking my code and trying and trying I was able to low these tendencies to about 1 or 2 % of the time but still it's not good enough. Since these bad behaviors increase as the speed increase it was quite easy to find what was wrong but still the perfect solution did not appear to me!
|
|
|
01-07-2008, 07:14 PM
|
#4
|
|
Bracer
Join Date: Jul 2007
Posts: 111
|
Fantastic Afromonkey0!
I love the idea of doing something for the community.
Let me know what codes you have come up with in time
You can catch up on my development on "Interactive Land".
This blog is not entitled exclusively to Game Maker though, it's more toward interactivity in general
__________________
Available Freelancer
|
|
|
01-08-2008, 03:06 PM
|
#5
|
|
Registered User
Join Date: Jan 2008
Posts: 6
|
I'm having some problems replicating GML functions actually. I made the functions global, so any object can use them, but i can't find a way for a script to know which object is calling it. It's annoying to have to pass the name as a parameter, i guess that's what i'll do though.
|
|
|
01-08-2008, 03:27 PM
|
#6
|
|
Registered User
Join Date: Jan 2008
Posts: 6
|
I've made a bunch of functions, passing the name as a variable with 'this', but all the ones which involve variables in the calling object don't work.
Here's the code for the function declarations
Code:
stop();
_global.GMLinit = function(object) {
var object._speed;
var object._direction;
var object._hspeed;
var object._vspeed;
};
_global.GMLstep = function(object) {
setvector(object);
setsplit(object);
object._x += object._hspeed;
object._y += object._vspeed;
};
_global.point_distance = function(x1, y1, x2, y2) {
var dx = x2-x1;
var dy = y2-y1;
return Math.sqrt(dx*dx+dy*dy);
};
_global.point_direction = function(x1, y1, x2, y2) {
var dx = x2-x1;
var dy = y2-y1;
// value in radians; multiply by (180/Math.PI) for degrees
return ((Math.atan2(dy, dx))*(180/math.PI));
};
_global.lengthdir_x = function(len, dir) {
return (Math.cos(Math.PI/180*dir)*len);
};
_global.lengthdir_y = function(len, dir) {
return (Math.sin(Math.PI/180*dir)*len);
};
_global.setvector = function(object) {
// sets direction and speed from hspeed and vpeed
object._direction = point_direction(0, 0, object._hspeed, object._vspeed);
object._speed = point_distance(0, 0, object._hspeed, object._vspeed);
};
_global.setsplit = function(object) {
// sets hspeed and vspeed from speed and direction
object._hspeed = lengthdir_x(object._speed, object._direction);
object._vspeed = lengthdir_y(object._speed, object._direction);
};
_global.motion_add = function(object, speed, dir) {
object._hspeed += lengthdir_x(speed, dir);
object._vspeed += lengthdir_y(speed, dir);
setvector(object);
};
It's probably something stupid, since this is my first ever actionscript code.
|
|
|
01-08-2008, 03:28 PM
|
#7
|
|
Bracer
Join Date: Jul 2007
Posts: 111
|
That's what I do too, pass "this" as an argument.
Ha Ha Ha
__________________
Available Freelancer
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 05:59 AM.
///
|
|