As a thank you to TJ for hosting his
Distances, Triangles, and Trig: For Game Makers class I've decided to release something a little special.
I present to you the Smart Oscillating Object (in this case packaged as an circular saw). To use the SOO you give it minimum and maximum x and y values, a speed for each axis and then place the object in the room
anywhere between.
The object will automatically calculate its speed, position and direction to continue its movement in each axis as an independent sine wave by calculating the inverse sine of the distance between the center of the two extremes and the location you placed the object in the room.In the creation code of the object you'll set the following variables
MinX = The minimum x value
MaxX = The maximum x value
MinY = The minimum y value
MaxY = The maximum y value
TimeX = The number of room steps to travel from MinX to MaxX (ie TimeX = room_speed * 2; //Travel time of 2 seconds)
TimeY = The number of room steps to travel from MinY to MaxY (ie TimeY = room_speed * 2; //Travel time of 2 seconds)
XDir = The direction to start moving in the x axis (XDir = 1; will make the object move to the right to start. XDir = -1; will make the object move to the left to start.)
YDir = The direction to start moving in the x axis (YDir = 1; will make the object move
down to start. XDir = -1; will make the object move
up to start.)
MoveTypeX = The type of movement in the X direction. (Setting the value to Smooth makes the object move in a sine wave and Linear makes the object move at a constant speed);
MoveTypeY = The type of movement in the Y direction. (Setting the value to Smooth makes the object move in a sine wave and Linear makes the object move at a constant speed);
In this example the two saws have been stacked on top of each other at position (480, 256)
One saw has the following creation code
MinX = 416;
MaxX = 544; //The object will bounce between 416 and 544 on the X axis
MinY = 240;
MaxY = 336; //The object will bounce between 336 and 240 in the Y axis
XDir = -1; //The object will start moving left
YDir = 1; //The object will start moving down
TimeX = room_speed * 2 * 0.9; //The object will move from 416 to 544 in slightly less than 2 seconds
TimeY = room_speed * 1.5 * 0.9; //The object will move from 240 to 336 in slightly less than 1.5 seconds
MoveTypeY = Smooth; //The object will move along the Y axis in a smooth sine wave
MoveTypeX = Linear; //The object will move along the X axis at a constant speed.
The second saw's creation code is the same except that the XDir is set to 1 to make it start moving to the right. The movement in the X axis is being done at a constant speed while the movement in the Y axis is being done in a smooth sine wave automatically calculated based on the starting position's relationship to the min and max y values.
All four of these saws share the exact same creation code but because they were each placed in one of the four corners the sine waves for each were calculated based on their individual starting position.
Information about object: objSaw
Sprite: sprSaw
Solid: false
Visible: true
Depth: -999999
Persistent: false
Parent: objPlayerKiller
Children:
Mask:
No Physics Object
Create Event:
execute code:
imgSpeed = 1;
Smooth = 1;
Linear = 2;
MinX = x;
MaxX = x;
MinY = y;
MaxY = y;
StepValX = 0;
StepValY = 0;
TimeX = room_speed * 1;
TimeY = room_speed * 1;
MoveTypeX = Linear;
MoveTypeY = Linear;
XDir = 1;
YDir = 1;
CSX = 0;
CSY = 0;
tag = 0;
trg = 0;
Step Event:
execute code:
if trg == 0 or global.trigger[trg] == 1
{
if MoveTypeX == Linear
{
var MXDist = (MaxX - MinX) / TimeX;
x += MXDist * XDir;
if x >= MaxX and XDir == 1
{
x = MaxX - (x - MaxX);
XDir = -1;
}
if x <= MinX and XDir == -1
{
x = MinX + (MinX - x);
XDir = 1;
}
}
if MoveTypeY == Linear
{
var MYDist = (MaxY - MinY) / TimeY;
y += MYDist * YDir;
if y >= MaxY and YDir == 1
{
y = MaxY - (y - MaxY);
YDir = -1;
}
if y <= MinY and YDir == -1
{
y = MinY + (MinY - y);
YDir = 1;
}
}
if MoveTypeX == Smooth
{
if StepValX == 0
{
CSX = MinX + (MaxX - MinX) / 2;
var nx = (x - CSX)/(MaxX - CSX);
StepValX = arcsin(nx);
if XDir == -1 {StepValX = pi - StepValX;}
if StepValX <= 0 {StepValX += 2 * pi;}
}
StepValX += ((pi * 2)/(TimeX * 2));
x = CSX + sin(StepValX) * ((MaxX - MinX)/2);
}
if MoveTypeY == Smooth
{
if StepValY == 0
{
CSY = MinY + (MaxY - MinY) / 2;
var ny = (y - CSY)/(MaxY - CSY);
StepValY = arcsin(ny);
if YDir == -1 {StepValY = pi - StepValY;}
if StepValY <= 0 {StepValY += 2 * pi;}
}
StepValY += ((pi * 2)/(TimeY * 2));
y = CSY + sin(StepValY) * ((MaxY - MinY)/2);
}
}
Draw Event:
execute code:
image_angle += imgSpeed;
if imgSpeed < 117 {imgSpeed += 0.25;}
draw_sprite_ext(sprite_index, 0, x, y, 1, 1, image_angle, c_white, 1 );