Question Regarding Efficient Code for the Angle Variable

Questions, comments and discussion about the Game Editor development.

Question Regarding Efficient Code for the Angle Variable

Postby bamby1983 » Wed Feb 06, 2013 6:11 am

I wanted to check whether is an efficient way in which to check the value of the "angle" variable in a game. The angle variable doesn't seem to be calculated with respect to the X-axis immediately after the MoveTo function is executed. In fact, at this point, it only shows the angle between the previous direction of motion and the subsequent direction of motion. For example, if the actor is moving at 45 degrees to the X-axis and a mouse down event causes it to move at 90 degrees to the X-axis, the angle variable's value immediately after the mouse down event is 45. However, the value displays correctly after a fraction of a second.

However, setting a timer to consistently check the value of the angle variable to update the actor's animation does not seem to be performance friendly. Take into account that there may well be several actors on a screen and its easy to understand that there may be a significant overhead associated with this kind of approach. Does anyone have any suggestion regarding a more performance friendly approach that can automatically change the actor;s animation when the value of the angle variable changes? The unit will not be travelling along a straight line and will be walking around obstacles without a mouse down event for every change in direction.
bamby1983
 
Posts: 112
Joined: Tue Jul 31, 2012 11:36 pm
Score: 8 Give a positive score

Re: Question Regarding Efficient Code for the Angle Variable

Postby skydereign » Wed Feb 06, 2013 7:04 am

That fraction of a second should never be more than 1 frame, so usually no larger than 1/30th of a second. The only way without using your own variables or calculations, is to use the angle variable directly for movement. Since you are using MoveTo, angle is only updated as the actor is moving. But, since you are the one calling MoveTo, you can use the current location, with the target location, to determine the proper value of angle.
Code: Select all
int ang = 0;
// MoveTo to 400, 200
ang = direction(x, y, 400, 200);
// now you can use ang to set animation
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Question Regarding Efficient Code for the Angle Variable

Postby bamby1983 » Wed Feb 06, 2013 1:51 pm

That would work if my unit was moving in a straight line. However, if the unit changes directions automatically as it finds its way through a maze/series of winding corridors, I would not know at what point to check for the change in angle/direction unless I were to do so at a frame-by-frame level, which would involve a process overhead.
bamby1983
 
Posts: 112
Joined: Tue Jul 31, 2012 11:36 pm
Score: 8 Give a positive score

Re: Question Regarding Efficient Code for the Angle Variable

Postby skydereign » Wed Feb 06, 2013 7:04 pm

Well, there is no other way to make a check happen every frame, without making code that runs every frame. You can simulate it with a timer, but that'd be pointless, as you'll be simulating the same behaviour you would get with draw, and it would just be messier. So, if you want that, you'll have to bite the bullet and use draw actor.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Question Regarding Efficient Code for the Angle Variable

Postby Soullem » Thu Feb 07, 2013 1:55 am

you can set a different variable equal to that and do an if statement to see if they are not equal (or greater than by 90 or whatever) and then change the animation accordingly, that way it only changes animation when the angle has changed accordingly. Then set AnglePrevious equal to angle again.
Current Project:
The Project That needs to be Done -- Pokemon http://game-editor.com/forum/viewtopic.php?f=4&t=12591

Temporarily Abandoned:
Souls of Gustara -- Awesome upgrade blimp game 42%ish
Eggventures of Eggman -- Adventure Game 20%ish
User avatar
Soullem
 
Posts: 105
Joined: Thu Aug 02, 2012 3:12 pm
Score: 5 Give a positive score

Re: Question Regarding Efficient Code for the Angle Variable

Postby bamby1983 » Thu Feb 07, 2013 12:24 pm

Does the draw actor event execute when the actor is rendered on the screen every frame?
bamby1983
 
Posts: 112
Joined: Tue Jul 31, 2012 11:36 pm
Score: 8 Give a positive score

Re: Question Regarding Efficient Code for the Angle Variable

Postby Soullem » Thu Feb 07, 2013 3:34 pm

Yeah, but it reads less code if it doesn't have to change the anim every screen. I don't think you can do this without a draw actor.
Current Project:
The Project That needs to be Done -- Pokemon http://game-editor.com/forum/viewtopic.php?f=4&t=12591

Temporarily Abandoned:
Souls of Gustara -- Awesome upgrade blimp game 42%ish
Eggventures of Eggman -- Adventure Game 20%ish
User avatar
Soullem
 
Posts: 105
Joined: Thu Aug 02, 2012 3:12 pm
Score: 5 Give a positive score

Re: Question Regarding Efficient Code for the Angle Variable

Postby skydereign » Thu Feb 07, 2013 7:01 pm

bamby1983 wrote:Does the draw actor event execute when the actor is rendered on the screen every frame?

Yes, but really it executes all the time. Every frame that the actor exists, and does not have the draw event disabled, it will trigger the draw actor code.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Question Regarding Efficient Code for the Angle Variable

Postby bamby1983 » Thu Feb 07, 2013 7:49 pm

Thanks for the response. I'll use the draw actor as recommended.

Is there a way by which I can make the actor name dynamic so that i can pass it along with a function instead of repeating this code individually for every actor? I tried passing it as a character string called actor_name but I can't use actor_name.angle in the function code.
bamby1983
 
Posts: 112
Joined: Tue Jul 31, 2012 11:36 pm
Score: 8 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron