theattacka

Last Login:
March 22, 2012
Warn:

Rank:
Member
Member Points:
45


User Profile
Follow

Hits: 778
Joined March 12, 2012


Rotate_to_point script
Posted on March 17, 2012 at 10:35

Boringdomness forced me to write some GML

Code
//Usage:
//rotate_to_point(x, y, minSpeed, maxSpeed, steps)
//Made bah attacka

var sturn,targetDir;
sturn = 0;

targetDir = point_direction(x,y,argument0,argument1);
if(image_angle > 360) {
    image_angle -= 360;
}
if(image_angle < 0) {
    image_angle += 360;
}

if(targetDir > image_angle) {

    if(targetDir > image_angle+180){ //1.
        sturn = (targetDir-360-image_angle);
    } else { //2.
        sturn = (targetDir-image_angle);
    }
    
} else {
    
    if(image_angle > targetDir+180) { //3.
        sturn = (360-image_angle+targetDir);
    } else { //4.
        sturn = (targetDir-image_angle);
    }

}

if(abs(image_angle-targetDir) <= argument2 || abs(image_angle-targetDir) >= 360-argument2) {
    image_angle = targetDir;
} else {

//Clamp max speed
    if(sturn > 0 && sturn > argument3*argument4) {
        sturn = argument3*argument4;
    }
    if(sturn < 0 && sturn < -(argument3*argument4)) {
        sturn = -(argument3*argument4);
    }
//Clamp min speed
    if(sturn > 0 && sturn < argument2*argument4) {
        sturn = argument2*argument4
    }
    if(sturn < 0 && sturn > -(argument2*argument4)) {
        sturn = -(argument2*argument4)
    }
    image_angle += sturn/argument4;
}


Here is a example code:
Code
image_angle=rotate_to_point(mouse_x,mouse_y,1,20,10);


Dev - Warning
Dev - After editing rating, make rating display change
Dev - When editing, update "edited" column
Dev - Restore deleted comments
Dev - Display deleted comments if mod (hidden, then with dropdown)
Dev - After deletion or during edit, make rating dropdown appear again
Dev - Stricter rating rules. Prevent user from rating again
Dev - Pages
Dev - Reporting
cool
Posted by svf March 17, 2012 10:56 - 1.2 years ago
| [#01]

Code
sturn = -1*(360-targetDir+image_angle);
should be written as
Code
sturn = (targetDir-360-image_angle);

Otherwise, good code.
Posted by Juju March 17, 2012 11:13 - 1.2 years ago
| [#02]

Well both of the codes works just fine so it acctually doesnt matter which code you use.
Depends if you want to rewrite it or not ^_^
Do whatever you want :)
Posted by theattacka March 17, 2012 11:21 - 1.2 years ago
| [#03]

The multiplication is redundant. There are many things in programming that "work fine" but that does not mean you should be doing it. The ends do not justify the means.
Posted by Juju March 17, 2012 11:54 - 1.2 years ago
| [#04]

If you have mathemical formulas in your code, try to work them out as much as possible. Don't write 240+16 for example, but make it 256. The difference is extremely tiny, but if it's called often, it will improve your fps because it doesn't have to calculate it itself.
Posted by Iasper March 17, 2012 12:03 - 1.2 years ago
| [#05]

MM true dat im gonna update the post/script with your code snip Juju
Posted by theattacka March 17, 2012 13:41 - 1.2 years ago
| [#06]

I have no idea what minSpeed and maxSpeed do, but the following code should do more or less the same:
Code
{
//rotate_to(from, to, delta)  - returns the next angle when moving from 'from' to 'to' with a step size of 'delta'
    var diff;
    diff = ((((argument1-argument0) mod 360)+540) mod 360)-180;
    if (abs(diff) < argument2) return argument1;
    else return argument0 + sign(diff) * argument2;
}

Code
{
//rotate_to_point(x, y, delta)  - your script, but I still don't know what minSpeed and maxSpeed are
image_angle = rotate_to(image_angle, point_direction(x,y,argument0, argument1), argument2);
}

BTW I tested your script and it doesn't even work in some cases.
Posted by sirxemic March 17, 2012 14:38 - 1.2 years ago
| [#07]

Min speed and Max speed Sets the maximum/minimum speed it can rotate
if its below minimum speed it will jump directly to assigned direction
Maximum speed is the highest speed it can rotate at one step
Posted by theattacka March 17, 2012 15:13 - 1.2 years ago
| [#08]

It would be better to have an angle argument rather than x and y co-ords
Posted by Moikle March 17, 2012 18:58 - 1.2 years ago
| [#09]

Quote: Moikle
It would be better to have an angle argument rather than x and y co-ords

Then remake it that way :3?
All you need to change is:
Code
targetDir = point_direction(x,y,argument0,argument1);

to
Code
targetDir = argument0

Then use the script like
Code
rotate_to_point(angle, 0, minSpeed, maxSpeed, steps)
Posted by theattacka March 17, 2012 20:14 - 1.2 years ago
| [#10]

I am already doing something like this in my platformer engine, only it is used to find the minimum and maximum angles a player can move up and down a slope. But I am doing it completely differently to you :P But I am having trouble making a script which finds the most clockwise or anticlockwise angle out of 2 from the same point (e.g
return max(anticlockwise angle from A to B, anticlockwise angle from A to C,)
Posted by Moikle March 17, 2012 22:19 - 1.2 years ago
| [#11]

Quote
The multiplication is redundant. There are many things in game maker that "work fine" but that does not mean you should be doing it.


Fixed, because in almost every other programming language ever made the compiler will optimize out the math for you, enabling you to write it in a potentially less confusing way. And you SHOULD, because then people know how you got "42" and it wasn't just some magic number you got out of a funny book. In game maker, at least use comments to represent the non-worked out equation to make it easier to follow.
Posted by blackhole March 18, 2012 6:19 - 1.2 years ago
| [#12]

theres an easier way to do this, but ill have to find it later
Posted by Alert Games March 18, 2012 14:12 - 1.2 years ago
| [#13]

blackhole - I meant it in a more general sense (i.e. many if statements work just fine but a switch is better in places) but sure, whatever.
Posted by Juju March 18, 2012 15:32 - 1.2 years ago
| [#14]

Recent Activity
 
Active Users (0)