Help wanted Canvas following Actor.

Game Editor comments and discussion.

Help wanted Canvas following Actor.

Postby Jeisenga » Sat Aug 08, 2020 3:32 pm

Help wanted Canvas following Actor.

Hi, I am working on this race game and a label is following the race car. I want this label at the back of the car no matter what angle the car is going. Also the label needs to move smoothly to it’s position. I worked out a jerky solution but I believe there must be a much nicer way and less coding to accomplish an even smoother movement. Look in the draw function of the actor Car_Info_Canvas to see what I mean.

Looking forward to your solutions.
Attachments
Ged.zip
(1.06 MiB) Downloaded 58 times
Jeisenga
 
Posts: 42
Joined: Sun Sep 23, 2018 7:22 am
Score: 2 Give a positive score

Re: Help wanted Canvas following Actor.

Postby lcl » Tue Aug 11, 2020 12:03 pm

Here, I made you an example file where the canvas follows the car nicely. It uses basic trigonometry to calculate the horizontal and vertical offsets from the car.
I didn't implement my solution in your .ged as you had made some odd design choices that would interfere with the system and make it more troublesome for me to implement.

Here's a couple of problems I noticed in your .ged:

  • You've made the view the parent of the car. That is quite an unusual approach as it means you need to move the view in order to move the car. The standard approach would be to move the car and make the view follow it. Easiest way to implement that is to make the car the parent of the view actor, so the other way around from what you have.
  • You had a lot of if statements in your code where you apparently tried to use the logical AND (this: &&) but actually used the bitwise AND (this: &) instead. Those are not the same thing and you should use the logical AND when doing simple if tests like that. More info on the differences and use cases: https://stackoverflow.com/a/49617421

I hope this helps! And if you need further explanation on something, just ask and I'll do my best to explain.
Attachments
fix.zip
(588.79 KiB) Downloaded 60 times
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Help wanted Canvas following Actor.

Postby Jeisenga » Thu Aug 13, 2020 3:55 pm

Thanks LCL. Point deserved.
I knew there was a better way but trigonometry is not my strong suit. I am sure I would never had figured this out. I did manage to convert your code to a car with moving background and a adjustable diameter. There is no doubt in my mind that my coding is messy but all I had was a little basic coding. I had to figure it out in the GE forum.
I had no idea what the difference was between & and &&, for sure I will study the documentation you provided. I also changed the view following the Car.
Can you explain why the variable distFromCar needs a real (double) and why a float doesn’t work?
Also another thing I can’t figure out, I want the mouse pointer to send to a certain coordinate so it’s out of the way. But xmouse and ymouse are read only so that doesn’t work. I’m sure it’s possible but maybe not in GE. GE self uses it in Full Screen mode. If you press Game mode the mouse jumps to the middle of the screen and I want it to one of the edges.

Also this is bugging me for a while: strcat(&Text_Result, “Why the &");
If I do a strcat it always puts a & in there and I have to delete it from the script for it to work. Is that a bug or can this & have a purpose?

Again Thanks for your help.
Jeisenga
 
Posts: 42
Joined: Sun Sep 23, 2018 7:22 am
Score: 2 Give a positive score

Re: Help wanted Canvas following Actor.

Postby lcl » Fri Aug 14, 2020 12:12 am

Glad you got it sorted out.

In regards to the trigonometry, the math used here is actually not too complicated. Check out the definition of a Unit Circle (mathisfun.com). You see that sin() and cos() can be used to calculate the factors for the vertical and horizontal displacements of a point corresponding to a given angle on a unit circle. In your case, the angle is the angle of the car's movement. Multiplying the canvas' desired distance from the car (distFromCar) by the values given by sin() and cos() gives the amounts of vertical and horizontal displacement needed. :)

Jeisenga wrote:Can you explain why the variable distFromCar needs a real (double) and why a float doesn’t work?

The variable distFromCar could as well be a float, I just used the GE's built-in variable creation system for simplicity, as there was nothing else that needed to go to Global Code, which is the only place where one can define global float variables. It could actually as well be an integer, since the result of the calculation is going to be the actor's position on the screen and that is always measured in pixels (in GE). It just makes sense for me to use a floating-point variable for trigonometry.

Oh, and the variable has to be a global variable because I wanted to be able to set it's value just by moving the canvas in the editor view and that meant initializing its value in the Create Actor event of the actor. And only global variables can be used across multiple different scripts, those floats defined in the Draw Actor event only exist in that script. But you could just as well have it be a local variable initialized with the desired value in the Draw Actor script just like the other variables, that would just mean that to adjust the distance you'd have to adjust the value in the script.

Sorry, I didn't quite understand what you meant with the question about the float, so I tried to answer all the different ways you could have meant that question.

Jeisenga wrote:Also another thing I can’t figure out, I want the mouse pointer to send to a certain coordinate so it’s out of the way. But xmouse and ymouse are read only so that doesn’t work. I’m sure it’s possible but maybe not in GE. GE self uses it in Full Screen mode. If you press Game mode the mouse jumps to the middle of the screen and I want it to one of the edges.

If you'd like to hide the cursor all together, there's an option for that in the Game Properties menu. Another solution is to hide the cursor and make your own cursor. It can be accomplished either with FollowMouse() or by using xmouse and ymouse to read the mouse position and move the custom cursor there. There's a couple more things to consider when making a custom cursor, and I'll tell more about those should you decide to go that way.

Jeisenga wrote:Also this is bugging me for a while: strcat(&Text_Result, “Why the &");
If I do a strcat it always puts a & in there and I have to delete it from the script for it to work. Is that a bug or can this & have a purpose?

It putting it there always automatically is a bug. The & in front of a variable like that is an address of -operator - it gives a pointer to the memory address where the variable resides. The ampersand becomes a problem in your example case because strings in C are actually just pointers to the first element of a char array. Using the address of operator for a pointer gives a pointer to a pointer, but that is not what strcpy(), strcat() etc. expect, hence the error. It has it's uses in other cases.
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Help wanted Canvas following Actor.

Postby Jeisenga » Fri Aug 14, 2020 7:53 am

I downloaded the Unit Circle pages and will study them.
I know about hiding the mouse and making your own custom mouse. But what I would like is the following. You got a Start button in your game in the middle of the screen. Push it with the mouse and the mouse should jump to the top or bottom of the screen. GE does it when you have the full screen modes and you press Game Mode the mouse jumps to the center of the screen. Even with no program at all just open a new game make it full screen in Config and press Game Mode. Allthough its at the top of your screen, the mouse pointer will jump to the center. But I think programming that in GE is not possible. Hiding a custom mouse is possible but I need to have the mouse appear on a certain point if the player reaches another stage in the game. Hope I made myself clear.

I understand int and float but I have not used double in anyway. I would like to understand where it is usefull so I could make better use of all the variables.

One more thing, do you know if it is possible to set the height and width of a canvas and/or filled region? Now all I can do is drag it and check it with height and width. Something like Set Height / Width would be helpfull.
Jeisenga
 
Posts: 42
Joined: Sun Sep 23, 2018 7:22 am
Score: 2 Give a positive score

Re: Help wanted Canvas following Actor.

Postby lcl » Sun Aug 16, 2020 10:14 pm

Jeisenga wrote:But what I would like is the following. You got a Start button in your game in the middle of the screen. Push it with the mouse and the mouse should jump to the top or bottom of the screen. GE does it when you have the full screen modes and you press Game Mode the mouse jumps to the center of the screen. Even with no program at all just open a new game make it full screen in Config and press Game Mode. Allthough its at the top of your screen, the mouse pointer will jump to the center. But I think programming that in GE is not possible. Hiding a custom mouse is possible but I need to have the mouse appear on a certain point if the player reaches another stage in the game. Hope I made myself clear.

Oh, yeah, sorry, I kind of forgot to say that, just like you thought, it's not possible to adjust the position of the actual mouse pointer on the screen in GE.

Jeisenga wrote:I understand int and float but I have not used double in anyway. I would like to understand where it is usefull so I could make better use of all the variables.

Double is a variable type used for real numbers (floating-point numbers), just like float. The difference is that double has double the size of float, so it can store more accurate floating-point numbers.

Jeisenga wrote:One more thing, do you know if it is possible to set the height and width of a canvas and/or filled region? Now all I can do is drag it and check it with height and width. Something like Set Height / Width would be helpfull.

Unfortunately, there is no such functionality in GE. If you want to resize a region actor to some specific width and height, the default grid of GE's "stage" is 20 x 20 pixels, so you can use the grid to help you know how large you need to drag the actor. And for more precise resizing you can zoom in in the editor by using "," and "." or "+" and "-".
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest