Jump to content

  • Log In with Google      Sign In   
  • Create Account


[C++] Delayed action of srand() - coordinates for object


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
23 replies to this topic

#21 slicer4ever   Crossbones+   -  Reputation: 3220

Like
0Likes
Like

Posted 22 November 2012 - 09:40 AM

Wrong again. It should be:

if(abs(ballposition.x-balltarget.x)< ballspeed || abs(ballposition.y-balltarget.y)< ballspeed)
	{
		balltarget.x = (rand() * 100) % SCREEN_WIDTH;
		balltarget.y = (rand() * 100) % SCREEN_HEIGHT;
	}

umm....no? (seriously, i'm trying to figure out why you think this would work, am i missing something?)
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

Sponsor:

#22 SuperVGA   Members   -  Reputation: 1118

Like
0Likes
Like

Posted 22 November 2012 - 09:53 AM

I guess if you really intend to use "manhattan units" how about using
   if( ( abs(ballposition.x-balltarget.x) + abs(ballposition.y-balltarget.y) ) < (ballspeed * 2) )
   {
      balltarget.x = rand() % SCREEN_WIDTH;
      balltarget.y = rand() % SCREEN_HEIGHT;
   }

But as it seems to be about a ball, I feel inclined to suggest that you just use an actual velocity vector,
the root of the squares of dX and dY added. (Or don't do the root but work in squared units, if you can/want to.)

Even better, you could use a 2d vector struct
struct 2dmotionvector
{
 float dx;
 float dy;
 float len;
 float angle;
}
To define your velocity, and work with that instead of having only independent x and y speeds to deal with.
-You can use len and angle primarily, and then set dx and dy if you need it in later statements.
(What if you apply spin to the ball, or it bounces off a surface? Way easier with 2d vector math on composite types in my opinion)

Edited by SuperVGA, 22 November 2012 - 10:54 AM.


#23 Trienco   Crossbones+   -  Reputation: 2056

Like
-1Likes
Like

Posted 22 November 2012 - 11:27 PM

Well, L.Spiro has a version that will at least trigger at all (when one coordinate is getting close enough), while the other version looks like in the end the ball will reach one coordinate first, then start jittering around on one axis, move only along the other axis and finally find a new destination. It also means that if one coordinate is reached, the balls will effectively move at only half the speed.

So obviously I second the approach of using vectors, simply because the other code only allows for 45° steps in motion and I doubt that sudden changes of direction while heading towards a destination are intended.
f@dzhttp://festini.device-zero.de

#24 L. Spiro   Crossbones+   -  Reputation: 12325

Like
1Likes
Like

Posted 23 November 2012 - 02:08 AM

umm....no? (seriously, i'm trying to figure out why you think this would work, am i missing something?)

My version is based on the logic in his first post. “If both coordinates move by over 300 units, generate new coordinates, but if only the X or the Y moves by over 300, that is fine.”

For his revision I only didn’t notice that he had switched from > to <, which achieves the correct results.

Comments would go a long way here. Don’t be afraid; they don’t bite. I really couldn’t look too terribly closely because there is not a single comment anywhere, although the code I “corrected” was small enough that I could have paid more attention there.


I still don’t really understand the goal behind the code, and again I would insist on using vectors. Makes everything simple.


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS