Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / AngleSprite

Author
Message
Behdadsoft
15
Years of Service
User Offline
Joined: 7th Apr 2009
Location: Tehran-Iran
Posted: 26th Dec 2013 15:18 Edited at: 26th Dec 2013 15:20
Hi.

I want rotate my Sprite with Mouse. I wrote this code but want rotate my Sprite between (360-300 and 360-45) or (300-45) but I don't Know how can write it.

I attached a Picture.

Attachments

Login to view attachments
Lucas Tiridath
AGK Developer
15
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 26th Dec 2013 16:42
Hi Behdadsoft. I'm not sure I really understand what you are trying to achieve here. I see that you are using the y coordinate of the mouse as the angle. If you want the mouse at the top of the screen to be 300 degrees and the bottom to be 45, then you could do something like this.



If that's not what you meant, could you give us some more details of what you're trying to do?

Behdadsoft
15
Years of Service
User Offline
Joined: 7th Apr 2009
Location: Tehran-Iran
Posted: 26th Dec 2013 17:06
Thanks Lucas Tiridath.

Your code work very well. but I have 2 question:

1- how can use Initializing for first run in AppGameKit?
2- could you say why divide (ScreenToWorldY(PointerY#) / GetVirtualHeight() and Multiplication it at 105?


Quote: "
could you give us some more details of what you're trying to do?
"


Yes. I want make a game like cannonbal game. but I don't know my work is correct or no. and need more detail for shoot and collision in AGK.
Lucas Tiridath
AGK Developer
15
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 26th Dec 2013 19:11
Quote: "how can use Initializing for first run in AppGameKit?"

If you're only targeting desktop platforms (and it looks like you are as you are using raw mouse commands) then you can use something like this.

This will place the mouse in the centre of the screen by default, meaning that the sprite will always start off upright.

Quote: "could you say why divide (ScreenToWorldY(PointerY#) / GetVirtualHeight() and Multiplication it at 105?"

Of course, it's always great when people want to understand why things work!

OK so let's break it down. PointerY# has already been clamped to be between 0 and the virtual screen height with these lines.

Therefore PointerY# / GetVirtualHeight() is guaranteed to return a value between 0 and 1. In AppGameKit, angles wrap around so 45 degrees is the same as 405 degrees (360 + 45). As you want values between 300 and 45 degrees, we know that the range of degrees is 105 (405 - 300). So we multiply our value between 0 and 1 by 105 to give us a value between 0 and 105 (e.g. 1 * 105 = 105, 0.5 * 105 = 52.5 etc.). Finally, we add our on our basic rotation (300) and we get a rotation between the two values you wanted based on the position of the mouse.

Quote: "Yes. I want make a game like cannonbal game. but I don't know my work is correct or no."

Ah in that case, maybe you would like to consider making the cannon point towards the mouse instead? This can be achieved using ATan2 as follows.

ATan2 finds the angle of a vector. [Sprite position] - [mouse position] gives us the direction vector from the mouse to the sprite, and ATan2 gives us the angle of this vector. We subtract 90 simply because the angle returned by ATan2 considers 0 rotation to be vertical, not to the right as in your diagram. Having found the angle, we just need to clamp it into the range you want.

I hope that helps.

Behdadsoft
15
Years of Service
User Offline
Joined: 7th Apr 2009
Location: Tehran-Iran
Posted: 26th Dec 2013 20:03
Thanks Lucas Tiridath, Your Explanation is very good.

Really this forum is the best for Support Products.

I want release Bullet when left Mouse button Down according my gun direction. for this work I Use this code but I don't Know how can release it according gun direction.



I'm beginner AppGameKit engine. and I want learn it Normative at begin.
Have you a suggestion for me?

Thanks A Lot
Lucas Tiridath
AGK Developer
15
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 26th Dec 2013 21:40
Quote: "I want release Bullet when left Mouse button Down according my gun direction."

So for this, we need our vector maths again. SetSpritePhysicsVelocity takes a vector as its second and third parameters. This vector represents both the direction and the speed that the sprite will travel. Getting the direction is almost the same as before. [Mouse position] - [sprite position] gives us the vector from the sprite to the mouse. Therefore we can use this directly to give the bullet the correct direction like so.

Remember that for this to work, you'll need to have switched physics on for the sprite first, like this.


The thing about this is that although the direction will be right, the speed will depend on how far the mouse is from the cannon. If that's what you want, that's fine. If not however, we have to do a little more work. First, we have to normalise the vector. This is done by dividing its components (x & y) by its length. To calculate its length, we use Pythagoras's theorem. Once the vector is normalised, this means that its length is 1. We can then multiply it up by the desired velocity to make it go at the correct speed. The code looks like this.


Quote: "I'm beginner AppGameKit engine. and I want learn it Normative at begin."

Well if you're looking for a good introduction to both AppGameKit and programming, you might like to consider Hands on AGK. I don't own it but his books on DarkBASIC Pro were great. If you're into video tutorials, Daniel Foreman has produced a whole load of them which you might like to check out here. For online written resources, Baxslash did a few tutorials which you can find here. Hope those help!

Behdadsoft
15
Years of Service
User Offline
Joined: 7th Apr 2009
Location: Tehran-Iran
Posted: 27th Dec 2013 06:09 Edited at: 27th Dec 2013 07:51
Thanks. but there are some problem in this script:
1- if I SetSpritePhysicsOn(2, 2) before release bullet, it Fall.
2- when I release the bullet, if change Cannon position also the bullet change the position at travel. and left mouse click still work.

My Problem at understand AppGameKit is I don't Know where should use divide or multiply, ... operators. I know VB Programming language Syntax but I can't Understand where should use operators. I work with other GUI game engines and wrote Hundreds line Script but think AppGameKit is no good idea for me. because for use it should to be a professional programmer. and there is no good tutorials and samples for better understand.
Lucas Tiridath
AGK Developer
15
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 27th Dec 2013 08:54
Quote: "if I SetSpritePhysicsOn(2, 2) before release bullet, it Fall."

Yes, this is because each frame after you set physics on, gravity will be applied to the bullet, causing it to fall unless it is resting on something. An easy way to deal with this is to set physics on only when the user clicks the mouse.

Quote: "when I release the bullet, if change Cannon position also the bullet change the position at travel."

To prevent the cannon from firing the bullet again, you could always just add a boolean value to check if the cannon has already been fired. So for example, you could initialise it as:

And then your fire event code could become:


Quote: "for use it should to be a professional programmer. and there is no good tutorials and samples for better understand."

Learning to program is hard, there's no doubt about that. Having said that, there are resources out there such as the ones I linked to above, and I would say that most of the people here, myself included, are not really professional programmers. If you need more examples, Tone Dialer and others have written useful examples for almost all of the commands in the AGK documentation here. And of course you can always post on these forums when you get stuck.

In the end, you should pick your development tools based on what you want to achieve. If your primary interest is in game design, then maybe you would be best off working in the GUI engines you're already experienced with. However if you are interested in developing your programming skills further, I would urge you to stick with AGK. It's a great learning tool and can produce some very impressive results. Anyhow whatever you choose, you're always welcome to pose your AppGameKit questions here.

Behdadsoft
15
Years of Service
User Offline
Joined: 7th Apr 2009
Location: Tehran-Iran
Posted: 27th Dec 2013 13:34 Edited at: 27th Dec 2013 14:30
Quote: "
you're always welcome to pose your AppGameKit questions here.
"


Thanks for your help.

I need Explanation about some part for good understood.if Possible.
1- in (ScreenToWorldY(PointerY#) / GetVirtualHeight()), how can have a value between 0 and 1?
can your more explanation about ScreenToWorldY() function? I can't find good Explanation in Document.

2- could you explanation about what is vector? (Of course I work with vector3 in some game engines but I think Now we work with vector2) and this codes?



mag# = Sqrt((x# ^ 2) + (y# ^ 2)) wrote according a^2+b^2=c pythagoras's theorem right? what is sqrt work?

Thanks
Lucas Tiridath
AGK Developer
15
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 27th Dec 2013 14:53 Edited at: 27th Dec 2013 14:53
Quote: "in (ScreenToWorldY(PointerY#) / GetVirtualHeight()), how can have a value between 0 and 1?"

ScreenToWorldY converts from screen space, which is measured in pixels, into world space, which is measured in arbitrary units. So for example, imagine I create a window 800x600 pixels in size. Then, I run the command SetVirtualResolution(100, 100). This means that in world units, the screen is now 100x100. Now imagine that I want to place a sprite where my mouse is. If I use

the result will now be wrong, because GetPointerX/Y will return the mouse position in screen coordinates, but SetSpritePosition expects them to be in world coordinates. So if my mouse is in the centre of the screen, the code will run as SetSpritePosition(1, 400, 300). As the world is defined as 100x100 units though, the sprite will be placed way off the bottom right of the screen. Therefore to get this to work, I need to convert from screen space to world space like so.

This is essentially the same as writing

but ScreenToWorldX/Y is clearly more concise.

As for why it is between 0 and 1, let's continue with our example from above, with a screen of 800x600 and a virtual screen size of 100x100. Let's examine the extreme cases first. If PointerY# is the highest value it can be (600), ScreenToWorldY will convert it to the highest virtual position possible (100). GetVirtualHeight returns the virtual height (100) and so the result will be 100 / 100 = 1. Now let's go to the other extreme. If PointerY# is the lowest possible value (0), then ScreenToWorldY will convert it to the lowest possible virtual position (0). The virtual height is still the same and so we have 0 / 100 = 0. As this equation is linear, all the values will fall between those two points. So if my mouse is in the middle of the screen again, PointerY# = 300. ScreenToWorldY(300) = 50 and so we have 50 / 100 = 0.5.

Quote: "could you explanation about what is vector?"

Yes, in this context, we are using 2D vectors. Vectors are a numeric representation of a direction and a magnitude. So you can think of them as an arrow drawn on a piece of graph paper. The direction of the arrow is the direction of the vector. The length of the arrow represents its size, which in the context of physics can be used to represent velocity.

Quote: "mag# = Sqrt((x# ^ 2) + (y# ^ 2)) wrote according a^2+b^2=c pythagoras's theorem right? what is sqrt work?"

In fact, Pythagoras's theorem is a^2 + b^2 = c^2. Therefore if we want to find c (not c^2) we have to take the square root (Sqrt) of the result.

Behdadsoft
15
Years of Service
User Offline
Joined: 7th Apr 2009
Location: Tehran-Iran
Posted: 27th Dec 2013 15:55 Edited at: 27th Dec 2013 17:03
Thanks.
Really Your Explanations is very better than AppGameKit Document.
and Excuse me for my Questions.

if we have SetVirtualResolution() or SetDisplayaspect() should convert screen coordinate to ScreenToWorld right?

Quote: "
we have to take the square root (Sqrt) of the result.
"


I know. I want know why should take square root?

and this is my code could you fix some problems?



the Problems are:
1- at first run the cannon angle is 300 I want fix it to 0. I can't use SetRawMousePosition(GetVirtualWidth() / 2, GetVirtualHeight() / 2), because I want make this games for all platforms.

2- when mouse pointer is top left corner and press left key, the bullet shoot to Behind the cannon.

Thanks
Lucas Tiridath
AGK Developer
15
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 27th Dec 2013 17:01
Quote: "if we have SetVirtualResolution() or SetDisplayaspect() should convert screen coordinate to ScreenToWorld right?"

In fact, I believe that the virtual resolution defaults to 100x100 and not the actual screen resolution, and so you should always use ScreenToWorld unless you are absolutely sure that:
1. Your virtual resolution is the same as the device's actual resolution
2. You've not use zooming
3. You've not moved the view port

Quote: "I know. I want know why should take square root?"

So in Pythagoras's theorem, a^2 + b^2 = c^2 where a and b are the x and y components and c is the length. As you can see, if we plug x and y into the equation and calculate x^2 + y^2, what we get is length^2, not length. The square root is the inverse operation to squaring a value (^2). To put that another way c = Sqrt(c^2). Therefore once we have done x^2 + y^2, we need to convert length^2 back to length. This conversion is done using Sqrt. I hope that makes more sense.

Quote: "and this is my code could you fix some problems?"

I just tried your code and it seems to be running OK. What specific issues are you having with it?

Behdadsoft
15
Years of Service
User Offline
Joined: 7th Apr 2009
Location: Tehran-Iran
Posted: 27th Dec 2013 17:09 Edited at: 27th Dec 2013 17:23
Thanks, Today I learned many things from you.

the Problems are:
1- at first run the cannon angle is 300, and I want fix it to 0. I can't use SetRawMousePosition(GetVirtualWidth() / 2, GetVirtualHeight() / 2), because I want make this games for all platforms.

2- when mouse pointer is top left corner or bottom left corner and press left mouse, the bullet shoot to Behind the cannon.and when pointer is up or under cannon and press Left mouse, shoot the bullet up or under cannon.
Lucas Tiridath
AGK Developer
15
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 27th Dec 2013 19:39 Edited at: 27th Dec 2013 19:40
Quote: "at first run the cannon angle is 300, and I want fix it to 0."

Well how you decide to do that is really an design decision. One way would be to only point the cannon at the pointer when it's pressed. You could do this by wrapping all of the angle code in an if statement to check that the pointer is currently down. Of course in this setup, you would then want to have the mouse/finger being released as the trigger event to fire the cannon rather than the pressed event. This would look something like this.


Quote: "when mouse pointer is top left corner or bottom left corner and press left mouse, the bullet shoot to Behind the cannon"

To clamp the velocity vector to the rotation of the cannon, we are going to have to do some conversions between vectors and angles. One approach would be to take the angle of the cannon as our angle, as we know that to already be clamped in the correct range. We can find this with the GetSpriteAngle command. Once we have that, it's just a matter of finding our velocity vector from the angle. One solution would be as follows.

In this solution, we first decide on our velocity and capture the current rotation of the cannon. We then imagine a vector pointer to the right (the default direction of the cannon). As it moves exactly to the right, and its total length is velocity#, we know that its x component must be velocity# and its y component must be 0. With this vector constructed, we then need to rotate it by the angle of rotation of the cannon. The formula for doing this is

For our vector, as we've already discovered, x = velocity# and y = 0. Therefore we can substitute in the values like so.

Of course, we can see immediately here that 0.0 * Sin(angle#) and 0.0 * Cos(angle#) will both equal 0, and so can simply be removed from the equation, leaving us with


Behdadsoft
15
Years of Service
User Offline
Joined: 7th Apr 2009
Location: Tehran-Iran
Posted: 28th Dec 2013 18:23 Edited at: 28th Dec 2013 18:25
Thanks Lucas.

I Small change the SpriteAngel for fix Mouse Drag and Drop.


I want Make a Slingshot Like Angry birds but I don't Know how can make it. Is this a Sprite?
Lucas Tiridath
AGK Developer
15
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 29th Dec 2013 09:33
Quote: "I want Make a Slingshot Like Angry birds but I don't Know how can make it. Is this a Sprite?"

Well I guess you could do this by stretching and rotating the slingshot sprite, right? So you could use SetSpriteOffset to make sure that rotation happened around the point where the slingshot attached to the catapult thing, SetSpriteAngle to change the slingshot's angle relative to the mouse, and SetSpriteScale to stretch the sprite the correct distance to the mouse.

Behdadsoft
15
Years of Service
User Offline
Joined: 7th Apr 2009
Location: Tehran-Iran
Posted: 29th Dec 2013 10:48
Quote: "
Well I guess you could do this by stretching and rotating the slingshot sprite, right?
"


I try to make it according your Explanation. but if there is any Problem I ask you.

Thanks.
Behdadsoft
15
Years of Service
User Offline
Joined: 7th Apr 2009
Location: Tehran-Iran
Posted: 30th Dec 2013 19:37 Edited at: 30th Dec 2013 21:10
Hi Lucas and Happy Christmas.

I try but Couldn't Make it.
This is my script:

Lucas Tiridath
AGK Developer
15
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 30th Dec 2013 22:27
Quote: "Hi Lucas and Happy Christmas."

Thank you! And yourself .

Could you provide the media you are using (Cache.png & slingshot.png) so I can see what exactly is happening?

Behdadsoft
15
Years of Service
User Offline
Joined: 7th Apr 2009
Location: Tehran-Iran
Posted: 30th Dec 2013 22:45
Quote: "
Thank you! And yourself .
"


Thanks

Quote: "
Could you provide the media you are using (Cache.png & slingshot.png) so I can see what exactly is happening?
"


Of Course.

Attachments

Login to view attachments
Lucas Tiridath
AGK Developer
15
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 31st Dec 2013 10:23 Edited at: 31st Dec 2013 10:24
I've just had a look and getting these kinds of things right always takes quite a lot of tweaking and refinement. You may decide in the end to use two sprites for the sling instead of one, so that you can ensure that they both appear to remain attached to the slingshot. Anyhow I've put together a very rough implementation to get you started.

This works as follows. First, we set the offset of the cache sprite so that it is in the top right hand corner (this will act as the pivot point - you could experiment with bringing this point a little further into the sprite so that it is between the two ends of the cache). Then, in the loop, we calculate the vector from the mouse to the sling with these lines.

This direction is the direction we want the sling to point towards. We then convert our vector to an angle of rotation as before.

Finally, we set the sprite's rotation. If the angle from the mouse to the sling is outside of the permitted range, we clamp the rotation and do not change the scaling.

If the angle is within the valid range, then we set the sprite to the angle calculated earlier and calculate the scaling. The scaling is done entirely to the width, simply by setting the width to the same size and the length of the vector.

I found that with the sprites you're using, adding 20 to the rotation makes it appear that the sling is being pulled by the mouse, but this offset will change if you change the sprite.

I hope that helps!

Behdadsoft
15
Years of Service
User Offline
Joined: 7th Apr 2009
Location: Tehran-Iran
Posted: 3rd Jan 2014 22:28
Thanks Lucas. I try to solve some Problem. if couldn't I ask you for help me.

Login to post a reply

Server time is: 2024-05-03 00:43:32
Your offset time is: 2024-05-03 00:43:32