Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[Release] Getting vector components by angle and length

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Odecey
Master Cheater
Reputation: 1

Joined: 19 Apr 2007
Posts: 259
Location: Scandinavia

PostPosted: Mon Aug 24, 2009 6:37 am    Post subject: [Release] Getting vector components by angle and length Reply with quote

What I'm presenting here is a function which, as the title states, returns a vector with the properties specified. This is useful for when you want the player to be able to move in more than the four regular directions(N/S/W/E).
What you can use it for:
Say you have a game where you want to give an object complete freedom in which direction it should move. This function could then be used to determine the new position of the object when it moves, given the length it should move, and it's current direction.

N.B:
- The Y- axis is reversed.
- Angle is in degrees.
- Angle 0 goes along the X-axis.

Code:

public Point GetVectorByLengthAndAngle(double length, double angle)
{
    if (length == 0)
        return new Point(0, 0);
    else if (length < 0)
        throw new ArgumentException("Length cannot be negative.");
    if (360< angle || angle < 0)
           throw new ArgumentException("Angle cannot be negative or bigger than 360.");
    double angleBuf = (((90 < angle && angle <=180)||(270 < angle && angle<=360)) ? 90 - (angle % 90) :(angle % 90))/(180/Math.PI) ;
    return new Point((int)(length * Math.Cos(angleBuf) * ((90 < angle && angle <= 270) ? -1 : 1)),(int)(length * Math.Sin(angleBuf) * ((180 <= angle && angle <= 360) ? 1 : -1)));

        }

As always, all feedback is appreciated.

_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren
Back to top
View user's profile Send private message MSN Messenger
Polynomial
Grandmaster Cheater
Reputation: 5

Joined: 17 Feb 2008
Posts: 524
Location: Inside the Intel CET shadow stack

PostPosted: Mon Aug 24, 2009 7:20 am    Post subject: Reply with quote

Good work. The math is solid Smile

Instead of throwing an exception when length is negative, why not simply take its absolute value and flip the angle by 180 degrees? Also, it is reasonably simple to correct for negative angles and angles over 360.

Code:
public Point PolarToVector(double length, double angle)
{
    if (angle >= 360) {
        angle = angle % 360;
    } else if (angle < 0) {
        angle = 360 - (angle % 360);
    }
    if (length == 0) {
        return new Point(0, 0);
    } else if (length < 0) {
        length = -length;
        angle = (angle + 180) % 360;
    }
    double angleBuf = (((90 < angle && angle <=180)||(270 < angle && angle<=360)) ? 90 - (angle % 90) :(angle % 90))/(180/Math.PI) ;
    return new Point((int)(length * Math.Cos(angleBuf) * ((90 < angle && angle <= 270) ? -1 : 1)),(int)(length * Math.Sin(angleBuf) * ((180 <= angle && angle <= 360) ? 1 : -1)));
}


Just a side note: a length and angle from a known line is called a polar coordinate.

_________________
It's not fun unless every exploit mitigation is enabled.
Please do not reply to my posts with LLM-generated slop; I consider it to be an insult to my time.
Back to top
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Mon Aug 24, 2009 7:31 am    Post subject: Reply with quote

Just a question.. I'm not familiar with C#/Java at all.. Is this C# or Java? Seeing that it isn't C/C++..

Also, this could be way more simple.. :) Something like this (C), just add your twists, like inverted Y or sth.:
Code:
Point p;
p.x = (int)( r*cos(a*PI/180) );
p.y = (int)( r*sin(a*PI/180) );
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites