The math behind MoveEntity

Advanced game related topics
tutiplain
User
User
Posts: 43
Joined: Wed Jun 30, 2010 3:00 am

The math behind MoveEntity

Post by tutiplain »

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!
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Re: The math behind MoveEntity

Post by gnasen »

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 :shock:

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
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: The math behind MoveEntity

Post by IdeasVacuum »

Matrix Math solutions for PB are here:
http://www.purebasic.fr/english/viewtop ... 16&t=45701
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
tutiplain
User
User
Posts: 43
Joined: Wed Jun 30, 2010 3:00 am

Re: The math behind MoveEntity

Post by tutiplain »

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.
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Re: The math behind MoveEntity

Post by gnasen »

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
xorc1zt
Enthusiast
Enthusiast
Posts: 276
Joined: Sat Jul 09, 2011 7:57 am

Re: The math behind MoveEntity

Post by xorc1zt »

User avatar
Psychophanta
Addict
Addict
Posts: 4975
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: The math behind MoveEntity

Post by Psychophanta »

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

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Re: The math behind MoveEntity

Post by gnasen »

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 :P).
pb 5.11
tutiplain
User
User
Posts: 43
Joined: Wed Jun 30, 2010 3:00 am

Re: The math behind MoveEntity

Post by tutiplain »

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()?
User avatar
Psychophanta
Addict
Addict
Posts: 4975
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: The math behind MoveEntity

Post by Psychophanta »

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

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
User avatar
Comtois
Addict
Addict
Posts: 1429
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: The math behind MoveEntity

Post by Comtois »

Ogre3D use Quaternion
Please correct my english
http://purebasic.developpez.com/
User avatar
Comtois
Addict
Addict
Posts: 1429
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: The math behind MoveEntity

Post by Comtois »

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: Select all

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
    	DirectionX = Cos(Radian(Angle))
    	DirectionY = Sin(Radian(Angle))
    	RotateSprite(0, Angle, #PB_Absolute)
    ElseIf KeyboardPushed(#PB_Key_Right)
    	Angle + 1
    	DirectionX = Cos(Radian(Angle))
    	DirectionY = Sin(Radian(Angle))
    	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
Please correct my english
http://purebasic.developpez.com/
User avatar
Psychophanta
Addict
Addict
Posts: 4975
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: The math behind MoveEntity

Post by Psychophanta »

@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

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
Post Reply