Rotating an object based upon another
BlitzMax Forums/BlitzMax Beginners Area/Rotating an object based upon another
| ||
Hello, I am trying to create the following scene, one object that rotates and another that I want to mimic its behaviour. In this case, a large boat with some smaller items, crates, on its deck which are individual objects and not part of the boat itself. The crates need to rotate along with the boat and also slide up and down its deck so in other words, when they move left and right along the boats deck they need to be moving along the same angle as the boat. In Blitz 3D I would just set the boat as the parent of the crates, in BMax things seem a little more difficult. Any ideas as to how I could achieve this? Thanks for any help, Jason. |
| ||
Sin and Cos are your fiends! perfect for smooth movement and rotational stuff.Graphics 640,480,0,60 Type boat Field x:Float Field y:Float Field angle:Float=0 Field radius:Float=0 Field crates:TList=New TList Function Create:boat() Local n:boat=New boat n.x=320 n.y=240 For Local i:Int=0 To 10 n.crates.addlast New crate Next Return n End Function Method update() 'rotate the boat. you could rotate this and move it how you like. 'but here you can use the arrow keys If KeyDown(key_up) radius:+3 If KeyDown(key_down) radius:-3 If KeyDown(key_left) angle:-3 If KeyDown(key_right) angle:+3 'stop it from moving to fast If radius>3 Then radius=3 If radius<-3 Then radius=-3 'where is it x=radius*Cos(angle)+x y=radius*Sin(angle)+y 'update the crates, sending the boats details For Local c:crate=EachIn crates c.update(x,y,angle) Next EndMethod Method draw() SetRotation 0 'draw the boat DrawLine 40*Cos(angle-45)+x,40*Sin(angle-45)+y,40*Cos(angle+45)+x,40*Sin(angle+45)+y DrawLine 40*Cos(angle+135)+x,40*Sin(angle+135)+y,40*Cos(angle+45)+x,40*Sin(angle+45)+y DrawLine 40*Cos(angle+135)+x,40*Sin(angle+135)+y,40*Cos(angle+225)+x,40*Sin(angle+225)+y DrawLine 40*Cos(angle+315)+x,40*Sin(angle+315)+y,40*Cos(angle+225)+x,40*Sin(angle+225)+y 'draw the crates For Local c:crate=EachIn crates c.draw() Next EndMethod EndType Type crate Field x:Float Field y:Float 'angle random just so it easier to see whats going on Field angle:Float=Rand(0,359) 'random distance from center of boat Field radius:Float=Rand(-30,30) 'this is the ammount the box is moving Field radius2:Float=0 Field x2:Float Field y2:Float Method update(inx:Float,iny:Float,inAngle:Float) 'work out x and y from the coordinates sent x=radius*Cos(inangle)+inx y=radius*Sin(inangle)+iny 'crates speed, cryptically called radius2, the sin(angle) is just so the mosion is smooth angle:-2 radius2=20*Sin(angle) 'move the crates around the x and y values, at the right angle. x2=radius2*Cos(inangle-90)+x y2=radius2*Sin(inangle-90)+y EndMethod Method draw() SetRotation angle DrawRect x2,y2,5,5 EndMethod EndType 'make a boat Local b:boat=New boat.Create() 'update While Not KeyHit(key_escape) b.update() b.draw() Flip;Cls Wend Hope that helps :) Cheers Charlie |
| ||
jkrankie : That`s a great help and exactly what I was after. Despite several failed attempts of my own it`s good to see that I was almost on the right track. Thanks again for your help, Jason. |
| ||
No problem :) Cheers Charlie |