Fangames > Programming Questions

Make cherry move in Archimedian spiral with constant speed?

(1/3) > >>

zaphod77:
an archimedian spiral means with each revolution the item has moved a set distance from the spawn point.

I wish two variations.

1) use spawn point as the inside of the spiral, and spiral outwords, with it getting a fixed distance out form the center with each revolution.
2) use spawn point as the outside of the spiral, and spiral inwards. Same thing, but it vanishes when it reaches the center.

it has to do it's magic by adjusting the direction without changing the speed.

how the heck do i do this? 

lawatson:
Two ways really: Trig functions/rotation matrix, or a ratio.

Trig functions method, step event:

angle+=(speed);
radius+=1;
x=centerx+(radius*sin(degtorad(angle)));
y=centery+(radius*cos(degtorad(angle)));

Ratio method (uses speed/direction):

direction=constant/max distance*current distance.

constant is just a value to speed things up, max distance is as far away as it will be, and current distance is point_distance(x,y,centerx,centery).


EDIT: wow why did it quote my message i wanted to edit it stupid ass me lol

infern0man1:
I can only help with the first one, and hopefully it's what you're looking for.



--- Code: (scrOrbit) ---argument0=centerX;
argument1=centerY;
argument2=radius;
argument3=position; //the position of where the cherry will spawn (in degrees)
argument4=radiusSpeed; //the speed of the cherry's movement - moves away from its centerX and centerY
argument5=spinSpeed; //the speed of the cherry's revolution
x=centerX+lengthdir_x(radius+rad,position+pos);
y=centerY+lengthdir_y(radius+rad,position+pos);
rad+=radiusSpeed;
pos+=spinSpeed;


//Note that I used position instead of direction here, as I've had problems with the cherries jumping forward a certain angle when they spawn another object with an incremented direction

--- End code ---


and to spawn them, set an alarm and do the following:



--- Code: (spawningObject) ---for(i=0;i<360;i+=360/num1){ //this is only for if you want more than one cherry spawned at the same time, and set num1 to however many cherries you want spawned at a time
  a=instance_create(room_width/2,room_height/2,objCherry); //using the center of the room as an example
  a.centerX=a.xstart;
  a.centerY=a.ystart;
  a.radius=0;
  a.position=i; //replace i with whatever direction you want if you only want one cherry spawned at a time
  a.radiusSpeed=num2; //set num2 to how fast you want the cherry to move away from it's centerX and centerY
  a.spinSpeed=num3; //set num3 to how fast you want the cherry to revolve
}
alarm[0]=num4; //set num4 to how frequently you want the cherries to spawn

--- End code ---


If you want the direction (or, with the way I made it, the position) of the cherries to change whenever a new one is spawned, add this:



--- Code: (spawningObject) ---~
  a.position=i+dir;
~
dir+=num5; //set num5 to how much you want the cherries' direction (position) to change

--- End code ---


and that's it, hopefully this can be of use for you :3




EDIT: tfw watson replied before me

zaphod77:

--- Quote from: lawatson on January 20, 2016, 04:40:59 PM ---Two ways really: Trig functions/rotation matrix, or a ratio.

Trig functions method, step event:

angle+=(speed);
radius+=1;
x=centerx+(radius*sin(degtorad(angle)));
y=centery+(radius*cos(degtorad(angle)));

--- End quote ---
won't work. my plan is to only adjust direction, and have it move at whatever speed is set.

--- Quote ---Ratio method (uses speed/direction):

direction=constant/max distance*current distance.

constant is just a value to speed things up, max distance is as far away as it will be, and current distance is point_distance(x,y,centerx,centery).

--- End quote ---
Looks like that's for an outward spiral. i tested it, and it seems to work simply using   
direction=spiral_factor*current distance.
where spiral_factor is larger for a tighter spiral.

that's counterclockwise. i can multiply that by negative 1 for clockwise.

but now how do i make it spiral IN instead?

infern0man1:
Actually, thinking about it, you could technically do the inwards spiral with my script, although I'm sure there are much more practical ways to do it; nevertheless:


I would set the radius to have every cherry be spawned outside the view of the room (which you would have to either calculate or do a few experiments to tweak the starting distance), and set the radiusSpeed to be negative so that the cherries move inward; after a period, have an alarm be set off in the cherries that decrease the image_alpha until it's zero, upon which the cherries destroy themselves.

Navigation

[0] Message Index

[#] Next page

Go to full version