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 Studio Chat / [SOLVED] How to position a 3d object by offset to another object?

Author
Message
JonEnstrom
User Offline
Joined: 27th Apr 2020
Location:
Posted: 30th Jun 2020 15:55
Is there an easy way to position an object by offset based on another object?

For example, I have a cannon object and want to create a cannonball object that originates right in front of the turret. If the cannon was in a fixed position then this is easy enough, I could simply position the cannonball by adding the appropriate x y and z to the position of the cannon object.

My problem arises when the cannon rotates to face a new direction. How can I make the cannonball originate right in front of the turret no matter which direction it's facing? The Y component wouldn't change since the cannon rotates on the Y axis, but the X and Z component need to change.

Right now I am converting the Y angle of the cannon to radians and then setting position of the cannonball with sin(yrads) for X and cos(yrads) for Z which is working somewhat but not perfectly and I don't understand the math well enough to fix it.

Am I overthinking the process? Is there a more basic way to accomplish what I need?

The author of this post has marked a post as an answer.

Go to answer

hendron
4
Years of Service
User Offline
Joined: 20th Dec 2015
Location:
Posted: 30th Jun 2020 16:22
You could try using FixObjectToObject() to attach a hidden dummy object to the cannonball spawn point on the turret, then use GetObjectWorldX(), GetObjectWorldY(), and GetObjectWorldZ() on the dummy object to find the offset spawn point.

Alternatively, another quick and dirty way to do it would be to temporarily reposition the turret using it's local axes, after you've rotated it, to find the spawn point.

JonEnstrom
User Offline
Joined: 27th Apr 2020
Location:
Posted: 30th Jun 2020 16:42
Thanks very much! The dummy object idea should work perfectly.

Your alternative code snippet doesn't make sense to me unfortunately, I can't see how moving the turret to the original cannonball spawn offset and then back again would do anything useful.. Wouldn't that just load up spawnx# spawny# and spawnz# with the same value as xoffset# yoffset# and zoffset#? Also you don't rotate the turret in the code, so maybe I am missing your concept.

It's neither here nor there mind you, the dummy object fixed to the cannon turret will work perfectly. I knew there was an easier way! I appreciate you taking the time to answer.
hendron
4
Years of Service
User Offline
Joined: 20th Dec 2015
Location:
Posted: 30th Jun 2020 17:06 Edited at: 30th Jun 2020 17:09
Glad to help.

Quote: "I can't see how moving the turret to the original cannonball spawn offset and then back again would do anything useful.. Wouldn't that just load up spawnx# spawny# and spawnz# with the same value as xoffset# yoffset# and zoffset#"


It's actually moving the turret on it's local axes by an amount relative to its current position. If your turret was always positioned at 0,0,0 and not rotated at all, then yes, the spawn coordinates would equal the offset coordinates. However, if the turret is in any other position or rotated (or both), the spawn coordinates will be the offset coordinates relative to the turret's current position and rotation. Try running this snippet to see what I mean. The cube is the turret and the sphere is the spawn point.

JonEnstrom
User Offline
Joined: 27th Apr 2020
Location:
Posted: 30th Jun 2020 17:29
OK now I understand. It's a roundabout way to get the world coordinates of (in your example) +6 units on the z axis from the original object. It seems like there should be a simpler way to get those coordinates without moving the object there, extracting the coords and then moving it back (I imagine this would get computationally expensive if scaled up to many objects).

Thanks again very much for your solutions!
JonEnstrom
User Offline
Joined: 27th Apr 2020
Location:
Posted: 30th Jun 2020 18:14 Edited at: 1st Jul 2020 23:10
This post has been marked by the post author as the answer.
Thinking about this a bit further, I wonder if I could simply spawn the cannonball and position it at the turret location, then rotate the cannonball to the match the current angle of the turret, and then move the cannonball along the local axes by the required offset.

EDIT: This worked perfectly, I just had to rotate the cannonball to match the orientation of the turret object before offsetting it, which means that yes I was overthinking it! Still a big thank you and credit to Hendron for your answers because you got me thinking in the right direction.

Login to post a reply

Server time is: 2020-08-04 03:19:53
Your offset time is: 2020-08-04 03:19:53