Rotating sprite, go in right direction..?
Blitz3D Forums/Blitz3D Beginners Area/Rotating sprite, go in right direction..?
| ||
Hey, 2D topview car "game" (well, not yet...). The car should be controlled like this: A key to give it some speed, and a key to break/reverse. And two keys to make it turn. I need to rotate the car, and make it go in the direction the car is rotated. I hope you understand.. :) I need some inspiration, as to how the hell this is achieved, because I simply don't know where to begin. I tried something like this: Dim car(2) car(1)=LoadImage("car.png") Just to try something.. I didn't get any farther than this, because apparrently Dim can't be used in conjunction with images.. And I cant Rotate the image 'on the fly', because that's just way too slow.. How is this done? Can someone maybe point me to some relevant example codes, or something? |
| ||
One way which is used in a lot of 2D games is to have images of the car in 8 different directions (North, Northeast, East, etc.) The image which is displayed is the one closest to the direction the car is pointed. As for that specific code, I see no reason that shouldn't work. As a matter of fact I store image handles in arrays all the time. The value returned by LoadImage is just an integer, pointing to the memory address for the loaded image. Are you using DrawImage car(1),x,y to display the image later? |
| ||
Yes I do. But after going through the code, I saw that there was a lot of places where I still used the old variable (car). After updating all these, there was no problem. I didn't even have to ask here about this. I'm very sorry. My mistake. Thanks a lot anyway, for your help! ;-) |
| ||
Another problem, that I hope someone can help me with.. I have rotated the car in 36 directions (1-36), and I have successfully coded it, so it will rotate right, when I press cursor right, and vice versa. But how do I make the car go in a particular direction? Like, when the car is in position "car(1)", it points upwards in a 10 degrees angle, and I want it to go exactly that way. I bet you have to use the SIN and/or COS commands, but I DO NOT understand! I have looked at a bunch of source codes for other BB games, that uses the same method (like asteroid games), but I'm so frustrated - I can't see how it's done (or how it CAN be done). Could someone push me in the right direction, please? |
| ||
All right, What I'm writing now, is true. I'm having a particular problem, which I try to figure out by myself for several days. Then I give up, and seek some help in here. Then within an hour after my post, I figure it out... This is weird, because this is not the first time this has happened.. I'm sorry.. Anyway, if someone else needs it, this is the basic method: Function MoveCarForward() carX=carX-Cos(CarRotatePos*10)*speed carY=carY-Sin(CarRotatePos*10)*speed End Function Function MoveCarBackward() carX=carX+Cos(CarRotatePos*10)*speed carY=carY+Sin(CarRotatePos*10)*speed End Function |
| ||
i have this, managed to dig it out, wrote it a while back for 3d. i've managed to convert it to 2d.Graphics3D 800,600 SetBuffer BackBuffer() ship=CreateImage(10,30) SetBuffer ImageBuffer(ship) Rect 3,0,4,30 Rect 0,20,10,10 SetBuffer BackBuffer() Dim ships(359) For loop=0 To 359 ships(loop)=CopyImage(ship) RotateImage ships(loop),loop MidHandle ships(loop) Next turn=0 speed#=0.00;1 x#=400 y#=300 timer=MilliSecs() time=10 xvector#=0 yvector#=0 While Not KeyHit(1) Cls If KeyDown(205) Then turn=turn+1:If turn=>360 Then turn=turn-360 If KeyDown(203) Then turn=turn-1:If turn<0 Then turn=359 If KeyDown(200) And speed<0 Then speed=speed+0.0001:xvector=xvector-(speed*-Sin(turn)):yvector=yvector-(speed*-Cos(turn)) If KeyDown(200) And speed>=0 Then speed=speed+0.0001:xvector=xvector+(speed*-Sin(turn)):yvector=yvector+(speed*-Cos(turn)) If KeyDown(208) And speed<=0 Then speed=speed-0.0001:xvector=xvector+(speed*-Sin(turn)):yvector=yvector+(speed*-Cos(turn)) If KeyDown(208) And speed>0 Then speed=speed-0.0001:xvector=xvector-(speed*-Sin(turn)):yvector=yvector-(speed*-Cos(turn)) x=x-xvector y=y+yvector DrawImage ships(turn),x,y Text 0,0," turn="+turn+" yturn="+yturn+" Cos(turn)="+Cos(turn)+" sin(turn)="+Sin(turn)+" speed="+speed+" XVector="+xvector+" Yvector="+yvector Flip Wend End |