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 / Rotating 4 points around same location

Author
Message
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 27th Jan 2016 22:58
I have been trying to work out an algorithm that would rotate 4 points around a location

1.........2
............
......o.....
.............
3.........4

Above is a picture that might help explain, points 1,2,3,4 have to rotate around the o position
so the x and y centre of each is the same
I know a formula can be calculated with something similar to this x=radius*cos(Degrees)+xcentre
y=radius*sin(Degrees)+ycentre but if I do that only one point works and have no idea how to make it that all four
would rotate around the o location, having same radius and xcentre and ycentre theres more maths I need to
consider can someone please help

This routine could be useful when you want to construct your own collision routine and have various effects depending on which point
fubar
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 28th Jan 2016 00:18
The declarations



the rotation I'm trying


The above code rotates around the point but its radius gets smaller but its close to what I want
fubar
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 28th Jan 2016 02:59
maybe you could use setspriteangle() to rotate the sprites (if all the sprites have the same offset)
if you then wanted a particular point you could use getworldxfromsprite() to convert a pixel position in the sprite to world co-ordinates

Sign up for NaGaCreMo!
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 28th Jan 2016 04:08
I want to move the sprites around a given location but they all have different offsets and must rotate as if they were one sprite
fubar
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 28th Jan 2016 06:29
It may be possible with joints if I can somehow join all 4 points to o


1.........2
............
......o.....
.............
3.........4

and just rotate o having the points 1,2,3,4 rotate at the same as if theyre joined but I have no idea how to do this
fubar
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 28th Jan 2016 08:32 Edited at: 28th Jan 2016 08:34
this will rotate 5 points around a central point (The center of the screen). you will need to adjust the calculations to the offset of your sprites translated to screen co-ordinates

Sign up for NaGaCreMo!
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 28th Jan 2016 09:05
You can use SetSpriteOffset and SetSpriteAngle to achieve what you want. Example:

V2 T1 (Mostly)
Phone Tap!
Uzmadesign
BatVink
Moderator
20
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 28th Jan 2016 09:09 Edited at: 28th Jan 2016 09:11
For these things, I find that cheating is a good approach.

Create a physics-based connection of 5 sprites. Rotate the central one. Get the positions of the other 4.



The downside of a weld joint is that the sprites have to rotate, they cannot stay horizontal while they orbit.

Quote: "You can use SetSpriteOffset and SetSpriteAngle to achieve what you want"

This is possible, but this also has a downside. You can't retrieve the screen location of the sprite if you need it. You can only get the point around which you offset it. I wrestled with this for a long time, but couldn't get around the problem.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 28th Jan 2016 10:13
Quote: " You can't retrieve the screen location of the sprite if you need it. You can only get the point around which you offset it."
I do love a challenge

Wow! Looks like GetSpriteX/Y doesn't take into account angles! That's probably a bug,

...but it IS possible to work around using GetWorldX/YfromSprite. e.g:



This would be more efficient than weld joints, allowing for thousands of sprites to be rotated at once.
V2 T1 (Mostly)
Phone Tap!
Uzmadesign
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 28th Jan 2016 11:23
here's the thing with incrementing the angle: the sprites farther away from the center will move faster than the sprites closer to the center
i think fubarpk wants them to move as if they were placed on an old record player
i think mr vinks idea is the go

Sign up for NaGaCreMo!
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 28th Jan 2016 11:35
Thanks that's exactly what I want
fubar
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 28th Jan 2016 12:02
Both methods give the same results, but whatever works best for you! It's good to have options right?
V2 T1 (Mostly)
Phone Tap!
Uzmadesign
BatVink
Moderator
20
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 28th Jan 2016 13:06
Quote: "but it IS possible to work around using GetWorldX/YfromSprite."


That would have saved me hours! I ended up using complex (in my world) trigonometry to fix my problem.
Thanks for the tip.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 28th Jan 2016 13:28
I knew you'd like it. It solves a few problems for a project I currently have on hold too!
V2 T1 (Mostly)
Phone Tap!
Uzmadesign
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 28th Jan 2016 20:33 Edited at: 28th Jan 2016 20:37
nothing

Sign up for NaGaCreMo!
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 28th Jan 2016 22:41
I love all these options and this post has helped me understand allot
Quote: "Learning is a good thing"

I'm presently having good success from BlinkOk trigonometrys solution
This was needed as I need to know the real world locations of each sprite

I plan to use it in al algorithm I'm working on that calculates the colour under the four corners of a sprite for my own collision control
it might sound like a bad way of doing it but I don't know how else you can know the corner positions of a rotated sprite (in old days youd just change the sprites hotspot)

And reason I'm doing collision control in this way is because the sprites surrounding are odd shapes where you are frequently over the transparent part and the speedy
collision controls that come with AppGameKit don't seem to work with transparency well for collision.
Quote: " the good old days when it was only a collision when it didn't collide with transparency"


