pighead10
|
  |
| Joined: 03 May 2009 |
| Total Posts: 10341 |
|
|
| 31 Mar 2012 10:12 AM |
| I have a 2d game, and I want something to be able to take something and be able to know what direction it should be going in. Now obviously I can just send something the angle of rotation in its constructor, but then how would it know which way it's supposed to be going in? I don't want it just to go in forwards, backwards, left, right, etc, I need it go be able to go everywhere. |
|
|
| Report Abuse |
|
|
su8
|
  |
| Joined: 06 Mar 2009 |
| Total Posts: 6334 |
|
| |
|
| |
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 31 Mar 2012 10:43 AM |
You shouldn't be using angles and trig at all if possible. Vectors are what you need. And they're really easy to manipulate.
Of course you will still need angles and trig functions in cases where you need angular velocity and stuff like that, but you should convert to vectors whenever possible. |
|
|
| Report Abuse |
|
|
pighead10
|
  |
| Joined: 03 May 2009 |
| Total Posts: 10341 |
|
|
| 31 Mar 2012 10:50 AM |
| So how would convert the stuff to vectors - remember it's not just 4 directions they need to be facing, it's any direction at all. |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 31 Mar 2012 10:56 AM |
Say the position of your thing is vector a. And the position of where it wants to face towards is vector b.
c = b - a is the vector pointing towards b from a
c/|c| is the unit vector facing that direction.
|c| means vector length or magnitude, which is just sqrt(c.x^2 + c.y^2)
Divide it by its length and get a unit vector, a vector with the length of one. |
|
|
| Report Abuse |
|
|
Oysi
|
  |
| Joined: 06 Jul 2009 |
| Total Posts: 9058 |
|
| |
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 31 Mar 2012 11:04 AM |
| I'm not exactly sure what he's asking, but that was my guess. If he wants an object to rotate towards another over time and then start moving, you'll have to use angles, yes. |
|
|
| Report Abuse |
|
|
pighead10
|
  |
| Joined: 03 May 2009 |
| Total Posts: 10341 |
|
|
| 31 Mar 2012 11:11 AM |
What should happen:
Generate object facing in any random direct Object starts moving forwards in the direction it is facing |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 31 Mar 2012 11:15 AM |
| Then you could just create a random vector, I suppose. |
|
|
| Report Abuse |
|
|
|
| 31 Mar 2012 11:16 AM |
| you need an unit vector that points towards that direction k? |
|
|
| Report Abuse |
|
|
Oysi
|
  |
| Joined: 06 Jul 2009 |
| Total Posts: 9058 |
|
| |
|
| |
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 31 Mar 2012 11:22 AM |
| @Oysi, but since he said he wants a random direction you can just generate a random vector and it unit length. There may not be completely random distribution,especially in a rectangular map, but that shouldn't be much of an issue. |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
| |
|
Oysi
|
  |
| Joined: 06 Jul 2009 |
| Total Posts: 9058 |
|
| |
|
Oysi
|
  |
| Joined: 06 Jul 2009 |
| Total Posts: 9058 |
|
| |
|
|
| 31 Mar 2012 11:29 AM |
I usually do this:
angle = math.atan2(pointToFaceY-posY,pointToFaceX-posX) --makes it face pointToFace
Now to make it move in that direction:
velocity = 10 posX = posX + velocity * math.cos(rotation) posY = posY + velocity * math.sin(rotation) -- this should make it move 'velocity' units in the direction it's facing. I may have mixed up sin and cos.
|
|
|
| Report Abuse |
|
|
pighead10
|
  |
| Joined: 03 May 2009 |
| Total Posts: 10341 |
|
|
| 31 Mar 2012 11:44 AM |
Not a random direction, but a specified direction that I can generate easi -
screw it, here is the code I have just to demonstrate:
void Mawbane::e(){ std::stringstream ss; for(int i=1;i<4;i++){ StraightProj* proj = new StraightProj("mawbane_e.png",DIRECTION); ss << i; } } |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 31 Mar 2012 11:46 AM |
Then use vectors?
Gotta implement a nice vector class with overloaded operators if you want it to be simple when doing vector operations. |
|
|
| Report Abuse |
|
|
pighead10
|
  |
| Joined: 03 May 2009 |
| Total Posts: 10341 |
|
|
| 31 Mar 2012 12:02 PM |
| I have an sfml vector class. But, as I said, how would I make the vectors in every direction?> |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 31 Mar 2012 12:05 PM |
| You won't, you said yourself you already know what direction you want them to go. Just convert that direction (whatever units you have it in) to a unit vector. |
|
|
| Report Abuse |
|
|
Oysi
|
  |
| Joined: 06 Jul 2009 |
| Total Posts: 9058 |
|
| |
|
pighead10
|
  |
| Joined: 03 May 2009 |
| Total Posts: 10341 |
|
|
| 31 Mar 2012 01:27 PM |
@Oysi
Sorry, I meant to say I hadn't actually read them because I was typing while waiting for something. |
|
|
| Report Abuse |
|
|
miloguy
|
  |
| Joined: 19 Dec 2009 |
| Total Posts: 7702 |
|
| |
|