generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Get corners of a rotated Gui

Previous Thread :: Next Thread 
EgoMoose is not online. EgoMoose
Joined: 04 Feb 2009
Total Posts: 2896
29 Mar 2015 02:04 PM
So over the past couple of days I've been working on Gui raycasting. One of the problems I've been having is getting the corners of GuiObjects that are rotated.

Help would be greatly appreciated!

As a side note, if you want to see the raycasting so far:
http://www.roblox.com/games/227732618/Gui-raycasting
Report Abuse
Darkmist101 is not online. Darkmist101
Joined: 30 Jul 2010
Total Posts: 6208
29 Mar 2015 02:12 PM
You should google it.
Report Abuse
Darkmist101 is not online. Darkmist101
Joined: 30 Jul 2010
Total Posts: 6208
29 Mar 2015 02:12 PM
Or cheat and place frames in the corners.
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
29 Mar 2015 02:25 PM
I had a problem with this because the rotation is around the center of the element, but the position is based on the top-left corner at rotation 0. I never did have the patience to work out the math for it. :/
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
29 Mar 2015 02:28 PM
Are you using Scale AND Offset or one or the other?

Depending on which I MAY be able to help you :)
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
29 Mar 2015 02:29 PM
You could just use AbsolutePosition to get the offset location.
Report Abuse
EgoMoose is not online. EgoMoose
Joined: 04 Feb 2009
Total Posts: 2896
29 Mar 2015 02:54 PM
@ AgentFirefox

Could you give me an example?
Report Abuse
yobo89 is not online. yobo89
Joined: 05 Jun 2010
Total Posts: 2341
29 Mar 2015 02:56 PM
Nice project, it could lead to a very cool genre of games.
Report Abuse
kingkiller1000 is not online. kingkiller1000
Joined: 12 Dec 2008
Total Posts: 26415
29 Mar 2015 03:08 PM
The position of any individual point (where the origin is the middle of the Gui) can be determined from these formula:

x' = xcosθ - ysinθ
y' = xsinθ + ycosθ

Where x and y are the coordinates (relative to the middle of teh Gui) when theta is 0.
Report Abuse
ThaneTheCreator is not online. ThaneTheCreator
Joined: 08 Apr 2009
Total Posts: 2182
29 Mar 2015 03:15 PM
I used this in a Gui based drawing game I'm making, it allows you to create lines from one point to another.

I'm not exactly sure how to convert this to what you want, but you can figure that out:

local y = Start.Y-Y
local x = Start.X-X
local Angle = -math.deg(math.atan(x/y))
local Length = math.sqrt((x*x)+(y*y))
Current.Rotation = Angle
Current.Size = UDim2.new(0,Size,0,Length)
Current.Position = UDim2.new(0,Start.X,0,Start.Y) - UDim2.new(0,x/2,0,y/2) - UDim2.new(0,0,0,Length/2)
Report Abuse
ThaneTheCreator is not online. ThaneTheCreator
Joined: 08 Apr 2009
Total Posts: 2182
29 Mar 2015 03:16 PM
X and Y are the coordinates of the mouse, and Start.X and Start.Y are where the line starts.
Report Abuse
EgoMoose is not online. EgoMoose
Joined: 04 Feb 2009
Total Posts: 2896
29 Mar 2015 03:19 PM
I used king's method and it's really darn close but I think my formula's missing something. It's always off by just a few pixels. Any help with what I've got so far?

--// shapeCentre and corner are Absolute values stored in vector2
local x = shapeCentre.x + (corner.x-shapeCentre.x) * math.cos(rot) + (corner.y-shapeCentre.y) * math.sin(rot)
local y = shapeCentre.y - (corner.x-shapeCentre.x) * math.sin(rot) + (corner.y-shapeCentre.y) * math.cos(rot)



Report Abuse
kingkiller1000 is not online. kingkiller1000
Joined: 12 Dec 2008
Total Posts: 26415
29 Mar 2015 03:27 PM
The x is cosine minus sine, not plus.
Report Abuse
EgoMoose is not online. EgoMoose
Joined: 04 Feb 2009
Total Posts: 2896
29 Mar 2015 03:47 PM
Still has the same problem. It's off by a few pixels...
Report Abuse
EgoMoose is not online. EgoMoose
Joined: 04 Feb 2009
Total Posts: 2896
29 Mar 2015 03:49 PM
Here I uploaded a model in case anyone wants to try themselves but is too lazy to actually write up the tedious base stuff. :D

