Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 12-20-2007, 11:29 AM   #1
Bracer
Bracer
 
Join Date: Jul 2007
Posts: 113
Send a message via MSN to Bracer
Default 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
Bracer is offline   Reply With Quote
Old 01-07-2008, 04:59 PM   #2
Afromonkey0
Registered User
 
Join Date: Jan 2008
Posts: 6
Default 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.
Afromonkey0 is offline   Reply With Quote
Old 01-07-2008, 05:46 PM   #3
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,528
Default

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!
ASWC is offline   Reply With Quote
Old 01-07-2008, 07:14 PM   #4
Bracer
Bracer
 
Join Date: Jul 2007
Posts: 113
Send a message via MSN to Bracer
Default

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
Bracer is offline   Reply With Quote
Old 01-08-2008, 03:06 PM   #5
Afromonkey0
Registered User
 
Join Date: Jan 2008
Posts: 6
Default

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.
Afromonkey0 is offline   Reply With Quote
Old 01-08-2008, 03:27 PM   #6
Afromonkey0
Registered User
 
Join Date: Jan 2008
Posts: 6
Default

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.
Afromonkey0 is offline   Reply With Quote
Old 01-08-2008, 03:28 PM   #7
Bracer
Bracer
 
Join Date: Jul 2007
Posts: 113
Send a message via MSN to Bracer
Default

That's what I do too, pass "this" as an argument.
Ha Ha Ha
__________________
Available Freelancer
Bracer is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Always get desired FPS in browser jasonoda ActionScript 1.0 (and below) 10 01-06-2011 01:52 PM
how to mount flash based file upload progress bar hmc ActionScript 2.0 1 04-09-2006 10:47 AM
Newbie struggling to seperate Drag and motion code.. silv1983 ActionScript 2.0 1 03-27-2006 12:25 PM
Creating fractal based terrain? dpvtank ActionScript 1.0 (and below) 0 07-26-2002 05:53 PM
code not working jaredly ActionScript 1.0 (and below) 6 06-05-2002 07:56 PM


All times are GMT. The time now is 03:43 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.