|
Post by johnno56 on Jul 9, 2019 22:45:42 GMT -6
I know there are 'built in' commands to rotate an image, what I would like to know is, how to do it 'old school'? I'm not talking 3D but simple (well maybe not so simple) 2D rotation routines. The item to be rotated can be either text, 2D primitive or even a sprite.
I would be curious to see how it it done without the aid of a built in 'rotate' command.
Reason: I would like to tackle a simple space shooter (keyboard/mouse controlled rotating ship) and maybe the odd bad guy or space rock. Just to see if I can....
No rush. Just curious.
Oh. Whilst on the subject of 2D.... How difficult would it be to create a "Box2D"-type library? (That should get your grey-matter churning... lol)
J
|
|
|
Post by n00b on Jul 9, 2019 23:22:46 GMT -6
There is a lot to answer here.
To answer your first question:
PI = 3.14159265359
Sub rotatePoint(pt_x, pt_y, center_x, center_y, angleDeg, ByRef new_x, Byref new_y)
angleRad = (angleDeg/180)*PI cosAngle = Cos(angleRad) sinAngle = Sin(angleRad) dx = (pt_x-center_x) dy = (pt_y-center_y)
new_x = center_x + int(dx*cosAngle-dy*sinAngle) new_y = center_y + int(dx*sinAngle+dy*cosAngle) End Sub
Here is a break down of how this function works:
pt_x, pt_y - the x,y of the point you are rotating
center_x, center_y - the point you are rotating around
angleDeg - The angle you are rotating by
new_x, new_y - where the new coordinates for the rotated point will be stored
As for how to build a physics engine this is a lot more complex. Box2D does rigid body simulation with a complex algorithm that accounts for a number of different properties. Here are the basic properties you will need to implement.
Gravity - Force applied on all objects Weight - Additional force applied on each object External Force - Consist of a direction and intensity that will be applied to an object Shape - Used to determine collision and response to external forces Depending on the complexity you are going for there are many other properties to consider but these are the basics for almost every physics engine.
|
|
|
Post by johnno56 on Jul 10, 2019 1:47:08 GMT -6
Cool. Thanks for the routine. I will test it out when I get home. I figured the "other" stuff would be difficult but it was worth a try. Thanks for that as well. J
|
|
|
Post by johnno56 on Jul 10, 2019 16:39:47 GMT -6
Well... I tried it...
I'm not having much success.
I call the routine using, rotatePoint(planex,planey,planex+32,planey+32,angle,0,0), so I can rotate the image in the top left corner. (the plane is a 64x64 png image - just for testing purposes...) But, when the routine is called, I get "Error on Line 46 in rotate01.bas: Expected identifier for ByRef argument" (line 46 is in Geany. "Sub rotatePoint(pt_x, pt_y, center_x, center_y, angleDeg, ByRef new_x, ByRef new_y)"
I have not come up against 'ByRef' before and have no clue as to how to fix it.
Are you able to provide an example of how the command is called? I just want to eliminate a probable user error....
Thank you
J
|
|
|
Post by tbird on Jul 10, 2019 17:12:40 GMT -6
You have to supply an already declared variable for ByRef to function. So think of it has its returning 2 variables new_x, new_y. What your doing when using ByRef is providing a reference to the variables and changing them in the subroutine.
PI = 3.14159265359 Sub rotatePoint(pt_x, pt_y, center_x, center_y, angleDeg, ByRef new_x, Byref new_y) angleRad = (angleDeg/180)*PI cosAngle = Cos(angleRad) sinAngle = Sin(angleRad) dx = (pt_x-center_x) dy = (pt_y-center_y) new_x = center_x + int(dx*cosAngle-dy*sinAngle) new_y = center_y + int(dx*sinAngle+dy*cosAngle) End Sub DIM nextX DIM nextY DIM change Screen_Width = 800 Screen_Height = 600
SetColor(RGB(255,100,100)) WindowOpen(0, "RotateME!", 0, 0, Screen_Width, Screen_Height, 1) CanvasOpen(0, Screen_Width, Screen_Height, 0, 0, Screen_Width, Screen_Height, 1) Canvas(0) While NOT Key(K_ESCAPE) ClearCanvas() If Key(K_SPACE) Then change = change + 15 End If rotatePoint(300, 300, 400, 300, change, nextX, nextY) Box(nextX, nextY, nextX + 20, nextY + 20) Update() Wend
|
|
|
Post by johnno56 on Jul 10, 2019 17:45:55 GMT -6
Ok. That program worked. But it was not what I was expecting... Sorry. I blame my inability to explain my intentions the first time... This program rotates an object around a central point (like an orbit) but what I am after is the 'old school' equivalent of RC's DrawImage_Rotate(). The object rotates around its own centre-point. Exactly like the ship in Asteroids.
My apologies for being so difficult....
J
|
|
|
Post by tbird on Jul 10, 2019 18:22:39 GMT -6
Ah, you would have to use the Rotate command I believe.
|
|
|
Post by tbird on Jul 10, 2019 18:32:15 GMT -6
Unless your just talking primitives..then you would just use the pt_x, pt_y where the origin would be, then the center for the center and newx and newy for the new coordinates for the origin...?
|
|
|
Rotation
Jul 25, 2019 7:21:18 GMT -6
via mobile
Post by eyfenna on Jul 25, 2019 7:21:18 GMT -6
I think you are quite correct kenn thouhh I think this has to be applied on every pixel of the picture. N00b provides a easy to understand algorithm for a mathematical way more difficult rotation formula.
|
|