Rotation and Moving

Kamac

Active Member
Licensed User
Longtime User
Hi.

I'd very need some examples of two functions!

1. Rotation -
Canvas DrawBitmapRotated which is rotated to my last X and Y touch on screen.

2. Moving -
Canvas DrawBitmap(...) which would keep moving to the X and Y touch on screen. (I belive vectors are needed)


I guess 1. is easier to-do than 2., but answers for any of these questions will be very appreciated
 

thedesolatesoul

Expert
Licensed User
Longtime User
I did somework a while a go similar to this.
But I didnt find decent graphics so my project is currently shelved as I work on something else.

But for rotation, you are looking for the angle between your object and the touch coords.
RadianAngle = ATan((Pp.y - touch.y)/ _
(Pp.x - touch.x) )
Use this angle to draw the rotated bitmap...
But you also need to know what angle your original bitmap is aligned to.
 
Upvote 0

Kamac

Active Member
Licensed User
Longtime User
You should have a look at chapter 14 Graphics / Drawing in the Beginner's Guide.
There you have examples for both questions.

Best regards.
________________

Um, not really.

Have a look at a example i've made for PC.

1. Sprite rotates to the place where the mouse has been clicked.

2. Sprite moves to the place where the mouse has been clicked.


The example for PC - look rar




Thanks for help, i've sorted that out while making that example

Last thing to do is to get some moving function :|
 
Upvote 0

Kamac

Active Member
Licensed User
Longtime User
Uhm, if you asked for what i mean then -

I didn't make that example in B4PPC nor B4A but other PC platform language.

And if not, i guess you did mean B4A not B4PPC? Since i don't know B4PPC syntax


Here's DarkBASIC Pro code which it was made in:

B4X:
Load Image "man.png",1

Sprite 1,400,200,1
offset sprite 1,25,25

size = 50000
Dim oldx(size)
Dim oldy(size)
Dim oldrot(size)
namb = 0
numb = 0

x = 400
y = 200
do
   if mouseclick()=2
      if numb > 0
         sprite 1,oldx(numb),oldy(numb),1
         rotate sprite 1,oldrot(numb)
         dec numb
         namb = numb
      endif
   else
      if mouseclick()
         if sprite x(1)<>mousex() and sprite y(1)<>mousey()
            pointsprite(1,mousex(),mousey())
            move sprite 1,0.04
         endif
      endif
      //Sprite 1,sprite x(1),sprite y(1),1
      Text 0,0,"Made by Kamac @ basic4ppc.com"
      
      
      
      oldx(namb) = sprite x(1)
      oldy(namb) = sprite y(1)
      oldrot(namb) = sprite angle(1)
      inc namb
      if namb > size then namb = 0
      numb = namb
   endif
loop





function pointsprite(s,x,y)

dx = sprite x(s) - x
dy = sprite y(s) - y
ang# = wrapvalue(atanfull(dx,dy)*-1)
rotate sprite s,ang#

endfunction

But actually it's not the raw rotation & movement which you saw, while being bored i've added Braid-like time reversing :icon_clap:


So here's rotation function:

B4X:
function pointsprite(s,x,y)

dx = sprite x(s) - x
dy = sprite y(s) - y
ang# = wrapvalue(atanfull(dx,dy)*-1)
rotate sprite s,ang#

endfunction

It should look different in B4A, but i am unsure how to convert atanfull.

And DBPro has built-in command called: Move Sprite

used here:

B4X:
if sprite x(1)<>mousex() and sprite y(1)<>mousey()   //If sprite isn't at mouse 
   pointsprite(1,mousex(),mousey()) //rotate
   move sprite 1,0.04                    //move
endif

I'd know how to move sprite, but all that DBPro has is built-in Move Sprite, so i can't check how was it made to convert it to B4A
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
It should look different in B4A, but i am unsure how to convert atanfull.

I'd know how to move sprite, but all that DBPro has is built-in Move Sprite, so i can't check how was it made to convert it to B4A

isnt atanfull = Atan in B4A?

Also to move a sprite, you just change its coordinates and redraw it.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
look @ my example.

It's not 'teleportation' but movement - in - direction - of :S

Yes, I understand. I was trying to create an example for you but the maths is going wrong :s

You need to enable a timer when the user touches the screen and disable it when the sprite reaches the touch coordinates.

The sprite needs to have a constant velocity. The two components Vx and Vy will depend on dx and dy.
So each timer tick:
sprite.x = sprite.x + vx
sprite.y = sprite.y + vy

where Vx = V.cos(Angle)
and Vy = V.sin(Angle)
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
boy, do I have something cool for you

this example is from the Djembefola's joystick example (that I have somehow being modifying for all examples!) ...so credits to him

the sprite just follows mouse movements

additional cool effect is that the speed slows down as it approaches the touch point so it looks really alive
 

Attachments

  • touchfollow.zip
    24.4 KB · Views: 349
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Here you have another version, not yet finished.
But it's time to go to bed, tomorrow I stand up early and will be absent the whole day hiking in the mountains.

Best regards.
what an awesome life hiking in the mountains! always wanted to go to switzerland!

EDIT: by the klaus your example is really cool. It looks like a cat is chasing my mouse as i drag it around!
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…