# PureBasic Forum

 It is currently Thu Sep 24, 2020 8:56 am

 All times are UTC + 1 hour

 Page 1 of 1 [ 14 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: The math behind MoveEntityPosted: Thu Jan 19, 2012 2:31 pm
 User

Joined: Wed Jun 30, 2010 3:00 am
Posts: 43
Hi all,

If I understand it correctly, the MoveEntity functions moves the entity relative to its current location. If I'm right, this should also take into account the entity's rotation. In other words, if I do MoveEntity(0,0,1), the Entity should move one unit forward in its z axis (assuming the z axis is relative to its current location). Can anyone explain how this works from a mathematical point of view? The reason I ask is because I am trying to write a similar function to use with the Sprite3D engine included with PB.

What I want is to be able to translate a sprite previously rotated with RotateSprite3D() forward or backward, but taking into account its current location. Unfortunately, I spent most of my day yesterday reading up on trigonometric functions and vector math, and I still have no idea how to achieve the desired result. This seems like a fairly standard thing to do in 2D games, so I'm guessing someone out there has to understand how it works. Any info will be greatly appreciated! Thanks!

Top

 Post subject: Re: The math behind MoveEntityPosted: Thu Jan 19, 2012 2:49 pm
 Enthusiast

Joined: Wed Sep 24, 2008 12:21 am
Posts: 282
You have to differ between the local and global coordination system. The global is the "normal" one (for example X/Y axis on your screen in 2d). If you take an object on this plane, it can be described with its position (x/y vector) and rotation (float). This creates an own local system (rotated and shifted). Lets assume the shift is zero (otherwise its little bit more difficult).

To move this object in the global system, there is no problem. Simple increase x/y.

If you want to move it in its own local system, you have to create a transformation matrix. It does nothing else then translating your x/y position in your global system to an position x'/y' in the local system. The inverse of the matrix does always exist and gives you the way back.
If T is this transformation matrix and v your global x/y vector, you achieve v' = T*v through multiplication (and backwards v = T^(-1) * v')
Lets say you want to shift your objects position local with the vector v.
Then you get the localshift v' = T*v. So all you have to do now is shifting it from its position p = x'/y' to p+v'.

This was just to get an idea of how to do it. You can try it yourself, just check "Affine transformations" on http://en.wikipedia.org/wiki/Transformation_matrix

The dimension is not important, you do it in the same way in 2d and 3d (only more dimensional vectors/matrices). You could even rotate 3d spaces in maybe a 5d space

I assumed that you know a little bit about vector/matrix mathematics and want to learn it by yourself. If not and you just want a procedure to do it, just ask

_________________
pb 5.11

Top

 Post subject: Re: The math behind MoveEntityPosted: Thu Jan 19, 2012 3:24 pm
 Always Here

Joined: Fri Oct 23, 2009 2:33 am
Posts: 6258
Location: Wales, UK
Matrix Math solutions for PB are here:
http://www.purebasic.fr/english/viewtopic.php?f=16&t=45701

_________________
IdeasVacuum
If it sounds simple, you have not grasped the complexity.

Top

 Post subject: Re: The math behind MoveEntityPosted: Thu Jan 19, 2012 3:33 pm
 User