http://www.roblox.com/shape-corners-item?id=231765514
Report Abuse
kingkiller1000 is not online. kingkiller1000
Joined: 12 Dec 2008
Total Posts: 26415
29 Mar 2015 03:50 PM
Also the sin in y should be positive. Do center.y + whatever*sin
Report Abuse
ThaneTheCreator is not online. ThaneTheCreator
Joined: 08 Apr 2009
Total Posts: 2182
29 Mar 2015 03:53 PM
Did you add half the width and half the height to what your positioning?
Report Abuse
DrMathematica is not online. DrMathematica
Joined: 29 Aug 2008
Total Posts: 27268
29 Mar 2015 03:56 PM
It's off by a few pixels because the Y value is not exactly as he says. It's a versine (1-cosine)

local XOffset = (1-math.cos(math.rad(Rotation))) * (XSizeInPixels/2)
Report Abuse
DrMathematica is not online. DrMathematica
Joined: 29 Aug 2008
Total Posts: 27268
29 Mar 2015 03:57 PM
*Because the X value
Report Abuse
kingkiller1000 is not online. kingkiller1000
Joined: 12 Dec 2008
Total Posts: 26415
29 Mar 2015 04:14 PM
What? My formula works just fine.
Report Abuse
EgoMoose is not online. EgoMoose
Joined: 04 Feb 2009
Total Posts: 2896
29 Mar 2015 04:54 PM
I can't get it for the life of me :(

If I ever do I'll post the code. Thanks for the help though everybody. I'm closer than I was before!
Report Abuse
EgoMoose is not online. EgoMoose
Joined: 04 Feb 2009
Total Posts: 2896
29 Mar 2015 05:26 PM
Well, I derped pretty hard guys!!

I didn't have the variable rot as a radian value...

I hate when I find a question and just a guy going "Got it!". So here's the final product for anyone drifting through this thread in the future.

wait()
local Func = {}
local world = script.Parent:WaitForChild("World") --// Set this, as a containing frame with UDim2.new(1, 0, 1, 0) because we need to account for coreUI

--// Functions

Func.getPos = function(obj, vec2)
return Vector2.new(
obj.AbsolutePosition.x + (obj.AbsoluteSize.x*vec2.x),
obj.AbsolutePosition.y + (obj.AbsoluteSize.y*vec2.y)
)
end

Func.getAbsoloute = function(UD)
local base = world.AbsoluteSize
local x, y = UD.X.Offset, UD.Y.Offset
local sx, sy = UD.X.Scale*base.x, UD.Y.Scale*base.y
return Vector2.new(math.floor((x+sx)+.5), math.floor((y+sy)+.5))
end

Func.getCorners = function(shape)
local rot = math.rad(shape.Rotation)
local shapeCentre = Func.getPos(shape, Vector2.new(.5, .5))
local shapePos = Func.getAbsoloute(shape.Position)
local shapeSize = Func.getAbsoloute(shape.Size)
--// First get corners without the rotation
local baseCorners = {
[1] = UDim2.new(0, shapePos.x, 0, shapePos.y); --// Top left
[2] = UDim2.new(0, shapePos.x, 0, shapePos.y) + UDim2.new(0, shapeSize.x, 0, 0); --// Top right
[3] = UDim2.new(0, shapePos.x, 0, shapePos.y) + UDim2.new(0, shapeSize.x, 0, shapeSize.y); --// Bottom right
[4] = UDim2.new(0, shapePos.x, 0, shapePos.y) + UDim2.new(0, 0, 0, shapeSize.y); --// Bottom left
}
local corners = {}
for index = 1, 4 do
local corner = Func.getAbsoloute(baseCorners[index])
local x = shapeCentre.x + (corner.x-shapeCentre.x) * math.cos(rot) - (corner.y-shapeCentre.y) * math.sin(rot)
local y = shapeCentre.y + (corner.x-shapeCentre.x) * math.sin(rot) + (corner.y-shapeCentre.y) * math.cos(rot)
corners[index] = Vector2.new(x, y)
end
return corners
end

Func.getCorners(script.Parent:WaitForChild("Frame"))
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image