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.

Dark GDK / Moving an object in correlation to its angle...

Author
Message
The Mayflower
16
Years of Service
User Offline
Joined: 17th Dec 2007
Location: Plymouth, Massachusetts
Posted: 27th Dec 2007 00:36
I never was good with trig, so...


Anyone know how to move an object forward based on the way it's turned using its angleX/Z?
Woozl
16
Years of Service
User Offline
Joined: 21st Dec 2007
Location:
Posted: 27th Dec 2007 03:58
Hello Mayflower,

There is an existing function to move an object "forward" (move in the direction it is facing.)

Try this:
dbMoveObject(int ID, float fStep);

I think this is what you are looking for, It allows forward movement and spares you the math

Hope this is what you need!

Regards, Woozl
Captain Waffles
16
Years of Service
User Offline
Joined: 29th Nov 2007
Location:
Posted: 27th Dec 2007 04:44
x = cos(yAngle)*speed;
z = sin(yAngle)*speed;

That's the stuff I use to move the player forwards one step in the direction they're facing at the speed specified.

"Keep laughing... they'll wonder what you're up to."
tempicek
16
Years of Service
User Offline
Joined: 27th Nov 2007
Location: Prague
Posted: 27th Dec 2007 12:34
First, you need to compute the movement vector. This can be performed by converting your spherical coordinates (given by two rotation values and a distance from origin) to cartesian coordinates. You can consider the distance from origin to be unit, so the conversion can be simplified to:



This would work for mathematical coordinate system where Z axis is vertical pointing up. You have to change the code slightly to match with your coordinate system.

Finally, you need to actually move the object, i.e. change its location. You already have the movement vector T(x,y,z), so the new position will be



where dist is a distance you want your object to move by.
FERSIS
18
Years of Service
User Offline
Joined: 17th May 2006
Location:
Posted: 27th Dec 2007 12:45
dbMoveObject doesnt do the trick ?
Woozl
16
Years of Service
User Offline
Joined: 21st Dec 2007
Location:
Posted: 27th Dec 2007 14:15
Hello again,

I should note, the dbMoveObject(...) function DOES move on all three axes.

This means if your object is tilted slightly up or down, as well as turned left or right, it will be moved up or down the Y-axis as well as along the x and z axes.

There are a few ways to overcome this if you want to move only on a flat plane (like along the ground).

MY preferred method (to avoid a lot of sin/cos math) is simply to save the Y position of the object before moving it, then restore it with a call to dbPositionObject(...) after the dbMoveObject(...).

Yes, the 'call to dbPositionObject' approach creates some overhead, though I'm thinking not much more than doing the sin/cos math required for movement limited to 2 axes (I have not tested this and I am no code optimization guru, heh, so I could be way off on that statement)


This is how I do it:


If you are moving along an uneven terrain, you can use this code instead, to adjust the Y position of the moved object to the same relative height above the terrain at its new position:


If someone who understands code efficiency better than I do would care to post a better method for doing this, please do! I'm always willing to learn from a master!

Regards,
Woozl
The Mayflower
16
Years of Service
User Offline
Joined: 17th Dec 2007
Location: Plymouth, Massachusetts
Posted: 28th Dec 2007 01:58
Yay! dbMoveObject() did it

Login to post a reply

Server time is: 2024-09-29 07:28:12
Your offset time is: 2024-09-29 07:28:12