Joined: Wed Jun 30, 2010 3:00 am
Posts: 43
Hi, Thx for your quick reply. Actually, I DO want to learn how to do it, but I'm not sure if I'll be able to. I have encountered transformation matrices before (I've delved a bit into Quartz programming in iOS) and understand the basic principle behind them. I just thought there might be a quicker way to do it in PB without having to write matrix multiplication procedures. I did find a sticky post here in the forum which has a lot of what (apparently I need), so I'll look into it. If I'm still unable to get the results I want, then I'll post back here for help.

Top

 Post subject: Re: The math behind MoveEntityPosted: Thu Jan 19, 2012 4:20 pm
 Enthusiast

Joined: Wed Sep 24, 2008 12:21 am
Posts: 282
sure, you can skip all matrix calculations and do all calculations in a more direct way (all matrix operations are built on top of the "normal" math operators). However it would be some kind of unreadable. The math behind it is pretty simple if you understood matrix calculation.

_________________
pb 5.11

Top

 Post subject: Re: The math behind MoveEntityPosted: Thu Jan 19, 2012 8:06 pm
 Enthusiast

Joined: Sat Jul 09, 2011 7:57 am
Posts: 276

http://en.wikipedia.org/wiki/Rotation_m ... _rotations

Top

 Post subject: Re: The math behind MoveEntityPosted: Fri Jan 20, 2012 4:41 pm

Joined: Wed Jun 11, 2003 9:33 pm
Posts: 4627
Location: Spa, relaxing and thinking, and learning...
gnasen wrote:
sure, you can skip all matrix calculations and do all calculations in a more direct way (all matrix operations are built on top of the "normal" math operators).

Indeed.
I never found it in any book or in any publication, but i made in the past a very simple way (supposedly uneditated) to transform coords (rotation and/or moving) without the use of any matrix algebra, but only vectorial algebra.
The only requirement to do it is that everything is given by a vector: position, velocities, accelerations and ALSO ANGLES.
So, with this method, there can be done any transformation in the n-dimensional space, because we are talking about simple vectors.
If anyone have interest in this, tell me and i will put a picture which explain it all.
Anyway, i explain how i went to this method:
if you take in account the main equation of rotation dynamics is:
linear speed = omega ^ Radius ; (being 'linear speed', 'omega' (angular speed) and 'Radius', all vectors, and '^' operator is the vectorial product)
then you can do:
time · linear speed = time · (omega ^ Radius) = (time · omega) ^ Radius ; ('time' is a scalar number, which means that 'time · linear speed' is just a TRAVELLED DISTANCE designed by a vector, and 'time · omega' is not now an angular speed, but just AN ANGLE designed also by a vector)
... the continuation of this math arguing is just simple, and you can continue it, but if you need more, ask for it.

_________________
http://www.zeitgeistmovie.com

Top

 Post subject: Re: The math behind MoveEntityPosted: Fri Jan 20, 2012 6:36 pm
 Enthusiast

Joined: Sat Jul 09, 2011 7:57 am
Posts: 276
doom 3 source code (C++) has rotations without matrix.

https://github.com/TTimo/doom3.gpl/blob ... Angles.cpp
https://github.com/TTimo/doom3.gpl/blob ... h/Angles.h
https://github.com/TTimo/doom3.gpl/blob ... Vector.cpp
https://github.com/TTimo/doom3.gpl/blob ... h/Vector.h

their math lib is great, especially the simd functions:
https://github.com/TTimo/doom3.gpl/tree ... idlib/math

Top

 Post subject: Re: The math behind MoveEntityPosted: Fri Jan 20, 2012 7:23 pm
 Enthusiast

Joined: Wed Sep 24, 2008 12:21 am
Posts: 282
but honestly, its nearly unreadable. Sure, you can do it without matrices, but its just ugly to read. All the calculations have been done with matrices before and then coded written out (hope that exists in english ).

_________________
pb 5.11

Top

 Post subject: Re: The math behind MoveEntityPosted: Sat Jan 21, 2012 9:03 pm
 User

Joined: Wed Jun 30, 2010 3:00 am
Posts: 43
If you guys think that any other way than using matrices is too complex, then I'll stick to that, then. However, will this require me to rewrite all the Sprite3D functions like RotateSprite3D()?

Top

 Post subject: Re: The math behind MoveEntityPosted: Mon May 25, 2015 2:04 pm

Joined: Wed Jun 11, 2003 9:33 pm
Posts: 4627
Location: Spa, relaxing and thinking, and learning...
tutiplain wrote:
If you guys think that any other way than using matrices is too complex, then I'll stick to that, then. However, will this require me to rewrite all the Sprite3D functions like RotateSprite3D()?

Do you really think matrix are complex?
I don't think there are other easier way than using matrix, there are just other ways, but computationally is the same complexity.

You can sear for: 'homogeneous transformation matrix' in the web, and there are (at least in spanish) some tutorials in video and in text.
Matrix are not only useful for 3D but also in robotics, etc.

_________________
http://www.zeitgeistmovie.com

Top

 Post subject: Re: The math behind MoveEntityPosted: Mon May 25, 2015 6:51 pm

Joined: Tue Aug 19, 2003 11:36 am
Posts: 1418
Location: Doubs - France
Ogre3D use Quaternion

_________________
http://purebasic.developpez.com/

Top

 Post subject: Re: The math behind MoveEntityPosted: Mon May 25, 2015 7:38 pm

Joined: Tue Aug 19, 2003 11:36 am
Posts: 1418
Location: Doubs - France
tutiplain wrote:
What I want is to be able to translate a sprite previously rotated with RotateSprite3D() forward or backward, but taking into account its current location. Unfortunately, I spent most of my day yesterday reading up on trigonometric functions and vector math, and I still have no idea how to achieve the desired result. This seems like a fairly standard thing to do in 2D games, so I'm guessing someone out there has to understand how it works. Any info will be greatly appreciated! Thanks!

I know this is an old post, but someone has resurrected it, so ... here an example

Code:
If InitSprite() = 0 Or InitKeyboard() = 0
MessageRequester("Error", "Sprite system can't be initialized", 0)
End
EndIf

If OpenScreen(800, 600, 32, "Sprite")

CreateSprite(0, 64,64)
StartDrawing(SpriteOutput(0))
Line(0,16,1,32, RGB(255,255,0))
LineXY(0,16,64,32, RGB(255,255,0))
LineXY(0,64-16,64,32, RGB(255,255,0))
FillArea(32,32,RGB(255,255,0),RGB(85,85,0))
StopDrawing()

Define.f  Angle, x, y, DirectionX, DirectionY, Vitesse = 2

Repeat

FlipBuffers()

ClearScreen(RGB(0,0,0))

ExamineKeyboard()
If KeyboardPushed(#PB_Key_Left)
Angle - 1
RotateSprite(0, Angle, #PB_Absolute)
ElseIf KeyboardPushed(#PB_Key_Right)
Angle + 1
RotateSprite(0, Angle, #PB_Absolute)
EndIf

If KeyboardPushed(#PB_Key_Up)
x + DirectionX * Vitesse
y + DirectionY * Vitesse
ElseIf KeyboardPushed(#PB_Key_Down)
x - DirectionX * Vitesse
y - DirectionY * Vitesse
EndIf

DisplayTransparentSprite(0,x,y)
Until KeyboardPushed(#PB_Key_Escape)

Else
MessageRequester("Error", "Can't open a 800*600 - 32 bit screen !", 0)
EndIf

_________________
http://purebasic.developpez.com/

Top

 Post subject: Re: The math behind MoveEntityPosted: Mon May 25, 2015 8:36 pm

Joined: Wed Jun 11, 2003 9:33 pm
Posts: 4627
Location: Spa, relaxing and thinking, and learning...
@Comtois,
Just allow a subsection info to everybody:

New algebras was introduced in the past, like matrix (looks like old chinese mathematicians was who introduced it), or complex numbers (introduced by Euler) or Differential calculus (introduced by I. Newton), etc.
Quaternions are not new algebra, quaternions are just matrix renamed as 'quaternions', don't know why.

_________________
http://www.zeitgeistmovie.com

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 14 posts ]

 All times are UTC + 1 hour

#### Who is online

Users browsing this forum: No registered users and 6 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ PureBasic    Coding Questions    Game Programming    3D Programming    Assembly Programming    The PureBasic Editor    The PureBasic Form Designer    General Discussion    Feature Requests and Wishlists    Tricks 'n' Tips Bug Reports    Bugs - Windows    Bugs - Linux    Bugs - Mac OSX    Bugs - IDE    Bugs - Documentation OS Specific    AmigaOS    Linux    Windows    Mac OSX Miscellaneous    Announcement    Off Topic Showcase    Applications - Feedback and Discussion    PureFORM & JaPBe    TailBite