Jump to content


Photo

Getting An Object To Orbit Around Another Object


  • Please log in to reply
7 replies to this topic

#1 el_tavish

el_tavish

    GMC Member

  • New Member
  • 277 posts

Posted 07 January 2008 - 04:13 AM

I want an object to circle or orbit around another object. For example, the earth orbits the sun. I'd rather use a coding solution instead of creating a circular path.
  • 0

#2 xshortguy

xshortguy

    GMC Member

  • Global Moderators
  • 4195 posts
  • Version:GM:Studio

Posted 07 January 2008 - 04:24 AM

Place these two pieces of code into the respective events of the orbiting object. Be sure to replace object_being_orbited with the appropriate object or instance id.

Creation Event:
orbit_angle = 0;
orbit_radius = 128;
orbit_speed = 360 / room_speed; //change of angle per step
Step Event
orbit_angle += orbit_speed;
x = object_being_orbited.x + orbit_radius * cos(orbit_angle * pi / 180);
y = object_being_orbited.y - orbit_radius * sin(orbit_angle * pi / 180);

This will cause an object to orbit around another object at a radius of 128 once every second. Change the orbit speed to change frequency. Change radius to change distance being orbited.

Edited by xshortguy, 07 January 2008 - 04:29 AM.

  • 0

#3 Dangerous_Dave

Dangerous_Dave

    GMC Member

  • Global Moderators
  • 9282 posts
  • Version:Unknown

Posted 07 January 2008 - 04:27 AM

Well, you could do it like this:

create:

myspeed=1; //smaller if the radius is bigger
dir=90; //start above point of orbit
radius=100;
orbitx=100;//point to orbit around
orbity=100;

step:

x=orbitx+lengthdir_x(radius,dir);
y=orbity+lengthdir_y(radius,dir);
dir+=myspeed;





And that should work :rambo
  • 0

#4 Foxx

Foxx

    GMC Member

  • New Member
  • 710 posts

Posted 07 January 2008 - 04:32 AM

or
dir_now := point_direction(argument0,argument1,x,y);
dir_new := (dir_now + argument3 + 360)mod(360);
x := lengthdir_x(argument2,dir_new) + argument0;
y := lengthdir_y(argument2,dir_new) + argument1;

argument0 = x //the x of where to orbit
argument1 = y //the y of where to orbit
argument2 = radius
argument3 = speed

this way you only need 1 piece of code and not only that but you can put it in the side bar bit undewr script which im sure you know how to do that and then you only need one script for a whole lotta objects
make sure you change the arguments
  • 0

#5 el_tavish

el_tavish

    GMC Member

  • New Member
  • 277 posts

Posted 07 January 2008 - 04:42 AM

this is kinda embarrasing but up until now I've never had used the same piece of code for more than one object. NEVER! I'm not quite sure how I managed to do this but I have very little knowledge of how I'm supposed to bring the code from the script section into various objects.

So I gotta ask, do I put the second part into the scripts section and the first part into the coding of the object?
  • 0

#6 Foxx

Foxx

    GMC Member

  • New Member
  • 710 posts

Posted 07 January 2008 - 04:46 AM

the second part is saying what the arguments are supposed to be like in the D&D actions you know where the 'execute code' action is well the next one is execute script like one of the scripts from the side bar and thats where you put the arguments in you see what i mean

P.s. its ok to be embrassed i didnt know that or even code 'GML' intil 3 months past have GM
  • 0

#7 el_tavish

el_tavish

    GMC Member

  • New Member
  • 277 posts

Posted 07 January 2008 - 05:18 AM

I've been using registered GM for just over a year :rambo. Despite all the coding and stuff that I've learned I never learned about scripts. hahaha. gotta learn sometime I guess. Thank you
  • 0

#8 Foxx

Foxx

    GMC Member

  • New Member
  • 710 posts

Posted 07 January 2008 - 05:25 AM

thats ok as long as i helped
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users