Jump to content

How to build a ship with two motors?


orisinal
 Share

Recommended Posts

Hi!

 

How do you build a rectangle shaped (2:1) ship that has two motors? Imagine a ship splitted half (2 x 1:1) and one motor is placed in the center of tail and the other one is in the center of the nose. You can see the ship top-down and rotate and thrust these motors with two virtual joysticks, one for each motor.

 

I'm new to game developing, Phaser and physics engines, but I'm not asking ready code from you. I'm just not sure what are the ingredients that should be used to build the ship and I don't want to hack something ridicilous.

 

I think I should P2 as my physics engine. Then I think I could start by creating a ship's tail as a square and somehow attach a rotatable motor in the center of it. Now, if I played with just this half of the ship, the ship itself wouldn't rotate and the rotated motor just moved the (half of the) ship around the world. However, if I created the nose in the same way and attached the nose to the tail with some magic glue, maybe the whole ship started to act more like a real ship, right?

 

I did some tests with different constraints between two sprites and they locked well with each other using  As nothing seemed to work as supposed, I started to play with springs and even though I might be able to hack it together using very stiff springs, it felt so wrong and performance unwise.

 

If that above has any sense:

- How do I attach a rotatable motor in center of square?

- What is the magic glue that should be used to attach the tail to the front?

 

Now I realized that I don't really understand the point in splitting the ship half. I think it came from my original idea that I wouldn't use separate motors, but just control some force vectors on the both halfs. :]

 

Also, feel free to point me to all the threads that has already answered to similar problem. I did search, but I think I don't know the right search terms.

 

Thanks, cheers.

Link to comment
Share on other sites

P2 has a lot more of the kind of features you sound like you require.

 

Not sure what you mean by 'magic glue'. I'm guessing you just want the motors to stay in the same place relative to the main body section.

 

The P2 site has lots of examples of things you may want to base your idea on.

 

http://schteppe.github.io/p2.js/demos/prismatic.html

http://schteppe.github.io/p2.js/demos/car.html

Link to comment
Share on other sites

Ah, right. Thank you Arcanorum!

 

I actually looked at the car example of P2 before, but when I started to play with Phaser I didn't look for answers outside the Phaser docs and examples :)

 

The car example is pretty much what I need for this, I think. The motors are kind of wheels, so the "magic clue" I wrote about is this:

 

// Constrain wheels to chassis with revolute constraints.
// Revolutes lets the connected bodies rotate around a shared point.
revoluteBack = new p2.RevoluteConstraint(chassisBody, wheelBody1, {
    localPivotA: [-0.5, -0.3],   // Where to hinge first wheel on the chassis
    localPivotB: [0, 0],
    collideConnected: false
});
 
(Sorry didn't know how to do a block of code here)
Link to comment
Share on other sites

imho this shouldn't be too hard.

 

for the sake of the example lets say the ship is a rectangle (rec1) and the motors are two circles (circ1 circ2)

 

you'd have to create all 3 sprites

activate physics on them

glue them together with revoluteConstraints

 

[      ]

o    o

constraint1=game.physics.p2.createRevoluteConstraint(circ1, [0,-10], rec1, [-20,40], 10000);constraint2=game.physics.p2.createRevoluteConstraint(circ2, [0,-10], rec2, [20,40], 10000);

the numbers in the brackets are the offsets x and y of the anchor on the given physics body.

 

then program a routine to stear those two circles with moveright moveleft moveup .. or better use "thrust" http://docs.phaser.io/Phaser.Physics.P2.Body.html#thrust and "reverse" instead of "move"

Link to comment
Share on other sites

This is so much fun :P Thank you for your help, all of you. Actually I figured it out after Arcanorum gave me some clue. I did practically what valueerror suggested in above, using Revolute Constraints to attach the motors with the ship and thrust to give some gas to motors, so the motor accelerates in the right direction.

var motor1_axel = game.physics.p2.createRevoluteConstraint(motor1, [ 0, 0 ], deck, [ -20, 80]);var motor2_axel = game.physics.p2.createRevoluteConstraint(motor2, [ 0, 0 ], deck, [ -20, -80]);

There's one thing I didn't see coming, but it's quite obvious. I'd like the motors to turn as the ship turns, but now the motors act like compasses (with adjustable north) when the ship rotates. I haven't tried to fix that yet, but I think I just need to create a function that calculates the rotation of motor based on ship's rotation and use that function inside update() whenever ship moves.

 

 

I've been struggling most with pixel perfect positioning. It seems that I can't just drop my sprites anywhere in the world and then glue them somewhere else using constraints. If I do that, things start to fly around violently and even if the sprite is quite close to correct position it drags the connected object and things wobbles until every pieces has find settled to their places. But then again, I haven't really looked at the solutions for this and I think I can find answer to this from other examples.

 

Great framework and very supportive forum. Hopefully I can publish the addres of my pet project here some day :)

Link to comment
Share on other sites

if you pull a circle from the center it will not rotate.. pull it from a point near its border.. iguess setting [0.0] to something like [0,-10]should do it... thats why i wrote it initially

 

I'm sorry, but that's not the issue. I can control and rotate the ship as I want with my two motors, but if the ship is turning I have to constantly adjust the rotation of motors to keep them pointing in the same direction relative to ship. Do you know what I mean?

Link to comment
Share on other sites

well.. if you are that sure that this is not the issue .. i must have missunderstood you .... or you didn't consider testing my advice..

i thought you might want something like that :

http://test.xapient.net/phaser/ALL/spaceship.html (use left/right for the motors to thrust and up/down for the ship to thrust)

i just put this up to test my theory (of what i thought is what you want) if i set the anchor to 0,0 the circles will stop rotating with the ship and i cant steer it anymore..

Link to comment
Share on other sites

Ah, thanks for the example. That's not quite what I want, but I admit that my original post was easy to misunderstand... The circles (motors) are positioned underneath the rectangle (ship) and the circles are independently rotated and thrusted/reversed by the player. So the layout is this:

 

[ o  o ]

 

Your ship's motors rotates with the ship due to fact that the constraints of the circles are eccentric and the circles keep colliding with the rectangle. In other words the circles have no room to turn around, because the axel in the circle isn't in the center [0, -10]. I want my motors to be rotated by player, but of course they should keep their rotation relative to ship, too.

 

Anyways, I think I'll find a solution for that and I've already been aswered to my original question :) Thanks for your help anyway.

Link to comment
Share on other sites

:) This is what I ended up to do in update():

motor1.body.angularVelocity = deck.body.angularVelocity;if(cursors.left.isDown) {	motor1.body.angularVelocity -= 3;} else if(cursors.right.isDown) {	motor1.body.angularVelocity += 3;}

Not sure if it's  a funny hack, but hey, it works!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...