But I loved all the help been trying so many things was going to put it in the to do list lol Some of my best games have gone there never to be completed and the funny part they worked really well just a little itty bitty issue
fubar
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 29th Jan 2016 02:07
For those interested here is the source on what ive been working on so far



the above changes has helped considerably
the collision part still needs some work
as it seems to send it backwards more than forwards lol but il get it. I used the same technique at first when I tried previous returning to the old position but it always overlapped the sprites

fubar
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 29th Jan 2016 02:17
If I just use the front bumper spots it works as I want it to


like a bumper car
fubar
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 29th Jan 2016 03:50
just thought ide upload and show the collision control in action for those interested still could do with some work



recapping I cant use the fast built in collision control cos the sprites already overlap just not the transparency side of things. The sprites are very irregular shapes aswell
so I need to work on this routine I beleive
fubar
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 29th Jan 2016 08:18
Looking good. Have you thought about using the 2D physics commands for control and collision?
V2 T1 (Mostly)
Phone Tap!
Uzmadesign
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 29th Jan 2016 08:55
I also have been experimenting with joints with the below piece of code


Which would be the better way to go I think if only the sprites would stay attached during collison because in effect they would be buildings so they shouldn't move but as the cars stationary
and the background moves not sure how to make all objects in a joint to respond to a collision. Been reading up on collisions and looking on the forum with some success

I'm not sure which technique il adopt yet but I need to perfect this collision lol
Quote: "theres no such think as too much coffee"
fubar
BatVink
Moderator
20
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 29th Jan 2016 10:46
Looking good
With the 2D physics, you can create your own collision shape. In short, you draw a shape around the collidable part of the sprite by setting a series of x,y points. You set them in a clockwise rotation, and the shape must be convex.
If your shape is concave or more complex, you can use multiple collision shapes to define it.
For your car, you can probably create a fairly rectangular collision shape with the corners cut off.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 29th Jan 2016 22:14 Edited at: 29th Jan 2016 22:18
i think there's a utility around the board somewhere that allows you to draw the collision shape visually.
...
found it!
https://forum.thegamecreators.com/thread/212975#msg2544432

Sign up for NaGaCreMo!
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 30th Jan 2016 01:42
That application sounds great blinkOk I definitely will give it a try
fubar
Phaelax
DBPro Master
20
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 30th Jan 2016 20:45 Edited at: 30th Jan 2016 20:46
Ok, here's my mathematical solution without relying on sprites. As long as you know something about the rectangle, you can figure out the info you need.

Some things to point out:
1. All four corners are the same distance from the center of the rectangle
2. All four corners will have the same angle
3. You need to know the width and height of the rectangle and origin position (the center) This can be calculated easily given a set of points or even with a sprite origin in the top-left corner.


Look at the attached image and note how I broke it up into triangles. All those triangles are congruent.

Step 1 is to calculate the angle of the corner. This is the offset angle.
Step 2 is to calculate the distance of the corner from the origin (center)

These values will never change unless the shape of the rectangle changes. So calculate them once and store them in variables somewhere.

Ok, now if you can, picture an arc going from 0 degrees to 180. A half circle. In the image this would be to the two end points of the horizontal green line. In a square, points A and B would simply be 90 degrees apart, but a rectangle stretches that out anywhere from 0 to 180. (0 and 180 being lines actually) We calculated one angle already, this gives us A. How far do we move along this arc to get to B? This is step 3, calculate that "leftover" angle. From A, we move over by this leftover angle twice to get to point B. C and D are simple to find, as they are 180 degrees from A and B.


I'd say more than half this code is merely for drawing the demonstration of what's going on.


"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

Attachments

Login to view attachments
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 31st Jan 2016 02:03
Wow Phaelax I'm definitely going to have a play with that sometime
not so sure my maths is up to use it to the best tho (even if I did pass discrete maths once lol but only just)

I was just thinking a 275 degree bounce factor if there was a collision (depending on the positioning of the objects I think this might work
so I was going to call the a modified version of the below code once I detected collision which moves all of the background
haven't done much coding last couple of days was my birthday on the 30th so I desired a drinking day lol
Ill try that and see if that is a disaster or does a dodgem car effect


[/quote]Math is a friend if you understand it[quote=]
fubar
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 31st Jan 2016 03:12
the below code moves background in the direction of 90 degrees to the direction it was heading before it collided


collisionflag =1 topleft collision
collisionflag =2 topright collision
fubar
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 31st Jan 2016 03:48
To demonstrate how the above changes work in practice I uploaded the following video

fubar

Login to post a reply

Server time is: 2024-03-29 15:30:46
Your offset time is: 2024-03-29 15:30:46